| | |
| | | package com.lf.server.helper; |
| | | |
| | | import org.apache.commons.logging.Log; |
| | | import org.apache.commons.logging.LogFactory; |
| | | import org.apache.poi.xwpf.usermodel.*; |
| | | |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.io.OutputStream; |
| | | import java.io.*; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | |
| | | * @author WWW |
| | | */ |
| | | public class WordHelper { |
| | | private final static Log log = LogFactory.getLog(WordHelper.class); |
| | | |
| | | /** |
| | | * 通过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); |
| | | public static void generateWord(String inputFile, String outPutFile, Map<String, String> insertTextMap, List<String[]> addList) { |
| | | try { |
| | | FileInputStream inputStream = new FileInputStream(inputFile); |
| | | FileOutputStream outputStream = new FileOutputStream(outPutFile); |
| | | |
| | | // 处理所有文段数据,除了表格 |
| | | handleParagraphs(xwpfDocument, insertTextMap); |
| | | // 获取docx解析对象 |
| | | XWPFDocument xwpfDocument = new XWPFDocument(inputStream); |
| | | |
| | | // 处理表格数据 |
| | | handleTable(xwpfDocument, insertTextMap, addList); |
| | | // 处理所有文段数据,除了表格 |
| | | if (null != insertTextMap && insertTextMap.size() > 0) { |
| | | handleParagraphs(xwpfDocument, insertTextMap); |
| | | } |
| | | |
| | | // 写入数据 |
| | | xwpfDocument.write(outputStream); |
| | | // 处理表格数据 |
| | | handleTable(xwpfDocument, insertTextMap, addList); |
| | | |
| | | outputStream.close(); |
| | | // 写入数据 |
| | | xwpfDocument.write(outputStream); |
| | | |
| | | outputStream.close(); |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | /** |
| | | * 处理表格数据方法 |
| | | */ |
| | | public static void handleTable(XWPFDocument xwpfDocument, Map<String, String> insertTextMap, List<String[]> addList) { |
| | | public static void handleTable(XWPFDocument xwpfDocument, Map<String, String> map, List<String[]> addList) { |
| | | List<XWPFTable> tables = xwpfDocument.getTables(); |
| | | for (XWPFTable table : tables) { |
| | | List<XWPFTableRow> rows = table.getRows(); |
| | |
| | | } |
| | | |
| | | 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); |
| | | } |
| | | } |
| | | if (null == map || map.isEmpty()) { |
| | | continue; |
| | | } |
| | | |
| | | replaceData(rows, map); |
| | | } else { |
| | | insertData(table, addList); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 替换数据 |
| | | */ |
| | | private static void replaceData(List<XWPFTableRow> rows, Map<String, String> map) { |
| | | 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(), map), 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]); |
| | | } |
| | | } |
| | | /** |
| | | * 插入数据 |
| | | */ |
| | | private static void insertData(XWPFTable table,List<String[]> addList) { |
| | | 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]); |
| | | } |
| | | } |
| | | } |