优化审计取证单Word文档表格单元格换行处理
This commit is contained in:
@@ -25,6 +25,10 @@ import org.apache.poi.openxml4j.util.ZipSecureFile;
|
||||
import org.apache.poi.xwpf.usermodel.XWPFDocument;
|
||||
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
|
||||
import org.apache.poi.xwpf.usermodel.XWPFRun;
|
||||
import org.apache.poi.xwpf.usermodel.XWPFTable;
|
||||
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
|
||||
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -137,9 +141,10 @@ public class AuditEvidenceController extends BaseController {
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理段落换行(与审计报告相同的处理逻辑)
|
||||
* 处理段落换行(增强版,同时处理表格单元格)
|
||||
*/
|
||||
private void processParagraphs(XWPFDocument document) {
|
||||
// 1. 处理普通段落
|
||||
List<XWPFParagraph> originalParas = new ArrayList<>(document.getParagraphs());
|
||||
|
||||
for (XWPFParagraph para : originalParas) {
|
||||
@@ -172,5 +177,38 @@ public class AuditEvidenceController extends BaseController {
|
||||
// 删除原段落
|
||||
document.removeBodyElement(pos + parts.length);
|
||||
}
|
||||
|
||||
// 2. 处理表格单元格中的段落
|
||||
List<XWPFTable> tables = document.getTables();
|
||||
for (XWPFTable table : tables) {
|
||||
for (XWPFTableRow row : table.getRows()) {
|
||||
for (XWPFTableCell cell : row.getTableCells()) {
|
||||
List<XWPFParagraph> cellParagraphs = cell.getParagraphs();
|
||||
for (XWPFParagraph para : cellParagraphs) {
|
||||
String text = para.getText();
|
||||
if (text == null || !text.contains("\n")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 清空原段落内容
|
||||
for (int i = para.getRuns().size() - 1; i >= 0; i--) {
|
||||
para.removeRun(i);
|
||||
}
|
||||
|
||||
// 分割文本并按行添加
|
||||
String[] lines = text.replace("\r\n", "\n").replace("\r", "\n").split("\n");
|
||||
for (int i = 0; i < lines.length; i++) {
|
||||
XWPFRun run = para.createRun();
|
||||
run.setText(lines[i].trim());
|
||||
|
||||
// 如果不是最后一行,添加换行
|
||||
if (i < lines.length - 1) {
|
||||
run.addBreak();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user