update 文档拆分表格

This commit is contained in:
lijiarao
2023-11-08 16:57:18 +08:00
parent e11edb4bc5
commit 56722244b3
2 changed files with 96 additions and 69 deletions
@@ -37,6 +37,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
@@ -120,29 +121,22 @@ public class FileSpiltService {
// nowStart.add(String.valueOf(sarFileSplitInfoEO.getStartNumber()));
// }
//获取文件地址
String connectId = sarFileSplitInfoEO.getConnectId();
String fileId = sarFileSplitInfoEO.getFileId();
// if (StringUtils.isEmpty(connectId) || StringUtils.isEmpty(fileName)) {
// throw new JeroBootException("当前文件未找到,请重新选择!");
// }
LambdaQueryWrapper<OSSFile> queryWrapper = new LambdaQueryWrapper();
// queryWrapper.eq(OSSFile::getConnectId, connectId);
queryWrapper.eq(OSSFile::getId, fileId);
queryWrapper.orderByDesc(OSSFile::getCreateTime);
List<OSSFile> ossFileList = iossFileService.list(queryWrapper);
if (CollectionUtils.isEmpty(ossFileList)) {
throw new JeroBootException("当前文件未找到,请重新选择!");
}
OSSFile ossFile = ossFileList.get(0);
String readFilePath = ossFile.getUrl();
if (StringUtils.isEmpty(readFilePath) || !MinioUtil.doesObjectExist(readFilePath)) {
throw new JeroBootException("当前文件未找到,请重新选择!");
}
try (InputStream is = MinioUtil.download(readFilePath);InputStream is1 = MinioUtil.download(readFilePath)) {
// readFilePath = "E:\\Data\\Downloads\\wordtest\\GB7258标准修订test - 表格测试 - 副本.docx";
// try (InputStream is = new FileInputStream(readFilePath);InputStream is1 = new FileInputStream(readFilePath)) {//需要将文件路更改为word文档所在路径。
//String fileId = sarFileSplitInfoEO.getFileId();
//LambdaQueryWrapper<OSSFile> queryWrapper = new LambdaQueryWrapper();
//queryWrapper.eq(OSSFile::getId, fileId);
//queryWrapper.orderByDesc(OSSFile::getCreateTime);
//List<OSSFile> ossFileList = iossFileService.list(queryWrapper);
//if (CollectionUtils.isEmpty(ossFileList)) {
// throw new JeroBootException("当前文件未找到,请重新选择!");
//}
//OSSFile ossFile = ossFileList.get(0);
//String readFilePath = ossFile.getUrl();
//if (StringUtils.isEmpty(readFilePath) || !MinioUtil.doesObjectExist(readFilePath)) {
// throw new JeroBootException("当前文件未找到,请重新选择!");
//}
//try (InputStream is = MinioUtil.download(readFilePath);InputStream is1 = MinioUtil.download(readFilePath)) {
String readFilePath = "E:\\Data\\Downloads\\wordtest\\GB7258标准修订test - 表格测试 - 副本.docx";
try (InputStream is = new FileInputStream(readFilePath); InputStream is1 = new FileInputStream(readFilePath)) {//需要将文件路更改为word文档所在路径。
ZipSecureFile.setMinInflateRatio(-1.0d);
XWPFDocument doc = new XWPFDocument(is);
@@ -882,34 +876,6 @@ public class FileSpiltService {
ReadWordTable readWordTable = new ReadWordTable();
String tableStr = readWordTable.readTable(table);
tabaleStringNew.append(tableStr);
/*List<XWPFTableRow> rows = table.getRows();
List<SarFileSplitItemsTableEO> tableList = new ArrayList<>();
int rowNum = 1;
for (XWPFTableRow row : rows) {
List<XWPFTableCell> cells = row.getTableCells();
int colNum = 1;
for (XWPFTableCell cell : cells) {
CTTcPr tcpr = cell.getCTTc().getTcPr();
CTVMerge vMerge = tcpr.getVMerge();
CTDecimalNumber gridSpan = tcpr.getGridSpan();
if (gridSpan != null) {
String aa = gridSpan.getVal().toString();
}
// 简单获取内容(简单方式是不能获取字体对齐方式的)
SarFileSplitItemsTableEO sarFileSplitItemsTableEO = new SarFileSplitItemsTableEO();
sarFileSplitItemsTableEO.setRowNum(rowNum);
sarFileSplitItemsTableEO.setColNum(colNum);
sarFileSplitItemsTableEO.setContent(cell.getText());
tableList.add(sarFileSplitItemsTableEO);
// 一个单元格可以理解为一个word文档,单元格里也可以加段落与表格
*//* List<XWPFParagraph> paragraphs = cell.getParagraphs();
for (XWPFParagraph paragraph : paragraphs) {
getParagraphText(paragraph);
}*//*
colNum++;
}
rowNum++;
}*/
message.getItemsCondi().add(table.getText());
message.setItemContent(message.getItemContent() + tabaleStringNew);
itemsValList.add(getSplitItemsValObject(message.getId(), SplitFilePragraTypeEnum.TABLE.getValue(), tabaleStringNew.toString(), message.getItemsCondi().size(), null));
@@ -1,10 +1,7 @@
package com.jero.modules.split.util;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDecimalNumber;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
import java.io.IOException;
import java.math.BigInteger;
@@ -64,8 +61,13 @@ public class ReadWordTable {
public int getRowspan(XWPFTable table, int row, int col) {
XWPFTableCell cell = table.getRow(row).getCell(col);
// 正常独立单元格
if (!isContinueRow(cell) && !isRestartRow(cell)) {
//合并单元格的中间cell
boolean continueRow = isContinueRow(cell);
//合并单元格的开始cell
boolean restartRow = isRestartRow(cell);
// 上面两种都不是就是正常独立单元格
if (!continueRow && !restartRow) {
return 1;
}
// 当前单元格的宽度
@@ -138,7 +140,7 @@ public class ReadWordTable {
if (tcPr.getVMerge() == null) {
return false;
}
if (tcPr.getVMerge().getVal() == null) {
if (tcPr.getVMerge().getVal().toString().equalsIgnoreCase("continue")) {
return true;
}
return false;
@@ -181,33 +183,92 @@ public class ReadWordTable {
public String readTable(XWPFTable table) throws IOException {
// 表格行数
int tableRowsSize = table.getRows().size();
StringBuilder tableToHtmlStr = new StringBuilder("<table border=\"1\">");
StringBuilder tableToHtmlStr = new StringBuilder("<table>");
for (int i = 0; i < tableRowsSize; i++) {
tableToHtmlStr.append("<tr>");
//最后一行的都增加下边框
if (i == tableRowsSize-1){
tableToHtmlStr.append("<tr style=\"border-bottom: 1px solid;\">");
}else {
tableToHtmlStr.append("<tr>");
}
int tableCellsSize = table.getRow(i).getTableCells().size();
for (int j = 0; j < tableCellsSize; j++) {
if (isOmitCell(i, j)) {
continue;
}
XWPFTableCell tableCell = table.getRow(i).getCell(j);
// 获取单元格的属性
CTTcPr tcPr = tableCell.getCTTc().getTcPr();
int colspan = getColspan(tcPr);
CTTcPr cellProperties = tableCell.getCTTc().getTcPr();
int colspan = getColspan(cellProperties);
if (colspan > 1) { // 合并的列
tableToHtmlStr.append("<td colspan='" + colspan + "'");
tableToHtmlStr.append("<td colspan=\"" + colspan + "\" ");
} else { // 正常列
tableToHtmlStr.append("<td");
tableToHtmlStr.append("<td ");
}
tableToHtmlStr.append("style=\" ");
XWPFTableCell.XWPFVertAlign verticalAlignment = tableCell.getVerticalAlignment();
if (verticalAlignment != null){
tableToHtmlStr.append("vertical-align: middle;");
}
CTTcBorders tcBorders = cellProperties.getTcBorders();
if (tcBorders != null){
CTBorder top = tcBorders.getTop();
if (top != null){
tableToHtmlStr.append("border-top: 1px solid;");
}
CTBorder bottom = tcBorders.getBottom();
if (bottom != null){
tableToHtmlStr.append("border-bottom: 1px solid;");
}
CTBorder left = tcBorders.getLeft();
if (left != null){
tableToHtmlStr.append("border-left: 1px solid;");
}
CTBorder right = tcBorders.getRight();
if (right != null){
tableToHtmlStr.append("border-right: 1px solid;");
}
}
tableToHtmlStr.append(" \" ");
int rowspan = getRowspan(table, i, j);
if (rowspan > 1) { // 合并的行
tableToHtmlStr.append(" rowspan='" + rowspan + "'>");
// 合并的行
if (rowspan > 1) {
tableToHtmlStr.append(" rowspan=\"" + rowspan + "\">");
} else {
tableToHtmlStr.append(">");
}
String text = tableCell.getText();
tableToHtmlStr.append(text + "</td>");
StringBuilder stringBuilder = new StringBuilder();
List<XWPFParagraph> paragraphs = tableCell.getParagraphs();
for (XWPFParagraph paragraph : paragraphs) {
for (XWPFRun xwrun : paragraph.getRuns()) {
VerticalAlign subscript = xwrun.getSubscript();
String smalltext = xwrun.getText(0);
if (smalltext==null){
continue;
}
switch (subscript) {
case BASELINE:
stringBuilder.append(smalltext);
break;
case SUBSCRIPT:
stringBuilder.append("<sub>").append(smalltext).append("</sub>");
break;
case SUPERSCRIPT:
stringBuilder.append("<sup>").append(smalltext).append("</sup>");
break;
default:
stringBuilder.append(smalltext);
}
}
stringBuilder.append("<br>");
}
tableToHtmlStr.append(stringBuilder).append("</td>");
}
tableToHtmlStr.append("</tr>");