word拆分功能处理
This commit is contained in:
+5
@@ -100,4 +100,9 @@ public class SarFileSplitInfoEO implements Serializable {
|
||||
@TableField(exist = false)
|
||||
private String flag;
|
||||
|
||||
@TableField(exist = false)
|
||||
private int startNumber;
|
||||
@TableField(exist = false)
|
||||
private int stopNumber;
|
||||
|
||||
}
|
||||
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package com.jero.modules.split.enums;
|
||||
|
||||
/**
|
||||
* 标准库参数类型枚举设置
|
||||
* @author gaoyan
|
||||
* date 2018/09/03
|
||||
*/
|
||||
public enum SplitFilePragraTypeEnum {
|
||||
|
||||
TEXT("TEXT","文字"),IMG("IMG","图片"),TABLE("TABLE","表格");
|
||||
|
||||
private String value;
|
||||
private String lable;
|
||||
|
||||
private SplitFilePragraTypeEnum(String value, String lable) {
|
||||
this.value = value;
|
||||
this.lable = lable;
|
||||
}
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
public String getLable() {
|
||||
return lable;
|
||||
}
|
||||
|
||||
}
|
||||
+11
-12
@@ -367,24 +367,23 @@
|
||||
|
||||
<!-- 批量插入接口 -->
|
||||
<insert id="insertForeach" parameterType="java.util.List">
|
||||
begin
|
||||
<foreach collection="list" item="item" index="index" separator=";">
|
||||
insert into sar_file_split_items_0329
|
||||
(id, info_id, items_num, items_name, newcar_put_time, product_put_time, menu_id, function_territory, apply_arctic, reference_stand, information_category, relevance_file, technology_territory, valid_flag, creation_user, creation_time, modify_time,
|
||||
|
||||
insert into sar_file_split_items
|
||||
(id, info_id, items_num, items_name, menu_id, function_territory,
|
||||
information_category, technology_territory,
|
||||
creation_user, creation_time, modify_time,
|
||||
modify_user, iterms_conditions)
|
||||
values (
|
||||
values
|
||||
<foreach collection="list" item="item" index="index" separator=",">(
|
||||
#{item.id, jdbcType=VARCHAR}, #{item.infoId, jdbcType=VARCHAR}, #{item.itemsNum, jdbcType=VARCHAR},
|
||||
#{item.itemsName, jdbcType=VARCHAR},
|
||||
#{item.newcarPutTime, jdbcType=TIMESTAMP}, #{item.productPutTime, jdbcType=TIMESTAMP},
|
||||
#{item.menuId, jdbcType=VARCHAR}, #{item.functionTerritory, jdbcType=VARCHAR}, #{item.applyArctic, jdbcType=VARCHAR},
|
||||
#{item.referenceStand, jdbcType=VARCHAR}, #{item.informationCategory, jdbcType=VARCHAR},
|
||||
#{item.relevanceFile, jdbcType=VARCHAR}, #{item.technologyTerritory, jdbcType=VARCHAR},
|
||||
#{item.validFlag, jdbcType=INTEGER}, #{item.creationUser, jdbcType=VARCHAR},
|
||||
#{item.menuId, jdbcType=VARCHAR}, #{item.functionTerritory, jdbcType=VARCHAR},
|
||||
#{item.informationCategory, jdbcType=VARCHAR}, #{item.technologyTerritory, jdbcType=VARCHAR},
|
||||
#{item.creationUser, jdbcType=VARCHAR},
|
||||
#{item.creationTime, jdbcType=TIMESTAMP}, #{item.modifyTime, jdbcType=TIMESTAMP},
|
||||
#{item.modifyUser, jdbcType=VARCHAR}, #{item.itermsConditions, jdbcType=CLOB}
|
||||
)
|
||||
)
|
||||
</foreach>
|
||||
;end;
|
||||
</insert>
|
||||
|
||||
<delete id="deleteByIds" parameterType="com.jero.modules.split.entity.SarFileSplitItemsEO">
|
||||
|
||||
+13
-13
@@ -219,20 +219,20 @@
|
||||
|
||||
<!-- 批量插入接口 -->
|
||||
<insert id="insertForeach" parameterType="java.util.List">
|
||||
insert into SAR_FILE_SPLIT_MENU(<include refid="Base_Column_List" />) (
|
||||
<foreach collection="list" item="item" index="index" separator="UNION ALL">
|
||||
select
|
||||
#{item.id, jdbcType=VARCHAR},
|
||||
#{item.infoId, jdbcType=VARCHAR},
|
||||
#{item.name, jdbcType=VARCHAR},
|
||||
#{item.pId, jdbcType=VARCHAR},
|
||||
#{item.displaySeq, jdbcType=VARCHAR},
|
||||
#{item.validFlag, jdbcType=INTEGER},
|
||||
#{item.creationTime, jdbcType=TIMESTAMP},
|
||||
#{item.modifyTime, jdbcType=TIMESTAMP}, #{item.remarks, jdbcType=VARCHAR},
|
||||
#{item.creationUser, jdbcType=VARCHAR}, #{item.modifyUser, jdbcType=VARCHAR} , #{item.itemName, jdbcType=VARCHAR} from dual
|
||||
insert into SAR_FILE_SPLIT_MENU(<include refid="Base_Column_List" />)values
|
||||
<foreach collection="list" item="item" index="index" separator=",">
|
||||
( #{item.id, jdbcType=VARCHAR},
|
||||
#{item.infoId, jdbcType=VARCHAR},
|
||||
#{item.name, jdbcType=VARCHAR},
|
||||
#{item.pId, jdbcType=VARCHAR},
|
||||
#{item.displaySeq, jdbcType=VARCHAR},
|
||||
#{item.validFlag, jdbcType=INTEGER},
|
||||
#{item.creationTime, jdbcType=TIMESTAMP},
|
||||
#{item.modifyTime, jdbcType=TIMESTAMP}, #{item.remarks, jdbcType=VARCHAR},
|
||||
#{item.creationUser, jdbcType=VARCHAR}, #{item.modifyUser, jdbcType=VARCHAR} ,
|
||||
#{item.itemName, jdbcType=VARCHAR}
|
||||
)
|
||||
</foreach>
|
||||
)
|
||||
</insert>
|
||||
|
||||
<delete id="deleteByIds" parameterType="com.jero.modules.split.entity.SarFileSplitMenuEO">
|
||||
|
||||
+1
@@ -112,4 +112,5 @@ public interface ISarFileSplitItemsEOService extends IService<SarFileSplitItemsE
|
||||
public void createItemsInfo(SarFileSplitItemsEO sarFileSplitItemsEO,String menuId,String infoId);
|
||||
|
||||
|
||||
int insertForeach(List<SarFileSplitItemsEO> messageList);
|
||||
}
|
||||
|
||||
+1
@@ -40,4 +40,5 @@ public interface ISarFileSplitItemsValEOService extends IService<SarFileSplitIte
|
||||
|
||||
List<SarFileSplitItemsValEO> queryByPage(SarFileSplitItemsValEOPage page);
|
||||
|
||||
int insertForeach(List<SarFileSplitItemsValEO> itemValList);
|
||||
}
|
||||
|
||||
+1296
File diff suppressed because it is too large
Load Diff
+17
-11
@@ -11,7 +11,6 @@ import com.jero.generater.modules.online.cgform.service.impl.OnlCgformFieldServi
|
||||
import com.jero.modules.oss.entity.OSSFile;
|
||||
import com.jero.modules.oss.service.IOSSFileService;
|
||||
import com.jero.modules.split.entity.SarFileSplitInfoEO;
|
||||
import com.jero.modules.split.entity.SarFileSplitMenuEO;
|
||||
import com.jero.modules.split.mapper.SarFileSplitInfoMapper;
|
||||
import com.jero.modules.split.mapper.SarFileSplitMenuEOMapper;
|
||||
import com.jero.modules.split.service.ISarFileSplitInfoService;
|
||||
@@ -47,6 +46,9 @@ public class SarFileSplitInfoServiceImpl extends ServiceImpl<SarFileSplitInfoMap
|
||||
|
||||
@Autowired
|
||||
private ISysDictItemService sysDictItemService;
|
||||
@Autowired
|
||||
private FileSpiltService fileSpiltService;
|
||||
|
||||
|
||||
/**
|
||||
* 保存
|
||||
@@ -57,22 +59,26 @@ public class SarFileSplitInfoServiceImpl extends ServiceImpl<SarFileSplitInfoMap
|
||||
@Override
|
||||
public void add(SarFileSplitInfoEO sarFileSplitInfoEO) {
|
||||
Date now = new Date();
|
||||
//保存拆分数据
|
||||
String infoId = UUID.randomUUID().toString().replace("-", "");
|
||||
sarFileSplitInfoEO.setId(infoId);
|
||||
sarFileSplitInfoEO.setSplitResult("成功"); // TODO 这块之后可能不会用到
|
||||
sarFileSplitInfoEO.setCreateTime(now);
|
||||
sarFileSplitInfoEO.setUpdateTime(now);
|
||||
save(sarFileSplitInfoEO);
|
||||
// 添加条款总目录 TODO 这块之后可能不会用到
|
||||
SarFileSplitMenuEO sarFileSplitMenuEO = new SarFileSplitMenuEO();
|
||||
sarFileSplitMenuEO.setName("总目录");
|
||||
sarFileSplitMenuEO.setInfoId(infoId);
|
||||
sarFileSplitMenuEO.setDisplaySeq(1L);
|
||||
sarFileSplitMenuEO.setId(UUID.randomUUID().toString().replace("-", ""));
|
||||
sarFileSplitMenuEO.setValidFlag(0);
|
||||
sarFileSplitMenuEO.setCreationTime(new Date());
|
||||
sarFileSplitMenuEO.setModifyTime(new Date());
|
||||
sarFileSplitMenuEOMapper.insertSelective(sarFileSplitMenuEO);
|
||||
|
||||
fileSpiltService.fileCHN(sarFileSplitInfoEO);
|
||||
|
||||
// 添加条款总目录 TODO 这块之后可能不会用到
|
||||
// SarFileSplitMenuEO sarFileSplitMenuEO = new SarFileSplitMenuEO();
|
||||
// sarFileSplitMenuEO.setName("总目录");
|
||||
// sarFileSplitMenuEO.setInfoId(infoId);
|
||||
// sarFileSplitMenuEO.setDisplaySeq(1L);
|
||||
// sarFileSplitMenuEO.setId(UUID.randomUUID().toString().replace("-", ""));
|
||||
// sarFileSplitMenuEO.setValidFlag(0);
|
||||
// sarFileSplitMenuEO.setCreationTime(new Date());
|
||||
// sarFileSplitMenuEO.setModifyTime(new Date());
|
||||
// sarFileSplitMenuEOMapper.insertSelective(sarFileSplitMenuEO);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+4
@@ -1461,6 +1461,10 @@ public class SarFileSplitItemsEOServiceImpl extends ServiceImpl<SarFileSplitItem
|
||||
dao.insertSelective(sarFileSplitItemsEO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int insertForeach(List<SarFileSplitItemsEO> messageList) {
|
||||
return dao.insertForeach(messageList);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
+6
@@ -137,4 +137,10 @@ public class SarFileSplitItemsValEOServiceImpl extends ServiceImpl<SarFileSplitI
|
||||
return dao.queryByPage(page);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int insertForeach(List<SarFileSplitItemsValEO> itemValList) {
|
||||
|
||||
return dao.insertForeach(itemValList);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+234
@@ -0,0 +1,234 @@
|
||||
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.CTTcPr;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.math.BigInteger;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Description:
|
||||
* @Author: yangxuenan
|
||||
* date: 2020/2/25 16:58
|
||||
*/
|
||||
public class ReadWordTable {
|
||||
|
||||
|
||||
/**
|
||||
* 保存生成HTML时需要被忽略的单元格
|
||||
*/
|
||||
private List<String> omitCellsList = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* 生成忽略的单元格列表中的格式
|
||||
*
|
||||
* @param row
|
||||
* @param col
|
||||
* @return
|
||||
*/
|
||||
public String generateOmitCellStr(int row, int col) {
|
||||
return row + ":" + col;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前单元格的colspan(列合并)的列数
|
||||
*
|
||||
* @param tcPr 单元格属性
|
||||
* @return
|
||||
*/
|
||||
public int getColspan(CTTcPr tcPr) {
|
||||
// 判断是否存在列合并
|
||||
CTDecimalNumber gridSpan = null;
|
||||
if ((gridSpan = tcPr.getGridSpan()) != null) { // 合并的起始列
|
||||
// 获取合并的列数
|
||||
BigInteger num = gridSpan.getVal();
|
||||
return num.intValue();
|
||||
} else { // 其他被合并的列或正常列
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前单元格的rowspan(行合并)的行数
|
||||
*
|
||||
* @param table 表格
|
||||
* @param row 行值
|
||||
* @param col 列值
|
||||
* @return
|
||||
*/
|
||||
public int getRowspan(XWPFTable table, int row, int col) {
|
||||
|
||||
XWPFTableCell cell = table.getRow(row).getCell(col);
|
||||
// 正常独立单元格
|
||||
if (!isContinueRow(cell) && !isRestartRow(cell)) {
|
||||
return 1;
|
||||
}
|
||||
// 当前单元格的宽度
|
||||
int cellWidth = getCellWidth(table, row, col);
|
||||
// 当前单元格距离左侧边框的距离
|
||||
int leftWidth = getLeftWidth(table, row, col);
|
||||
|
||||
// 用户保存当前单元格行合并的单元格数-1(因为不包含自身)
|
||||
List<Boolean> list = new ArrayList<>();
|
||||
getRowspan(table, row, cellWidth, leftWidth, list);
|
||||
|
||||
return list.size() + 1;
|
||||
}
|
||||
|
||||
private void getRowspan(XWPFTable table, int row, int cellWidth, int leftWidth,
|
||||
List<Boolean> list) {
|
||||
// 已达到最后一行
|
||||
if (row + 1 >= table.getNumberOfRows()) {
|
||||
return;
|
||||
}
|
||||
row = row + 1;
|
||||
int colsNum = table.getRow(row).getTableCells().size();
|
||||
// 因为列合并单元格可能导致行合并的单元格并不在同一列,所以从头遍历列,通过属性、宽度以及距离左边框间距来判断是否是行合并
|
||||
for (int i = 0; i < colsNum; i++) {
|
||||
XWPFTableCell testTable = table.getRow(row).getCell(i);
|
||||
// 是否为合并单元格的中间行(包括结尾行)
|
||||
if (isContinueRow(testTable)) {
|
||||
// 是被上一行单元格合并的单元格
|
||||
if (getCellWidth(table, row, i) == cellWidth
|
||||
&& getLeftWidth(table, row, i) == leftWidth) {
|
||||
list.add(true);
|
||||
// 被合并的单元格在生成html时需要忽略
|
||||
addOmitCell(row, i);
|
||||
// 去下一行继续查找
|
||||
getRowspan(table, row, cellWidth, leftWidth, list);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否是合并行的起始行单元格
|
||||
*
|
||||
* @param tableCell
|
||||
* @return
|
||||
*/
|
||||
public boolean isRestartRow(XWPFTableCell tableCell) {
|
||||
CTTcPr tcPr = tableCell.getCTTc().getTcPr();
|
||||
if (tcPr.getVMerge() == null) {
|
||||
return false;
|
||||
}
|
||||
if (tcPr.getVMerge().getVal() == null) {
|
||||
return false;
|
||||
}
|
||||
if (tcPr.getVMerge().getVal().toString().equalsIgnoreCase("restart")) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否是合并行的中间行单元格(包括结尾的最后一行的单元格)
|
||||
*
|
||||
* @param tableCell
|
||||
* @return
|
||||
*/
|
||||
public boolean isContinueRow(XWPFTableCell tableCell) {
|
||||
CTTcPr tcPr = tableCell.getCTTc().getTcPr();
|
||||
if (tcPr.getVMerge() == null) {
|
||||
return false;
|
||||
}
|
||||
if (tcPr.getVMerge().getVal() == null) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public int getLeftWidth(XWPFTable table, int row, int col) {
|
||||
int leftWidth = 0;
|
||||
for (int i = 0; i < col; i++) {
|
||||
leftWidth += getCellWidth(table, row, i);
|
||||
}
|
||||
return leftWidth;
|
||||
}
|
||||
|
||||
public int getCellWidth(XWPFTable table, int row, int col) {
|
||||
BigInteger width = table.getRow(row).getCell(col).getCTTc().getTcPr().getTcW().getW();
|
||||
return width.intValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加忽略的单元格(被行合并的单元格,生成HTML时需要忽略)
|
||||
*
|
||||
* @param row
|
||||
* @param col
|
||||
*/
|
||||
public void addOmitCell(int row, int col) {
|
||||
String omitCellStr = generateOmitCellStr(row, col);
|
||||
omitCellsList.add(omitCellStr);
|
||||
}
|
||||
|
||||
public boolean isOmitCell(int row, int col) {
|
||||
String cellStr = generateOmitCellStr(row, col);
|
||||
return omitCellsList.contains(cellStr);
|
||||
}
|
||||
|
||||
public String readTable(XWPFTable table) throws IOException {
|
||||
// 表格行数
|
||||
int tableRowsSize = table.getRows().size();
|
||||
StringBuilder tableToHtmlStr = new StringBuilder("<table>");
|
||||
|
||||
for (int i = 0; i < tableRowsSize; i++) {
|
||||
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);
|
||||
if (colspan > 1) { // 合并的列
|
||||
tableToHtmlStr.append("<td colspan='" + colspan + "'");
|
||||
} else { // 正常列
|
||||
tableToHtmlStr.append("<td");
|
||||
}
|
||||
|
||||
int rowspan = getRowspan(table, i, j);
|
||||
if (rowspan > 1) { // 合并的行
|
||||
tableToHtmlStr.append(" rowspan='" + rowspan + "'>");
|
||||
} else {
|
||||
tableToHtmlStr.append(">");
|
||||
}
|
||||
String text = tableCell.getText();
|
||||
tableToHtmlStr.append(text + "</td>");
|
||||
|
||||
}
|
||||
tableToHtmlStr.append("</tr>");
|
||||
}
|
||||
tableToHtmlStr.append("</table>");
|
||||
|
||||
clearTableInfo();
|
||||
|
||||
return tableToHtmlStr.toString();
|
||||
}
|
||||
|
||||
public void clearTableInfo() {
|
||||
// System.out.println(omitCellsList);
|
||||
omitCellsList.clear();
|
||||
}
|
||||
|
||||
// public static void main(String[] args) {
|
||||
// ReadWordTable readWordTable = new com.adc.da.lawss.common.ReadWordTable();
|
||||
//
|
||||
// try (FileInputStream fileInputStream = new FileInputStream("E:\\下载\\table1.docx");
|
||||
// XWPFDocument document = new XWPFDocument(fileInputStream);) {
|
||||
// List<XWPFTable> tables = document.getTables();
|
||||
// for (XWPFTable table : tables) {
|
||||
// System.out.println(readWordTable.readTable(table));
|
||||
// }
|
||||
// } catch (IOException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// }
|
||||
}
|
||||
+517
-517
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,234 @@
|
||||
package com.jero.modules.utils;
|
||||
|
||||
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.CTTcPr;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.math.BigInteger;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Description:
|
||||
* @Author: yangxuenan
|
||||
* date: 2020/2/25 16:58
|
||||
*/
|
||||
public class ReadWordTable {
|
||||
|
||||
|
||||
/**
|
||||
* 保存生成HTML时需要被忽略的单元格
|
||||
*/
|
||||
private List<String> omitCellsList = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* 生成忽略的单元格列表中的格式
|
||||
*
|
||||
* @param row
|
||||
* @param col
|
||||
* @return
|
||||
*/
|
||||
public String generateOmitCellStr(int row, int col) {
|
||||
return row + ":" + col;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前单元格的colspan(列合并)的列数
|
||||
*
|
||||
* @param tcPr 单元格属性
|
||||
* @return
|
||||
*/
|
||||
public int getColspan(CTTcPr tcPr) {
|
||||
// 判断是否存在列合并
|
||||
CTDecimalNumber gridSpan = null;
|
||||
if ((gridSpan = tcPr.getGridSpan()) != null) { // 合并的起始列
|
||||
// 获取合并的列数
|
||||
BigInteger num = gridSpan.getVal();
|
||||
return num.intValue();
|
||||
} else { // 其他被合并的列或正常列
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前单元格的rowspan(行合并)的行数
|
||||
*
|
||||
* @param table 表格
|
||||
* @param row 行值
|
||||
* @param col 列值
|
||||
* @return
|
||||
*/
|
||||
public int getRowspan(XWPFTable table, int row, int col) {
|
||||
|
||||
XWPFTableCell cell = table.getRow(row).getCell(col);
|
||||
// 正常独立单元格
|
||||
if (!isContinueRow(cell) && !isRestartRow(cell)) {
|
||||
return 1;
|
||||
}
|
||||
// 当前单元格的宽度
|
||||
int cellWidth = getCellWidth(table, row, col);
|
||||
// 当前单元格距离左侧边框的距离
|
||||
int leftWidth = getLeftWidth(table, row, col);
|
||||
|
||||
// 用户保存当前单元格行合并的单元格数-1(因为不包含自身)
|
||||
List<Boolean> list = new ArrayList<>();
|
||||
getRowspan(table, row, cellWidth, leftWidth, list);
|
||||
|
||||
return list.size() + 1;
|
||||
}
|
||||
|
||||
private void getRowspan(XWPFTable table, int row, int cellWidth, int leftWidth,
|
||||
List<Boolean> list) {
|
||||
// 已达到最后一行
|
||||
if (row + 1 >= table.getNumberOfRows()) {
|
||||
return;
|
||||
}
|
||||
row = row + 1;
|
||||
int colsNum = table.getRow(row).getTableCells().size();
|
||||
// 因为列合并单元格可能导致行合并的单元格并不在同一列,所以从头遍历列,通过属性、宽度以及距离左边框间距来判断是否是行合并
|
||||
for (int i = 0; i < colsNum; i++) {
|
||||
XWPFTableCell testTable = table.getRow(row).getCell(i);
|
||||
// 是否为合并单元格的中间行(包括结尾行)
|
||||
if (isContinueRow(testTable)) {
|
||||
// 是被上一行单元格合并的单元格
|
||||
if (getCellWidth(table, row, i) == cellWidth
|
||||
&& getLeftWidth(table, row, i) == leftWidth) {
|
||||
list.add(true);
|
||||
// 被合并的单元格在生成html时需要忽略
|
||||
addOmitCell(row, i);
|
||||
// 去下一行继续查找
|
||||
getRowspan(table, row, cellWidth, leftWidth, list);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否是合并行的起始行单元格
|
||||
*
|
||||
* @param tableCell
|
||||
* @return
|
||||
*/
|
||||
public boolean isRestartRow(XWPFTableCell tableCell) {
|
||||
CTTcPr tcPr = tableCell.getCTTc().getTcPr();
|
||||
if (tcPr.getVMerge() == null) {
|
||||
return false;
|
||||
}
|
||||
if (tcPr.getVMerge().getVal() == null) {
|
||||
return false;
|
||||
}
|
||||
if (tcPr.getVMerge().getVal().toString().equalsIgnoreCase("restart")) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否是合并行的中间行单元格(包括结尾的最后一行的单元格)
|
||||
*
|
||||
* @param tableCell
|
||||
* @return
|
||||
*/
|
||||
public boolean isContinueRow(XWPFTableCell tableCell) {
|
||||
CTTcPr tcPr = tableCell.getCTTc().getTcPr();
|
||||
if (tcPr.getVMerge() == null) {
|
||||
return false;
|
||||
}
|
||||
if (tcPr.getVMerge().getVal() == null) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public int getLeftWidth(XWPFTable table, int row, int col) {
|
||||
int leftWidth = 0;
|
||||
for (int i = 0; i < col; i++) {
|
||||
leftWidth += getCellWidth(table, row, i);
|
||||
}
|
||||
return leftWidth;
|
||||
}
|
||||
|
||||
public int getCellWidth(XWPFTable table, int row, int col) {
|
||||
BigInteger width = table.getRow(row).getCell(col).getCTTc().getTcPr().getTcW().getW();
|
||||
return width.intValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加忽略的单元格(被行合并的单元格,生成HTML时需要忽略)
|
||||
*
|
||||
* @param row
|
||||
* @param col
|
||||
*/
|
||||
public void addOmitCell(int row, int col) {
|
||||
String omitCellStr = generateOmitCellStr(row, col);
|
||||
omitCellsList.add(omitCellStr);
|
||||
}
|
||||
|
||||
public boolean isOmitCell(int row, int col) {
|
||||
String cellStr = generateOmitCellStr(row, col);
|
||||
return omitCellsList.contains(cellStr);
|
||||
}
|
||||
|
||||
public String readTable(XWPFTable table) throws IOException {
|
||||
// 表格行数
|
||||
int tableRowsSize = table.getRows().size();
|
||||
StringBuilder tableToHtmlStr = new StringBuilder("<table>");
|
||||
|
||||
for (int i = 0; i < tableRowsSize; i++) {
|
||||
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);
|
||||
if (colspan > 1) { // 合并的列
|
||||
tableToHtmlStr.append("<td colspan='" + colspan + "'");
|
||||
} else { // 正常列
|
||||
tableToHtmlStr.append("<td");
|
||||
}
|
||||
|
||||
int rowspan = getRowspan(table, i, j);
|
||||
if (rowspan > 1) { // 合并的行
|
||||
tableToHtmlStr.append(" rowspan='" + rowspan + "'>");
|
||||
} else {
|
||||
tableToHtmlStr.append(">");
|
||||
}
|
||||
String text = tableCell.getText();
|
||||
tableToHtmlStr.append(text + "</td>");
|
||||
|
||||
}
|
||||
tableToHtmlStr.append("</tr>");
|
||||
}
|
||||
tableToHtmlStr.append("</table>");
|
||||
|
||||
clearTableInfo();
|
||||
|
||||
return tableToHtmlStr.toString();
|
||||
}
|
||||
|
||||
public void clearTableInfo() {
|
||||
// System.out.println(omitCellsList);
|
||||
omitCellsList.clear();
|
||||
}
|
||||
|
||||
// public static void main(String[] args) {
|
||||
// ReadWordTable readWordTable = new com.adc.da.lawss.common.ReadWordTable();
|
||||
//
|
||||
// try (FileInputStream fileInputStream = new FileInputStream("E:\\下载\\table1.docx");
|
||||
// XWPFDocument document = new XWPFDocument(fileInputStream);) {
|
||||
// List<XWPFTable> tables = document.getTables();
|
||||
// for (XWPFTable table : tables) {
|
||||
// System.out.println(readWordTable.readTable(table));
|
||||
// }
|
||||
// } catch (IOException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// }
|
||||
}
|
||||
Reference in New Issue
Block a user