From 796b44ea813a1133beae4f3a67f1c0263510c0c7 Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期日, 17 十一月 2024 09:45:07 +0800 Subject: [PATCH] 1 --- src/main/java/com/moon/server/helper/WordHelper.java | 55 +------------------------------------------------------ 1 files changed, 1 insertions(+), 54 deletions(-) diff --git a/src/main/java/com/moon/server/helper/WordHelper.java b/src/main/java/com/moon/server/helper/WordHelper.java index fc1d69a..03c7469 100644 --- a/src/main/java/com/moon/server/helper/WordHelper.java +++ b/src/main/java/com/moon/server/helper/WordHelper.java @@ -9,16 +9,10 @@ import java.util.List; import java.util.Map; -/** - * Word甯姪绫� - * @author WWW - */ +@SuppressWarnings("ALL") public class WordHelper { private final static Log log = LogFactory.getLog(WordHelper.class); - /** - * 閫氳繃word妯℃澘鐢熸垚word鐨勪富鏂规硶 - */ public static void generateWord(String inputFile, String outPutFile, Map<String, String> insertTextMap, List<String[]> addList) { FileInputStream inputStream = null; FileOutputStream outputStream = null; @@ -27,18 +21,14 @@ inputStream = new FileInputStream(inputFile); outputStream = new FileOutputStream(outPutFile); - // 鑾峰彇docx瑙f瀽瀵硅薄 XWPFDocument xwpfDocument = new XWPFDocument(inputStream); - // 澶勭悊鎵�鏈夋枃娈垫暟鎹紝闄や簡琛ㄦ牸 if (null != insertTextMap && insertTextMap.size() > 0) { handleParagraphs(xwpfDocument, insertTextMap); } - // 澶勭悊琛ㄦ牸鏁版嵁 handleTable(xwpfDocument, insertTextMap, addList); - // 鍐欏叆鏁版嵁 xwpfDocument.write(outputStream); } catch (Exception ex) { log.error(ex.getMessage(), ex); @@ -56,24 +46,17 @@ } } - /** - * 澶勭悊鎵�鏈夋枃娈垫暟鎹紝闄や簡琛ㄦ牸 - */ public static void handleParagraphs(XWPFDocument xwpfDocument, Map<String, String> insertTextMap) { for (XWPFParagraph paragraph : xwpfDocument.getParagraphs()) { String text = paragraph.getText(); if (isReplacement(text)) { for (XWPFRun run : paragraph.getRuns()) { - // 鍒ゆ柇甯︽湁 ${} 鐨剅un run.setText(matchesValue(run.text(), insertTextMap), 0); } } } } - /** - * 澶勭悊琛ㄦ牸鏁版嵁鏂规硶 - */ public static void handleTable(XWPFDocument xwpfDocument, Map<String, String> map, List<String[]> addList) { List<XWPFTable> tables = xwpfDocument.getTables(); for (XWPFTable table : tables) { @@ -94,9 +77,6 @@ } } - /** - * 鏇挎崲鏁版嵁 - */ private static void replaceData(List<XWPFTableRow> rows, Map<String, String> map) { for (XWPFTableRow row : rows) { List<XWPFTableCell> tableCells = row.getTableCells(); @@ -114,9 +94,6 @@ } } - /** - * 鎻掑叆鏁版嵁 - */ private static void insertData(XWPFTable table, List<String[]> addList) { for (int i = 1, c = addList.size(); i < c; i++) { table.createRow(); @@ -142,9 +119,6 @@ } } - /** - * 璁剧疆鍗曞厓鏍兼枃鏈� - */ private static void setCellText(XWPFTableCell cell, String text) { List<XWPFParagraph> paragraphs = cell.getParagraphs(); for (XWPFParagraph paragraph : paragraphs) { @@ -155,13 +129,6 @@ } } - /** - * 鏈�${}鐨勫�煎尮閰嶅嚭鏇挎崲鐨勬暟鎹紝娌℃湁${}灏辫繑鍥炲師鏉ョ殑鏁版嵁 - * - * @param wordValue ${...} 甯�${}鐨勫彉閲� - * @param map 瀛樺偍闇�瑕佹浛鎹㈢殑鏁版嵁 - * @return java.lang.String - */ public static String matchesValue(String wordValue, Map<String, String> map) { for (String s : map.keySet()) { String s1 = "${" + s + "}"; @@ -173,16 +140,10 @@ return wordValue; } - /** - * 娴嬭瘯鏄惁鍖呭惈闇�瑕佹浛鎹㈢殑鏁版嵁 - */ 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(); @@ -207,10 +168,8 @@ XWPFRun cellR = cellP.createRun(); cellR.setText(text); - // 澶嶅埗瀛椾綋淇℃伅 copyFontInfo(cellR, tmpR); - // 澶嶅埗娈佃惤淇℃伅 cellP.setAlignment(tmpP.getAlignment()); cellP.setVerticalAlignment(tmpP.getVerticalAlignment()); cellP.setBorderBetween(tmpP.getBorderBetween()); @@ -224,17 +183,11 @@ 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; @@ -274,9 +227,6 @@ } } - /** - * 澶嶅埗娈佃惤闂磋窛淇℃伅 - */ private static void copySpacing(CTPPr tmpPpr, CTPPr cellPpr) { CTSpacing tmpSpacing = tmpPpr.getSpacing(); if (tmpSpacing != null) { @@ -310,9 +260,6 @@ } } - /** - * 澶嶅埗娈佃惤缂╄繘淇℃伅 - */ private static void copyParagraph(CTPPr tmpPpr, CTPPr cellPpr) { CTInd tmpInd = tmpPpr.getInd(); if (tmpInd != null) { -- Gitblit v1.9.3