| | |
| | | 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; |
| | |
| | | inputStream = new FileInputStream(inputFile); |
| | | outputStream = new FileOutputStream(outPutFile); |
| | | |
| | | // 获取docx解析对象 |
| | | 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); |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 处理所有文段数据,除了表格 |
| | | */ |
| | | 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> map, List<String[]> addList) { |
| | | List<XWPFTable> tables = xwpfDocument.getTables(); |
| | | for (XWPFTable table : tables) { |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 替换数据 |
| | | */ |
| | | private static void replaceData(List<XWPFTableRow> rows, Map<String, String> map) { |
| | | for (XWPFTableRow row : rows) { |
| | | List<XWPFTableCell> tableCells = row.getTableCells(); |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 插入数据 |
| | | */ |
| | | private static void insertData(XWPFTable table, List<String[]> addList) { |
| | | for (int i = 1, c = addList.size(); i < c; i++) { |
| | | table.createRow(); |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 设置单元格文本 |
| | | */ |
| | | private static void setCellText(XWPFTableCell cell, String text) { |
| | | List<XWPFParagraph> paragraphs = cell.getParagraphs(); |
| | | for (XWPFParagraph paragraph : paragraphs) { |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 有${}的值匹配出替换的数据,没有${}就返回原来的数据 |
| | | * |
| | | * @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 + "}"; |
| | |
| | | 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(); |
| | |
| | | XWPFRun cellR = cellP.createRun(); |
| | | cellR.setText(text); |
| | | |
| | | // 复制字体信息 |
| | | copyFontInfo(cellR, tmpR); |
| | | |
| | | // 复制段落信息 |
| | | cellP.setAlignment(tmpP.getAlignment()); |
| | | cellP.setVerticalAlignment(tmpP.getVerticalAlignment()); |
| | | cellP.setBorderBetween(tmpP.getBorderBetween()); |
| | |
| | | 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; |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 复制段落间距信息 |
| | | */ |
| | | private static void copySpacing(CTPPr tmpPpr, CTPPr cellPpr) { |
| | | CTSpacing tmpSpacing = tmpPpr.getSpacing(); |
| | | if (tmpSpacing != null) { |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 复制段落缩进信息 |
| | | */ |
| | | private static void copyParagraph(CTPPr tmpPpr, CTPPr cellPpr) { |
| | | CTInd tmpInd = tmpPpr.getInd(); |
| | | if (tmpInd != null) { |