sprinboot解析word报告的一天
记录日志,其中很多代码都是使用网上搜来的,自己修改适合自己的版本罢了,代码很垃圾,正在努力学习中,不喜也可喷
1.Springboot+poi 解析word报告
1.首先要引入一些主要的依赖,以下版本是我项目中用到的
springboot:<version>2.2.8.RELEASE</version>
<!-- word -->
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.4.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.17</version>
</dependency>
<!-- 用于处理当前的word文档2007的老版本 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.17</version>
</dependency>
2.贴一些代码
1.解析word中普通的段落,其中{{ }} 是word中需要替换的表示,跟网上常见的坐标不太一致,我只修改成了我需要的样子
//word 中的段落 public void readAndReplaceWordParagraphs(XWPFDocument document, Map<String, Object> map) { Iterator<XWPFParagraph> itPara = document.getParagraphsIterator(); Set<String> set = map.keySet(); while (itPara.hasNext()) { XWPFParagraph paragraph = itPara.next(); List<XWPFRun> run = paragraph.getRuns(); if (!CollectionUtils.isEmpty(run)) { for (String key : set) { for (int i = 0; i < run.size(); i++) { String text0 = run.get(i).getText(run.get(i).getTextPosition()); if (!StringUtils.isBlank(text0)) { //解析word 出现一个字符单独出现的情况 if (text0.contains("{{") && text0.contains("}}")) { if (Objects.equals(text0.replaceAll(" ", ""), key.replaceAll(" ", ""))) { run.get(i).setText(map.get(key).toString(), 0); } } //解析word 出现一个字符单独出现的情况 if (text0.trim().equals(key)) { run.get(i).setText((String) map.get(key), 0); } if (text0.trim().contains("{{") && !text0.trim().contains("}}")) { int startIndex = text0.lastIndexOf("{{"); int endIndex = 1; if (startIndex != -1) { endIndex = text0.substring(startIndex).indexOf("}}"); } if (endIndex < 0) { // 记录分隔符中间跨越的runs数量,用于字符串拼接和替换 int num = 0; int j = i + 1; for (; j < run.size(); j++) { String text1 = run.get(j).getText(run.get(j).getTextPosition()); if (text1 != null && text1.contains("}}")) { num = j - i; break; } } if (num != 0) { // num!=0说明找到了@@配对,需要替换 {{ {{ 配对替换 StringBuilder newText = new StringBuilder(); for (int s = i; s <= i + num; s++) { String text2 = run.get(s).getText(run.get(s).getTextPosition()); if (!StringUtils.isBlank(text2)) { String replaceText = text2; if (s == i && text2.contains("{{") && text2.contains("}}")) { newText.append(text2); } else if (s == i && text2.contains("{{")) { replaceText = text2.substring(0, text2.indexOf("{{")); newText.append(text2.substring(text2.indexOf("{{"))); } else if (text2.contains("}}")) { replaceText = text2.substring(replaceText.indexOf("}}") + 2); newText.append(text2, 0, text2.indexOf("}}") + 2); } else { replaceText = ""; newText.append(text2); } run.get(s).setText(replaceText, 0); } } //合并替换 run.get(i).setText(newText.toString(), 0); i = i - 1; } } } } } } } } } 2.解析word中的表格,表格中也存在段落,项目中的word模板较为复杂,所以有很多冗余的代码,我也想要知道如何优化。。。 //word 中的表格 如何提高效率 最耗时间 public void readAndReplaceWordTable(XWPFDocument document, Map<String, Object> map) { Iterator<XWPFTable> itTableCell = document.getTablesIterator(); while (itTableCell.hasNext()) { XWPFTable table = itTableCell.next(); int count = table.getNumberOfRows(); Set<String> set = map.keySet(); for (int i = 0; i < count; i++) { XWPFTableRow row = table.getRow(i); List<XWPFTableCell> cells = row.getTableCells(); //需要替换的坐标 key for (String key : set) { if (!CollectionUtils.isEmpty(cells)) { for (XWPFTableCell cell : cells) { // 包含 段落形式 计算容积率建筑面积={{ 3,P3 }}×{{ 3,G3 }}÷10000={{ 3,Q3 }}(万元) List<XWPFParagraph> cellParList = cell.getParagraphs(); if (!CollectionUtils.isEmpty(cellParList)) { for (XWPFParagraph xwpfParagraph : cellParList) { //每个格子循环 其中包含有段落 List<XWPFRun> run = xwpfParagraph.getRuns(); //每个格子的内容都要单独处理 if (!CollectionUtils.isEmpty(run)) { for (int q = 0; q < run.size(); q++) { String text0 = run.get(q).getText(run.get(q).getTextPosition()); if (!StringUtils.isBlank(text0)) { //解析word 出现一个字符单独出现的情况 if (text0.contains("{{") && text0.contains("}}")) { if (Objects.equals(text0.replaceAll(" ", ""), key.replaceAll(" ", ""))) { run.get(q).setText(map.get(key).toString(), 0); } } if (text0.trim().equals(key)) { run.get(q).setText(map.get(key).toString(), 0); } //出现换行的情况 if (text0.trim().contains("{{") && !text0.trim().contains("}}")) { int startIndex = text0.lastIndexOf("{{"); int endIndex = 1; if (startIndex != -1) { endIndex = text0.substring(startIndex).indexOf("}}"); } if (endIndex < 0) { // 记录分隔符中间跨越的runs数量,用于字符串拼接和替换 int num = 0; int j = q + 1; for (; j < run.size(); j++) { String text1 = run.get(j).getText(run.get(j).getTextPosition()); if (text1 != null && text1.contains("}}")) { num = j - q; break; } } if (num != 0) { // num!=0说明找到了 {{ {{ 配对替换 StringBuilder newText = new StringBuilder(); for (int s = q; s <= q + num; s++) { String text2 = run.get(s).getText(run.get(s).getTextPosition()); if (!StringUtils.isBlank(text2)) { String replaceText = text2; if (s == q && text2.contains("{{") && text2.contains("}}")) { newText.append(text2); } else if (s == q && text2.contains("{{")) { replaceText = text2.substring(0, text2.indexOf("{{")); newText.append(text2.substring(text2.indexOf("{{"))); } else if (text2.contains("}}")) { replaceText = text2.substring(replaceText.indexOf("}}") + 2); newText.append(text2, 0, text2.indexOf("}}") + 2); } else { replaceText = ""; newText.append(text2); } run.get(s).setText(replaceText, 0); } } //合并替换 run.get(q).setText(newText.toString(), 0); q = q - 1; } } } } } } } } } } //表格 end } } } }
3.解析word中的表格中嵌套表格,这个单单解析表格是解析不出来的,又是冗余代码,喷吧
//word 表格中的表格
public void readAndReplaceWordTableCell(XWPFDocument document, Map<String, Object> map) {
Iterator<XWPFTable> itTableCell = document.getTablesIterator();
while (itTableCell.hasNext()) {
XWPFTable table = itTableCell.next();
int count = table.getNumberOfRows();
Set<String> set = map.keySet();
for (String key : set) {
for (int i = 0; i < count; i++) {
XWPFTableRow row = table.getRow(i);
List<XWPFTableCell> cells = row.getTableCells();
//表格 start
if (!CollectionUtils.isEmpty(cells)) {
for (XWPFTableCell cell : cells) {
//表格嵌套表格
List<XWPFTable> xwpfTableList = cell.getTables();
if (!CollectionUtils.isEmpty(xwpfTableList)) {
for (XWPFTable xwpfTable : xwpfTableList) {
int numberOfRows = xwpfTable.getNumberOfRows();
for (int i1 = 0; i1 < numberOfRows; i1++) {
XWPFTableRow row1 = xwpfTable.getRow(i1);
List<XWPFTableCell> cells1 = row1.getTableCells();
for (XWPFTableCell cell1 : cells1) {
// 处理成段落的形式
List<XWPFParagraph> cellParList = cell1.getParagraphs();
if (CollectionUtils.isEmpty(cellParList)) {
continue;
}
for (XWPFParagraph xwpfParagraph : cellParList) {
List<XWPFRun> run = xwpfParagraph.getRuns(); //每个格子的内容都要单独处理
if (CollectionUtils.isEmpty(run)) {
continue;
}
for (int q = 0; q < run.size(); q++) {
String text0 = run.get(q).getText(run.get(q).getTextPosition());
if (StringUtils.isBlank(text0)) {
continue;
}
if (!StringUtils.isBlank(text0)) {
if (text0.contains("{{") && text0.contains("}}")) {
if (Objects.equals(text0.replaceAll(" ", ""), key.replaceAll(" ", ""))) {
run.get(q).setText(map.get(key).toString(), 0);
}
}
//解析word 出现一个字符单独出现的情况
if (text0.trim().equals(key)) {
run.get(q).setText(map.get(key).toString(), 0);
}
if (text0.trim().contains("{{") && !text0.trim().contains("}}")) {
int startIndex = text0.lastIndexOf("{{");
int endIndex = 1;
if (startIndex != -1) {
endIndex = text0.substring(startIndex).indexOf("}}");
}
if (endIndex < 0) {
// 记录分隔符中间跨越的runs数量,用于字符串拼接和替换
int num = 0;
int j = q + 1;
for (; j < run.size(); j++) {
String text1 = run.get(j).getText(run.get(j).getTextPosition());
if (text1 != null && text1.contains("}}")) {
num = j - q;
break;
}
}
if (num != 0) {
// num!=0说明找到了 {{ {{ 配对替换
StringBuilder newText = new StringBuilder();
for (int s = q; s <= q + num; s++) {
String text2 = run.get(s).getText(run.get(s).getTextPosition());
if (!StringUtils.isBlank(text2)) {
String replaceText = text2;
if (s == q && text2.contains("{{") && text2.contains("}}")) {
newText.append(text2);
} else if (s == q && text2.contains("{{")) {
replaceText = text2.substring(0, text2.indexOf("{{"));
newText.append(text2.substring(text2.indexOf("{{")));
} else if (text2.contains("}}")) {
replaceText = text2.substring(replaceText.indexOf("}}") + 2);
newText.append(text2.substring(0, text2.indexOf("}}") + 2));
} else {
replaceText = "";
newText.append(text2);
}
run.get(s).setText(replaceText, 0);
}
}
//合并替换
run.get(q).setText(newText.toString(), 0);
q = q - 1;
}
}
}
}
}
}
}
}
}
}////表格嵌套表格 end
}
}
}// 表格 end
}
}
}
2.结束语
代码优化不够,我也不知道咋改了。。。。

浙公网安备 33010602011771号