From 0fa26e8067affc02fd743018bf1919d7e69ae285 Mon Sep 17 00:00:00 2001 From: luozukai Date: Wed, 4 Sep 2019 10:19:32 +0800 Subject: [PATCH] =?UTF-8?q?[=E5=90=88=E5=90=8C][=E4=BF=AE=E6=94=B9][?= =?UTF-8?q?=E5=BC=80=E5=8F=91=E7=BB=93=E7=AE=97=E5=8D=95]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../iformall/domain/vo/WordDocTableVo.java | 19 ++ .../service/impl/WxBillAllServiceImpl.java | 31 +-- .../java/com/iformall/utils/WordUtil.java | 183 ++---------------- 3 files changed, 54 insertions(+), 179 deletions(-) create mode 100644 mallinkService/src/main/java/com/iformall/domain/vo/WordDocTableVo.java diff --git a/mallinkService/src/main/java/com/iformall/domain/vo/WordDocTableVo.java b/mallinkService/src/main/java/com/iformall/domain/vo/WordDocTableVo.java new file mode 100644 index 000000000..820240bb0 --- /dev/null +++ b/mallinkService/src/main/java/com/iformall/domain/vo/WordDocTableVo.java @@ -0,0 +1,19 @@ +package com.iformall.domain.vo; + +import lombok.Data; + +import java.io.Serializable; +import java.util.List; + +@Data +public class WordDocTableVo implements Serializable{ + + private static final long serialVersionUID = 1L; + + private List> params; + + private int tableIndex = 0; + + //替换的行 + private int rowIndex = 0; +} diff --git a/mallinkService/src/main/java/com/iformall/service/impl/WxBillAllServiceImpl.java b/mallinkService/src/main/java/com/iformall/service/impl/WxBillAllServiceImpl.java index 72048e249..ffe2f850a 100644 --- a/mallinkService/src/main/java/com/iformall/service/impl/WxBillAllServiceImpl.java +++ b/mallinkService/src/main/java/com/iformall/service/impl/WxBillAllServiceImpl.java @@ -814,37 +814,42 @@ public class WxBillAllServiceImpl implements WxBillAllService { result.put("payMU", PriceUtil.digitUppercase(payM.doubleValue())); result.put("bMU", PriceUtil.digitUppercase(bM.doubleValue())); - List> rowsParams = new ArrayList<>(); + WordDocTableVo wordDocTableVo = new WordDocTableVo(); + List> rowsParams = new ArrayList<>(); int size = wxBillSettle.getReceiveBillIds().size() > wxBillSettle.getPayBillIds().size()?wxBillSettle.getReceiveBillIds().size():wxBillSettle.getPayBillIds().size(); int count = 1; for (int i = 0; i < size ; i++) { WxBillSettleBill bill = i>wxBillSettle.getReceiveBillIds().size()-1?null:wxBillSettle.getReceiveBillIds().get(i); - Map map = new HashedMap(); - map.put("s1",count+""); + List cellList = new ArrayList<>(); + cellList.add(count+""); + if(bill != null){ - map.put("bn1",bill.getBillName()); - map.put("money1",bill.getReceivePay()); + cellList.add(bill.getBillName()); + cellList.add(bill.getReceivePay()); }else{ - map.put("bn1",""); - map.put("money1",""); + cellList.add(""); + cellList.add(""); } WxBillSettleBill payBill = i>wxBillSettle.getPayBillIds().size()-1?null:wxBillSettle.getPayBillIds().get(i); if(payBill != null){ - map.put("bn2",payBill.getBillName()); - map.put("money2",payBill.getReceivePay()); + cellList.add(payBill.getBillName()); + cellList.add(payBill.getReceivePay()); }else{ - map.put("bn2",""); - map.put("money2",""); + cellList.add(""); + cellList.add(""); } - rowsParams.add(map); + rowsParams.add(cellList); count++; } + wordDocTableVo.setParams(rowsParams); + wordDocTableVo.setTableIndex(0); + wordDocTableVo.setRowIndex(2); String templatePath = "contract-word-template/settle.docx"; String filepath = fmUploadDir; String filename = UUID.randomUUID() + ".docx"; String exportFileName = "结算单.docx"; - WordUtil.exportWord(templatePath, filepath, filename, exportFileName, result, request, response,rowsParams); + WordUtil.exportWord(templatePath, filepath, filename, exportFileName, result, request, response,wordDocTableVo); } diff --git a/mallinkService/src/main/java/com/iformall/utils/WordUtil.java b/mallinkService/src/main/java/com/iformall/utils/WordUtil.java index e05254d85..780404e82 100644 --- a/mallinkService/src/main/java/com/iformall/utils/WordUtil.java +++ b/mallinkService/src/main/java/com/iformall/utils/WordUtil.java @@ -1,6 +1,7 @@ package com.iformall.utils; import cn.afterturn.easypoi.word.WordExportUtil; +import com.iformall.domain.vo.WordDocTableVo; import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.StringUtils; import org.apache.poi.xwpf.usermodel.*; @@ -36,7 +37,7 @@ public class WordUtil { * @param request HttpServletRequest * @param response HttpServletResponse */ - public static void exportWord(String templatePath, String temDir, String fileName, String exportFileName, Map params, HttpServletRequest request, HttpServletResponse response,List> rowsParams) { + public static void exportWord(String templatePath, String temDir, String fileName, String exportFileName, Map params, HttpServletRequest request, HttpServletResponse response, WordDocTableVo wordDocTableVo) { Assert.notNull(templatePath, "模板路径不能为空"); Assert.notNull(temDir, "临时文件路径不能为空"); Assert.notNull(exportFileName, "导出文件名不能为空"); @@ -58,8 +59,8 @@ public class WordUtil { XWPFDocument doc = WordExportUtil.exportWord07(templatePath, params); - if(!CollectionUtils.isEmpty(rowsParams)) - insertValueToTable(doc,rowsParams,0); + if(wordDocTableVo != null) + insertValueToTable(doc,wordDocTableVo); String tmpPath = temDir + fileName; FileOutputStream fos = new FileOutputStream(tmpPath); @@ -88,26 +89,30 @@ public class WordUtil { } catch (Exception e) { e.printStackTrace(); } - } /** * 循环填充表格内容 * @param xwpfDocument - * @param params - * @param tableIndex * @throws Exception */ - public static void insertValueToTable(XWPFDocument xwpfDocument, List> params, int tableIndex) throws Exception { + public static void insertValueToTable(XWPFDocument xwpfDocument, WordDocTableVo wordDocTableVo) throws Exception { List tableList = xwpfDocument.getTables(); - XWPFTable table = tableList.get(tableIndex); + XWPFTable table = tableList.get(wordDocTableVo.getTableIndex()); List rows = table.getRows(); - if(rows.size()<2){ - throw new Exception("tableIndex对应表格应该为2行"); - } + int rowIndex = wordDocTableVo.getRowIndex(); + XWPFTableRow tmpRow = rows.get(rowIndex); + List cellList = tmpRow.getTableCells(); + + for (List stringList : wordDocTableVo.getParams()) { + for (int i = 0; i < stringList.size(); i++) { + cellList.get(i).setText(stringList.get(i)); + } + table.addRow(tmpRow,rowIndex); + rowIndex++; + } - table.addRow(rows.get(2),2); // //模板的那一行 // XWPFTableRow tmpRow = rows.get(2); @@ -145,161 +150,7 @@ public class WordUtil { // table.removeRow(2); } - /** - * 复制模板行的属性 - * @param tmpCell - * @param cell - * @param text - * @throws Exception - */ - public static void setCellText(XWPFTableCell tmpCell, XWPFTableCell cell,String text) throws Exception { - CTTc cttc2 = tmpCell.getCTTc(); - CTTcPr ctPr2 = cttc2.getTcPr(); - CTTc cttc = cell.getCTTc(); - CTTcPr ctPr = cttc.addNewTcPr(); - if (ctPr2.getTcW() != null) { - ctPr.addNewTcW().setW(ctPr2.getTcW().getW()); - } - if (ctPr2.getVAlign() != null) { - ctPr.addNewVAlign().setVal(ctPr2.getVAlign().getVal()); - } - if (cttc2.getPList().size() > 0) { - CTP ctp = cttc2.getPList().get(0); - if (ctp.getPPr() != null) { - if (ctp.getPPr().getJc() != null) { - cttc.getPList().get(0).addNewPPr().addNewJc() - .setVal(ctp.getPPr().getJc().getVal()); - } - } - } - if (ctPr2.getTcBorders() != null) { - ctPr.setTcBorders(ctPr2.getTcBorders()); - } - XWPFParagraph tmpP = tmpCell.getParagraphs().get(0); - XWPFParagraph cellP = cell.getParagraphs().get(0); - XWPFRun tmpR = null; - if (tmpP.getRuns() != null && tmpP.getRuns().size() > 0) { - tmpR = tmpP.getRuns().get(0); - } - XWPFRun cellR = cellP.createRun(); - cellR.setText(text); - // 复制字体信息 - if (tmpR != null) { - if(!cellR.isBold()){ - cellR.setBold(tmpR.isBold()); - } - cellR.setItalic(tmpR.isItalic()); - cellR.setUnderline(tmpR.getUnderline()); - cellR.setColor(tmpR.getColor()); - cellR.setTextPosition(tmpR.getTextPosition()); - if (tmpR.getFontSize() != -1) { - cellR.setFontSize(tmpR.getFontSize()); - } - if (tmpR.getFontFamily() != null) { - cellR.setFontFamily(tmpR.getFontFamily()); - } - if (tmpR.getCTR() != null) { - if (tmpR.getCTR().isSetRPr()) { - CTRPr tmpRPr = tmpR.getCTR().getRPr(); - if (tmpRPr.isSetRFonts()) { - CTFonts tmpFonts = tmpRPr.getRFonts(); - CTRPr cellRPr = cellR.getCTR().isSetRPr() ? cellR - .getCTR().getRPr() : cellR.getCTR().addNewRPr(); - CTFonts cellFonts = cellRPr.isSetRFonts() ? cellRPr - .getRFonts() : cellRPr.addNewRFonts(); - cellFonts.setAscii(tmpFonts.getAscii()); - cellFonts.setAsciiTheme(tmpFonts.getAsciiTheme()); - cellFonts.setCs(tmpFonts.getCs()); - cellFonts.setCstheme(tmpFonts.getCstheme()); - cellFonts.setEastAsia(tmpFonts.getEastAsia()); - cellFonts.setEastAsiaTheme(tmpFonts.getEastAsiaTheme()); - cellFonts.setHAnsi(tmpFonts.getHAnsi()); - cellFonts.setHAnsiTheme(tmpFonts.getHAnsiTheme()); - } - } - } - - } - // 复制段落信息 - cellP.setAlignment(tmpP.getAlignment()); - cellP.setVerticalAlignment(tmpP.getVerticalAlignment()); - cellP.setBorderBetween(tmpP.getBorderBetween()); - cellP.setBorderBottom(tmpP.getBorderBottom()); - cellP.setBorderLeft(tmpP.getBorderLeft()); - cellP.setBorderRight(tmpP.getBorderRight()); - cellP.setBorderTop(tmpP.getBorderTop()); - cellP.setPageBreak(tmpP.isPageBreak()); - if (tmpP.getCTP() != null) { - if (tmpP.getCTP().getPPr() != null) { - CTPPr tmpPPr = tmpP.getCTP().getPPr(); - CTPPr cellPPr = cellP.getCTP().getPPr() != null ? cellP - .getCTP().getPPr() : cellP.getCTP().addNewPPr(); - // 复制段落间距信息 - CTSpacing tmpSpacing = tmpPPr.getSpacing(); - if (tmpSpacing != null) { - CTSpacing cellSpacing = cellPPr.getSpacing() != null ? cellPPr - .getSpacing() : cellPPr.addNewSpacing(); - if (tmpSpacing.getAfter() != null) { - cellSpacing.setAfter(tmpSpacing.getAfter()); - } - if (tmpSpacing.getAfterAutospacing() != null) { - cellSpacing.setAfterAutospacing(tmpSpacing - .getAfterAutospacing()); - } - if (tmpSpacing.getAfterLines() != null) { - cellSpacing.setAfterLines(tmpSpacing.getAfterLines()); - } - if (tmpSpacing.getBefore() != null) { - cellSpacing.setBefore(tmpSpacing.getBefore()); - } - if (tmpSpacing.getBeforeAutospacing() != null) { - cellSpacing.setBeforeAutospacing(tmpSpacing - .getBeforeAutospacing()); - } - if (tmpSpacing.getBeforeLines() != null) { - cellSpacing.setBeforeLines(tmpSpacing.getBeforeLines()); - } - if (tmpSpacing.getLine() != null) { - cellSpacing.setLine(tmpSpacing.getLine()); - } - if (tmpSpacing.getLineRule() != null) { - cellSpacing.setLineRule(tmpSpacing.getLineRule()); - } - } - // 复制段落缩进信息 - CTInd tmpInd = tmpPPr.getInd(); - if (tmpInd != null) { - CTInd cellInd = cellPPr.getInd() != null ? cellPPr.getInd() - : cellPPr.addNewInd(); - if (tmpInd.getFirstLine() != null) { - cellInd.setFirstLine(tmpInd.getFirstLine()); - } - if (tmpInd.getFirstLineChars() != null) { - cellInd.setFirstLineChars(tmpInd.getFirstLineChars()); - } - if (tmpInd.getHanging() != null) { - cellInd.setHanging(tmpInd.getHanging()); - } - if (tmpInd.getHangingChars() != null) { - cellInd.setHangingChars(tmpInd.getHangingChars()); - } - if (tmpInd.getLeft() != null) { - cellInd.setLeft(tmpInd.getLeft()); - } - if (tmpInd.getLeftChars() != null) { - cellInd.setLeftChars(tmpInd.getLeftChars()); - } - if (tmpInd.getRight() != null) { - cellInd.setRight(tmpInd.getRight()); - } - if (tmpInd.getRightChars() != null) { - cellInd.setRightChars(tmpInd.getRightChars()); - } - } - } - } - } } \ No newline at end of file