From bf9f5d506d34cbbcc68f77cdf71e30a6cf3cd7d4 Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期三, 09 八月 2023 13:50:07 +0800 Subject: [PATCH] 1 --- src/main/java/com/lf/server/helper/WordHelper.java | 221 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 210 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/lf/server/helper/WordHelper.java b/src/main/java/com/lf/server/helper/WordHelper.java index ff87b12..5704e07 100644 --- a/src/main/java/com/lf/server/helper/WordHelper.java +++ b/src/main/java/com/lf/server/helper/WordHelper.java @@ -3,6 +3,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.poi.xwpf.usermodel.*; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.*; import java.io.*; import java.util.List; @@ -19,9 +20,12 @@ * 閫氳繃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瑙f瀽瀵硅薄 XWPFDocument xwpfDocument = new XWPFDocument(inputStream); @@ -36,10 +40,19 @@ // 鍐欏叆鏁版嵁 xwpfDocument.write(outputStream); - - outputStream.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); + } } } @@ -104,18 +117,40 @@ /** * 鎻掑叆鏁版嵁 */ - private static void insertData(XWPFTable table,List<String[]> addList) { - for (int i = 1; i < addList.size(); i++) { - XWPFTableRow row = table.createRow(); + private static void insertData(XWPFTable table, List<String[]> addList) { + for (int i = 1, c = addList.size(); i < c; i++) { + table.createRow(); } + 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 xwpfTableCell = tableCells.get(j); - xwpfTableCell.setText(addList.get(i - 1)[j]); + XWPFTableCell oldCell = oldCells.get(j); + XWPFTableCell newCell = tableCells.get(j); + + if (0 == i) { + // newCell.setText(addList.get(i)[j]) + setCellText(newCell, addList.get(i)[j]); + } else { + setCellText(oldCell, newCell, addList.get(i)[j]); + } + } + } + } + + /** + * 璁剧疆鍗曞厓鏍兼枃鏈� + */ + private static void setCellText(XWPFTableCell cell, String text) { + List<XWPFParagraph> paragraphs = cell.getParagraphs(); + for (XWPFParagraph paragraph : paragraphs) { + List<XWPFRun> runs = paragraph.getRuns(); + for (XWPFRun run : runs) { + run.setText(text, 0); } } } @@ -144,4 +179,168 @@ public static boolean isReplacement(String text) { return text.contains("$"); } + + /** + * 澶嶅埗妯℃澘琛岀殑灞炴�� + */ + private static void setCellText(XWPFTableCell tmpCell, XWPFTableCell cell, String text) { + 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 (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); + + // 澶嶅埗瀛椾綋淇℃伅 + copyFontInfo(cellR, tmpR); + + // 澶嶅埗娈佃惤淇℃伅 + 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&&tmpP.getCTP().getPPr() != null) { + CTPPr tmpPpr = tmpP.getCTP().getPPr(); + CTPPr cellPpr = cellP.getCTP().getPPr() != null ? cellP.getCTP().getPPr() : cellP.getCTP().addNewPPr(); + + // 澶嶅埗娈佃惤闂磋窛淇℃伅 + copySpacing(tmpPpr, cellPpr); + + // 澶嶅埗娈佃惤缂╄繘淇℃伅 + copyParagraph(tmpPpr, cellPpr); + } + } + + /** + * 澶嶅埗瀛椾綋淇℃伅 + */ + private static void copyFontInfo(XWPFRun cellR, XWPFRun tmpR) { + if (tmpR == null) { + return; + } + 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()); + } + } + } + } + + /** + * 澶嶅埗娈佃惤闂磋窛淇℃伅 + */ + private static void copySpacing(CTPPr tmpPpr, CTPPr cellPpr) { + 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()); + } + } + } + + /** + * 澶嶅埗娈佃惤缂╄繘淇℃伅 + */ + private static void copyParagraph(CTPPr tmpPpr, CTPPr cellPpr) { + 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()); + } + } + } } -- Gitblit v1.9.3