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 6543e8dd0..e72acfd82 100644 --- a/mallinkService/src/main/java/com/iformall/service/impl/WxBillAllServiceImpl.java +++ b/mallinkService/src/main/java/com/iformall/service/impl/WxBillAllServiceImpl.java @@ -844,7 +844,7 @@ public class WxBillAllServiceImpl implements WxBillAllService { } wordDocTableVo.setParams(rowsParams); wordDocTableVo.setTableIndex(0); - wordDocTableVo.setRowIndex(1); + wordDocTableVo.setRowIndex(2); String templatePath = "contract-word-template/settle.docx"; String filepath = fmUploadDir; diff --git a/mallinkService/src/main/java/com/iformall/utils/WordUtil.java b/mallinkService/src/main/java/com/iformall/utils/WordUtil.java index 6d5b447b3..d253a8ca6 100644 --- a/mallinkService/src/main/java/com/iformall/utils/WordUtil.java +++ b/mallinkService/src/main/java/com/iformall/utils/WordUtil.java @@ -99,59 +99,61 @@ public class WordUtil { public static void insertValueToTable(XWPFDocument xwpfDocument, WordDocTableVo wordDocTableVo) throws Exception { List tableList = xwpfDocument.getTables(); XWPFTable table = tableList.get(wordDocTableVo.getTableIndex()); - List rows = table.getRows(); - int rowIndex = wordDocTableVo.getRowIndex(); - XWPFTableRow tmpRow = rows.get(rowIndex); + addOrRemoveRow(table,wordDocTableVo.getParams().size(),wordDocTableVo.getRowIndex()); + int row = wordDocTableVo.getRowIndex(); for (List stringList : wordDocTableVo.getParams()) { - table.addRow(tmpRow,rowIndex); - rowIndex++; - //替换新行内容 - XWPFTableRow tableRow = rows.get(rowIndex); + row++; for (int i = 0; i < stringList.size(); i++) { - tableRow.getTableCells().get(i).setText(stringList.get(i)); + table.getRow(row).getCell(i).setText(stringList.get(i)); } } - tmpRow.getTableCells().get(0).setText("aaaa"); - -// //模板的那一行 -// XWPFTableRow tmpRow = rows.get(2); -// List tmpCells = null; -// List cells2 = null; -// XWPFTableCell tmpCell = null; -// tmpCells = tmpRow.getTableCells(); -// -// String cellText = null; -// String cellTextKey = null; -// Map totalMap = null; -// for (int i = 0, len = params.size(); i < len; i++) { -// Map map = params.get(i); -// // 创建新的一行 -// XWPFTableRow row = table.createRow(); -// // 获取模板的行高 设置为新一行的行高 -// row.setHeight(tmpRow.getHeight()); -// cells = row.a(); -// for (int k = 0, klen = tmpCells.size(); k < klen; k++) { -// tmpCell = tmpCells.get(k); -// XWPFTableCell cell = cells.get(k); -// cellText = tmpCell.getText(); -// if (StringUtils.isNotBlank(cellText)) { -// //转换为mapkey对应的字段 -// cellTextKey = cellText.replace("$", "").replace("{", "").replace("}", ""); -// if (map.containsKey(cellTextKey)) { -// // 填充内容 并且复制模板行的属性 -// setCellText(tmpCell,cell,map.get(cellTextKey)); -// } -// } -// } -// -// } -// // 删除模版行 -// table.removeRow(2); + table.removeRow(wordDocTableVo.getRowIndex()); } + /* + * @param table 对应表格 + * @param add 增加或删除行数 if add>0 增加行 add<0 删除行 + * @param fromRow 添加开始行位置(fromRow-1是模版行) + */ + public static void addOrRemoveRow(XWPFTable table, int add,int fromRow) { + XWPFTableRow row = table.getRow(fromRow-1); + if(add>0) + { + while(add>0) + { + copyPro(row,table.insertNewTableRow(fromRow)); + add--; + } + } + else + { + while(add<0) + { + table.removeRow(fromRow-1); + add++; + } + } + } - - + public static void copyPro(XWPFTableRow sourceRow,XWPFTableRow targetRow) { + //复制行属性 + targetRow.getCtRow().setTrPr(sourceRow.getCtRow().getTrPr()); + List cellList = sourceRow.getTableCells(); + if(null==cellList) + { + return ; + } + //添加列、复制列以及列中段落属性 + XWPFTableCell targetCell = null; + for(XWPFTableCell sourceCell:cellList) + { + targetCell = targetRow.addNewTableCell(); + //列属性 + targetCell.getCTTc().setTcPr(sourceCell.getCTTc().getTcPr()); + //段落属性 + targetCell.getParagraphs().get(0).getCTP().setPPr(sourceCell.getParagraphs().get(0).getCTP().getPPr()); + } + } } \ No newline at end of file diff --git a/mallinkService/src/main/resources/contract-word-template/settle.docx b/mallinkService/src/main/resources/contract-word-template/settle.docx index b5e4b9e32..6739c66a2 100644 Binary files a/mallinkService/src/main/resources/contract-word-template/settle.docx and b/mallinkService/src/main/resources/contract-word-template/settle.docx differ