¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.lf.server.helper; |
| | | |
| | | import org.apache.poi.xwpf.usermodel.*; |
| | | |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.io.OutputStream; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * Word帮å©ç±» |
| | | * @author WWW |
| | | */ |
| | | public class WordHelper { |
| | | /** |
| | | * éè¿word模æ¿çæwordçä¸»æ¹æ³ |
| | | */ |
| | | public static void generateWord(InputStream inputStream, OutputStream outputStream, Map<String, String> insertTextMap, List<String[]> addList) throws IOException { |
| | | // è·ådocxè§£æå¯¹è±¡ |
| | | XWPFDocument xwpfDocument = new XWPFDocument(inputStream); |
| | | |
| | | // å¤çææææ®µæ°æ®ï¼é¤äºè¡¨æ ¼ |
| | | handleParagraphs(xwpfDocument, insertTextMap); |
| | | |
| | | // å¤çè¡¨æ ¼æ°æ® |
| | | handleTable(xwpfDocument, insertTextMap, addList); |
| | | |
| | | // åå
¥æ°æ® |
| | | xwpfDocument.write(outputStream); |
| | | |
| | | outputStream.close(); |
| | | } |
| | | |
| | | /** |
| | | * å¤çææææ®µæ°æ®ï¼é¤äºè¡¨æ ¼ |
| | | */ |
| | | 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()) { |
| | | // 夿另æ ${} çrun |
| | | run.setText(matchesValue(run.text(), insertTextMap), 0); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å¤çè¡¨æ ¼æ°æ®æ¹æ³ |
| | | */ |
| | | public static void handleTable(XWPFDocument xwpfDocument, Map<String, String> insertTextMap, List<String[]> addList) { |
| | | List<XWPFTable> tables = xwpfDocument.getTables(); |
| | | for (XWPFTable table : tables) { |
| | | List<XWPFTableRow> rows = table.getRows(); |
| | | if (rows.size() < 2) { |
| | | continue; |
| | | } |
| | | |
| | | if (isReplacement(table.getText())) { |
| | | // æ¿æ¢æ°æ® |
| | | for (XWPFTableRow row : rows) { |
| | | List<XWPFTableCell> tableCells = row.getTableCells(); |
| | | for (XWPFTableCell tableCell : tableCells) { |
| | | if (isReplacement(tableCell.getText())) { |
| | | List<XWPFParagraph> paragraphs = tableCell.getParagraphs(); |
| | | for (XWPFParagraph paragraph : paragraphs) { |
| | | List<XWPFRun> runs = paragraph.getRuns(); |
| | | for (XWPFRun run : runs) { |
| | | run.setText(matchesValue(tableCell.getText(), insertTextMap), 0); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } else { |
| | | // æå
¥æ°æ® |
| | | for (int i = 1; i < addList.size(); i++) { |
| | | XWPFTableRow row = table.createRow(); |
| | | } |
| | | |
| | | List<XWPFTableRow> rowList = table.getRows(); |
| | | for (int i = 1; i < rowList.size(); i++) { |
| | | XWPFTableRow xwpfTableRow = rowList.get(i); |
| | | 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]); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * æ${}çå¼å¹é
åºæ¿æ¢çæ°æ®ï¼æ²¡æ${}å°±è¿ååæ¥çæ°æ® |
| | | * |
| | | * @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 + "}"; |
| | | if (s1.equals(wordValue)) { |
| | | wordValue = map.get(s); |
| | | } |
| | | } |
| | | |
| | | return wordValue; |
| | | } |
| | | |
| | | /** |
| | | * æµè¯æ¯å¦å
å«éè¦æ¿æ¢çæ°æ® |
| | | */ |
| | | public static boolean isReplacement(String text) { |
| | | return text.contains("$"); |
| | | } |
| | | } |