WordUtil.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package jnpf.util;
  2. import lombok.Cleanup;
  3. import org.apache.poi.ooxml.POIXMLDocument;
  4. import org.apache.poi.xwpf.usermodel.*;
  5. import java.io.File;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.util.List;
  9. import java.util.Map;
  10. import java.util.Set;
  11. /**
  12. *
  13. * @author JNPF开发平台组
  14. * @version V3.1.0
  15. * @copyright 引迈信息技术有限公司
  16. * @date 2021/3/16 10:57
  17. */
  18. public class WordUtil {
  19. /**
  20. * 根据模板生成新word文档
  21. * 判断表格是需要替换还是需要插入,判断逻辑有$为替换,表格无$为插入
  22. * @param inputUrl 模板存放地址
  23. * @param outputUrl 新文档存放地址
  24. * @param textMap 需要替换的信息集合
  25. * @param tableList 需要插入的表格信息集合
  26. * @return 成功返回true,失败返回false
  27. */
  28. public static void changWord(String inputUrl, String outputUrl, Map<String, String> textMap, List<String[]> tableList) {
  29. boolean changeFlag = true;
  30. try {
  31. XWPFDocument document = new XWPFDocument(POIXMLDocument.openPackage(inputUrl));
  32. changeText(document, textMap);
  33. changeTable(document, textMap, tableList);
  34. File file = new File(jnpf.util.XSSEscape.escapePath(outputUrl));
  35. @Cleanup FileOutputStream stream = new FileOutputStream(file);
  36. document.write(stream);
  37. stream.close();
  38. } catch (IOException e) {
  39. e.printStackTrace();
  40. }
  41. }
  42. /**
  43. * 替换段落文本
  44. * @param document docx解析对象
  45. * @param textMap 需要替换的信息集合
  46. */
  47. public static void changeText(XWPFDocument document, Map<String, String> textMap){
  48. List<XWPFParagraph> paragraphs = document.getParagraphs();
  49. for (XWPFParagraph paragraph : paragraphs) {
  50. String text = paragraph.getText();
  51. if(checkText(text)){
  52. List<XWPFRun> runs = paragraph.getRuns();
  53. for (XWPFRun run : runs) {
  54. run.setText(changeValue(run.toString(), textMap),0);
  55. }
  56. }
  57. }
  58. }
  59. /**
  60. * 替换表格对象方法
  61. * @param document docx解析对象
  62. * @param textMap 需要替换的信息集合
  63. * @param tableList 需要插入的表格信息集合
  64. */
  65. public static void changeTable(XWPFDocument document, Map<String, String> textMap, List<String[]> tableList){
  66. List<XWPFTable> tables = document.getTables();
  67. for (int i = 0; i < tables.size(); i++) {
  68. XWPFTable table = tables.get(i);
  69. if(table.getRows().size()>1){
  70. if(checkText(table.getText())){
  71. List<XWPFTableRow> rows = table.getRows();
  72. eachTable(rows, textMap);
  73. }else{
  74. insertTable(table, tableList);
  75. }
  76. }
  77. }
  78. }
  79. /**
  80. * 遍历表格
  81. * @param rows 表格行对象
  82. * @param textMap 需要替换的信息集合
  83. */
  84. public static void eachTable(List<XWPFTableRow> rows , Map<String, String> textMap){
  85. for (XWPFTableRow row : rows) {
  86. List<XWPFTableCell> cells = row.getTableCells();
  87. for (XWPFTableCell cell : cells) {
  88. if(checkText(cell.getText())){
  89. List<XWPFParagraph> paragraphs = cell.getParagraphs();
  90. for (XWPFParagraph paragraph : paragraphs) {
  91. List<XWPFRun> runs = paragraph.getRuns();
  92. for (XWPFRun run : runs) {
  93. run.setText(changeValue(run.toString(), textMap),0);
  94. }
  95. }
  96. }
  97. }
  98. }
  99. }
  100. /**
  101. * 为表格插入数据,行数不够添加新行
  102. * @param table 需要插入数据的表格
  103. * @param tableList 插入数据集合
  104. */
  105. public static void insertTable(XWPFTable table, List<String[]> tableList){
  106. for(int i = 1; i < tableList.size(); i++){
  107. XWPFTableRow row =table.createRow();
  108. }
  109. List<XWPFTableRow> rows = table.getRows();
  110. for(int i = 1; i < rows.size(); i++){
  111. XWPFTableRow newRow = table.getRow(i);
  112. List<XWPFTableCell> cells = newRow.getTableCells();
  113. for(int j = 0; j < cells.size(); j++){
  114. XWPFTableCell cell = cells.get(j);
  115. cell.setText(tableList.get(i-1)[j]);
  116. }
  117. }
  118. }
  119. /**
  120. * 判断文本中时候包含$
  121. * @param text 文本
  122. * @return 包含返回true,不包含返回false
  123. */
  124. public static boolean checkText(String text){
  125. boolean check = false;
  126. if(text.indexOf("$")!= -1){
  127. check = true;
  128. }
  129. return check;
  130. }
  131. /**
  132. * 匹配传入信息集合与模板
  133. * @param value 模板需要替换的区域
  134. * @param textMap 传入信息集合
  135. * @return 模板需要替换区域信息集合对应值
  136. */
  137. public static String changeValue(String value, Map<String, String> textMap){
  138. Set<Map.Entry<String, String>> textSets = textMap.entrySet();
  139. for (Map.Entry<String, String> textSet : textSets) {
  140. String key = "${"+textSet.getKey()+"}";
  141. if(value.indexOf(key)!= -1){
  142. value = textSet.getValue();
  143. }
  144. }
  145. if(checkText(value)){
  146. value = "";
  147. }
  148. return value;
  149. }
  150. }