update 文档拆分表格
This commit is contained in:
+17
-51
@@ -37,6 +37,7 @@ import org.springframework.stereotype.Service;
|
|||||||
import org.springframework.transaction.annotation.Propagation;
|
import org.springframework.transaction.annotation.Propagation;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import java.io.FileInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
@@ -120,29 +121,22 @@ public class FileSpiltService {
|
|||||||
// nowStart.add(String.valueOf(sarFileSplitInfoEO.getStartNumber()));
|
// nowStart.add(String.valueOf(sarFileSplitInfoEO.getStartNumber()));
|
||||||
// }
|
// }
|
||||||
//获取文件地址
|
//获取文件地址
|
||||||
String connectId = sarFileSplitInfoEO.getConnectId();
|
//String fileId = sarFileSplitInfoEO.getFileId();
|
||||||
String fileId = sarFileSplitInfoEO.getFileId();
|
//LambdaQueryWrapper<OSSFile> queryWrapper = new LambdaQueryWrapper();
|
||||||
// if (StringUtils.isEmpty(connectId) || StringUtils.isEmpty(fileName)) {
|
//queryWrapper.eq(OSSFile::getId, fileId);
|
||||||
// throw new JeroBootException("当前文件未找到,请重新选择!");
|
//queryWrapper.orderByDesc(OSSFile::getCreateTime);
|
||||||
// }
|
//List<OSSFile> ossFileList = iossFileService.list(queryWrapper);
|
||||||
LambdaQueryWrapper<OSSFile> queryWrapper = new LambdaQueryWrapper();
|
//if (CollectionUtils.isEmpty(ossFileList)) {
|
||||||
// queryWrapper.eq(OSSFile::getConnectId, connectId);
|
// throw new JeroBootException("当前文件未找到,请重新选择!");
|
||||||
queryWrapper.eq(OSSFile::getId, fileId);
|
//}
|
||||||
queryWrapper.orderByDesc(OSSFile::getCreateTime);
|
//OSSFile ossFile = ossFileList.get(0);
|
||||||
List<OSSFile> ossFileList = iossFileService.list(queryWrapper);
|
//String readFilePath = ossFile.getUrl();
|
||||||
if (CollectionUtils.isEmpty(ossFileList)) {
|
//if (StringUtils.isEmpty(readFilePath) || !MinioUtil.doesObjectExist(readFilePath)) {
|
||||||
throw new JeroBootException("当前文件未找到,请重新选择!");
|
// throw new JeroBootException("当前文件未找到,请重新选择!");
|
||||||
}
|
//}
|
||||||
OSSFile ossFile = ossFileList.get(0);
|
//try (InputStream is = MinioUtil.download(readFilePath);InputStream is1 = MinioUtil.download(readFilePath)) {
|
||||||
|
String readFilePath = "E:\\Data\\Downloads\\wordtest\\GB7258标准修订test - 表格测试 - 副本.docx";
|
||||||
String readFilePath = ossFile.getUrl();
|
try (InputStream is = new FileInputStream(readFilePath); InputStream is1 = new FileInputStream(readFilePath)) {//需要将文件路更改为word文档所在路径。
|
||||||
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文档所在路径。
|
|
||||||
ZipSecureFile.setMinInflateRatio(-1.0d);
|
ZipSecureFile.setMinInflateRatio(-1.0d);
|
||||||
XWPFDocument doc = new XWPFDocument(is);
|
XWPFDocument doc = new XWPFDocument(is);
|
||||||
|
|
||||||
@@ -882,34 +876,6 @@ public class FileSpiltService {
|
|||||||
ReadWordTable readWordTable = new ReadWordTable();
|
ReadWordTable readWordTable = new ReadWordTable();
|
||||||
String tableStr = readWordTable.readTable(table);
|
String tableStr = readWordTable.readTable(table);
|
||||||
tabaleStringNew.append(tableStr);
|
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.getItemsCondi().add(table.getText());
|
||||||
message.setItemContent(message.getItemContent() + tabaleStringNew);
|
message.setItemContent(message.getItemContent() + tabaleStringNew);
|
||||||
itemsValList.add(getSplitItemsValObject(message.getId(), SplitFilePragraTypeEnum.TABLE.getValue(), tabaleStringNew.toString(), message.getItemsCondi().size(), null));
|
itemsValList.add(getSplitItemsValObject(message.getId(), SplitFilePragraTypeEnum.TABLE.getValue(), tabaleStringNew.toString(), message.getItemsCondi().size(), null));
|
||||||
|
|||||||
@@ -1,10 +1,7 @@
|
|||||||
package com.jero.modules.split.util;
|
package com.jero.modules.split.util;
|
||||||
|
|
||||||
import org.apache.poi.xwpf.usermodel.XWPFTable;
|
import org.apache.poi.xwpf.usermodel.*;
|
||||||
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
|
||||||
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 java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
@@ -64,8 +61,13 @@ public class ReadWordTable {
|
|||||||
public int getRowspan(XWPFTable table, int row, int col) {
|
public int getRowspan(XWPFTable table, int row, int col) {
|
||||||
|
|
||||||
XWPFTableCell cell = table.getRow(row).getCell(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;
|
return 1;
|
||||||
}
|
}
|
||||||
// 当前单元格的宽度
|
// 当前单元格的宽度
|
||||||
@@ -138,7 +140,7 @@ public class ReadWordTable {
|
|||||||
if (tcPr.getVMerge() == null) {
|
if (tcPr.getVMerge() == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (tcPr.getVMerge().getVal() == null) {
|
if (tcPr.getVMerge().getVal().toString().equalsIgnoreCase("continue")) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@@ -181,33 +183,92 @@ public class ReadWordTable {
|
|||||||
public String readTable(XWPFTable table) throws IOException {
|
public String readTable(XWPFTable table) throws IOException {
|
||||||
// 表格行数
|
// 表格行数
|
||||||
int tableRowsSize = table.getRows().size();
|
int tableRowsSize = table.getRows().size();
|
||||||
StringBuilder tableToHtmlStr = new StringBuilder("<table border=\"1\">");
|
StringBuilder tableToHtmlStr = new StringBuilder("<table>");
|
||||||
|
|
||||||
for (int i = 0; i < tableRowsSize; i++) {
|
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();
|
int tableCellsSize = table.getRow(i).getTableCells().size();
|
||||||
for (int j = 0; j < tableCellsSize; j++) {
|
for (int j = 0; j < tableCellsSize; j++) {
|
||||||
if (isOmitCell(i, j)) {
|
if (isOmitCell(i, j)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
XWPFTableCell tableCell = table.getRow(i).getCell(j);
|
XWPFTableCell tableCell = table.getRow(i).getCell(j);
|
||||||
|
|
||||||
// 获取单元格的属性
|
// 获取单元格的属性
|
||||||
CTTcPr tcPr = tableCell.getCTTc().getTcPr();
|
CTTcPr cellProperties = tableCell.getCTTc().getTcPr();
|
||||||
int colspan = getColspan(tcPr);
|
|
||||||
|
int colspan = getColspan(cellProperties);
|
||||||
if (colspan > 1) { // 合并的列
|
if (colspan > 1) { // 合并的列
|
||||||
tableToHtmlStr.append("<td colspan='" + colspan + "'");
|
tableToHtmlStr.append("<td colspan=\"" + colspan + "\" ");
|
||||||
} else { // 正常列
|
} 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);
|
int rowspan = getRowspan(table, i, j);
|
||||||
if (rowspan > 1) { // 合并的行
|
// 合并的行
|
||||||
tableToHtmlStr.append(" rowspan='" + rowspan + "'>");
|
if (rowspan > 1) {
|
||||||
|
tableToHtmlStr.append(" rowspan=\"" + rowspan + "\">");
|
||||||
} else {
|
} else {
|
||||||
tableToHtmlStr.append(">");
|
tableToHtmlStr.append(">");
|
||||||
}
|
}
|
||||||
String text = tableCell.getText();
|
StringBuilder stringBuilder = new StringBuilder();
|
||||||
tableToHtmlStr.append(text + "</td>");
|
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>");
|
tableToHtmlStr.append("</tr>");
|
||||||
|
|||||||
Reference in New Issue
Block a user