| | |
| | | * 通过word模板生成word的主方法 |
| | | */ |
| | | public static void generateWord(String inputFile, String outPutFile, Map<String, String> insertTextMap, List<String[]> addList) { |
| | | FileInputStream inputStream = null; |
| | | FileOutputStream outputStream = null; |
| | | |
| | | try { |
| | | FileInputStream inputStream = new FileInputStream(inputFile); |
| | | FileOutputStream outputStream = new FileOutputStream(outPutFile); |
| | | inputStream = new FileInputStream(inputFile); |
| | | outputStream = new FileOutputStream(outPutFile); |
| | | |
| | | // 获取docx解析对象 |
| | | XWPFDocument xwpfDocument = new XWPFDocument(inputStream); |
| | |
| | | |
| | | // 写入数据 |
| | | xwpfDocument.write(outputStream); |
| | | |
| | | outputStream.close(); |
| | | inputStream.close(); |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | } finally { |
| | | try { |
| | | if (outputStream != null) { |
| | | outputStream.close(); |
| | | } |
| | | if (inputStream != null) { |
| | | inputStream.close(); |
| | | } |
| | | } catch (Exception e) { |
| | | log.error(e.getMessage(), e); |
| | | } |
| | | } |
| | | } |
| | | |
| | |
| | | * 插入数据 |
| | | */ |
| | | private static void insertData(XWPFTable table, List<String[]> addList) { |
| | | XWPFTableRow oldRow = table.getRow(1); |
| | | for (int i = 1; i < addList.size(); i++) { |
| | | for (int i = 1, c = addList.size(); i < c; i++) { |
| | | XWPFTableRow row = table.createRow(); |
| | | row.setHeight(oldRow.getHeight()); |
| | | } |
| | | |
| | | List<XWPFTableCell> oldCells = table.getRow(1).getTableCells(); |
| | | List<XWPFTableRow> rowList = table.getRows(); |
| | | for (int i = 1; i < rowList.size(); i++) { |
| | | XWPFTableRow xwpfTableRow = rowList.get(i); |
| | | for (int i = 0, c = addList.size(); i < c; i++) { |
| | | XWPFTableRow xwpfTableRow = rowList.get(i + 1); |
| | | |
| | | List<XWPFTableCell> tableCells = xwpfTableRow.getTableCells(); |
| | | for (int j = 0; j < tableCells.size(); j++) { |
| | | XWPFTableCell oldCell = oldRow.getTableCells().get(j); |
| | | XWPFTableCell oldCell = oldCells.get(j); |
| | | XWPFTableCell newCell = tableCells.get(j); |
| | | |
| | | //newCell.setText(addList.get(i - 1)[j]); |
| | | setCellText(oldCell, newCell, addList.get(i - 1)[j]); |
| | | if (0 == i) { |
| | | newCell.setText(addList.get(i)[j]); |
| | | } else { |
| | | setCellText(oldCell, newCell, addList.get(i)[j]); |
| | | } |
| | | } |
| | | } |
| | | } |
| | |
| | | } |
| | | 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 |
| | | 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(); |
| | | CTFonts cellFonts = cellRpr.isSetRFonts() ? cellRpr |
| | | .getRFonts() : cellRpr.addNewRFonts(); |
| | | cellFonts.setAscii(tmpFonts.getAscii()); |
| | | cellFonts.setAsciiTheme(tmpFonts.getAsciiTheme()); |
| | | cellFonts.setCs(tmpFonts.getCs()); |
| | |
| | | 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(); |
| | | CTPPr tmpPpr = tmpP.getCTP().getPPr(); |
| | | CTPPr cellPpr = cellP.getCTP().getPPr() != null ? cellP.getCTP().getPPr() : cellP.getCTP().addNewPPr(); |
| | | |
| | | // 复制段落间距信息 |
| | | CTSpacing tmpSpacing = tmpPPr.getSpacing(); |
| | | CTSpacing tmpSpacing = tmpPpr.getSpacing(); |
| | | if (tmpSpacing != null) { |
| | | CTSpacing cellSpacing = cellPPr.getSpacing() != null ? cellPPr |
| | | .getSpacing() : cellPPr.addNewSpacing(); |
| | | CTSpacing cellSpacing = cellPpr.getSpacing() != null ? cellPpr |
| | | .getSpacing() : cellPpr.addNewSpacing(); |
| | | if (tmpSpacing.getAfter() != null) { |
| | | cellSpacing.setAfter(tmpSpacing.getAfter()); |
| | | } |
| | |
| | | } |
| | | |
| | | // 复制段落缩进信息 |
| | | CTInd tmpInd = tmpPPr.getInd(); |
| | | CTInd tmpInd = tmpPpr.getInd(); |
| | | if (tmpInd != null) { |
| | | CTInd cellInd = cellPPr.getInd() != null ? cellPPr.getInd() |
| | | : cellPPr.addNewInd(); |
| | | CTInd cellInd = cellPpr.getInd() != null ? cellPpr.getInd() |
| | | : cellPpr.addNewInd(); |
| | | if (tmpInd.getFirstLine() != null) { |
| | | cellInd.setFirstLine(tmpInd.getFirstLine()); |
| | | } |