| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- package platform.common.util.word;
- import java.math.BigInteger;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- import org.apache.poi.xwpf.usermodel.BodyElementType;
- import org.apache.poi.xwpf.usermodel.IBodyElement;
- 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 org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
- /**
- * @Description 操作word的基本工具类
- * 2018年12月3日 上午11:12:18
- * @Author Huangxiaocong
- */
- public class XWPFHelperTable {
- /**
- * 删除指定位置的表格,被删除表格后的索引位置
- *
- * @param document
- * @param pos
- * @Author Huangxiaocong 2018年12月1日 下午10:32:43
- */
- public void deleteTableByIndex(XWPFDocument document, int pos) {
- Iterator<IBodyElement> bodyElement = document.getBodyElementsIterator();
- int eIndex = 0, tableIndex = -1;
- while (bodyElement.hasNext()) {
- IBodyElement element = bodyElement.next();
- BodyElementType elementType = element.getElementType();
- if (elementType == BodyElementType.TABLE) {
- tableIndex++;
- if (tableIndex == pos) {
- break;
- }
- }
- eIndex++;
- }
- document.removeBodyElement(eIndex);
- }
- /**
- * 获得指定位置的表格
- *
- * @param document
- * @param index
- * @return
- * @Author Huangxiaocong 2018年12月1日 下午10:34:14
- */
- public XWPFTable getTableByIndex(XWPFDocument document, int index) {
- List<XWPFTable> tableList = document.getTables();
- if (tableList == null || index < 0 || index > tableList.size()) {
- return null;
- }
- return tableList.get(index);
- }
- /**
- * 得到表格的内容(第一次跨行单元格视为一个,第二次跳过跨行合并的单元格)
- *
- * @param table
- * @return
- * @Author Huangxiaocong 2018年12月1日 下午10:46:41
- */
- public List<List<String>> getTableRConten(XWPFTable table) {
- List<List<String>> tableContextList = new ArrayList<List<String>>();
- for (int rowIndex = 0, rowLen = table.getNumberOfRows(); rowIndex < rowLen; rowIndex++) {
- XWPFTableRow row = table.getRow(rowIndex);
- List<String> cellContentList = new ArrayList<String>();
- for (int colIndex = 0, colLen = row.getTableCells().size(); colIndex < colLen; colIndex++) {
- XWPFTableCell cell = row.getCell(colIndex);
- CTTc ctTc = cell.getCTTc();
- if (ctTc.isSetTcPr()) {
- CTTcPr tcPr = ctTc.getTcPr();
- if (tcPr.isSetHMerge()) {
- CTHMerge hMerge = tcPr.getHMerge();
- if (STMerge.RESTART.equals(hMerge.getVal())) {
- cellContentList.add(getTableCellContent(cell));
- }
- } else if (tcPr.isSetVMerge()) {
- CTVMerge vMerge = tcPr.getVMerge();
- if (STMerge.RESTART.equals(vMerge.getVal())) {
- cellContentList.add(getTableCellContent(cell));
- }
- } else {
- cellContentList.add(getTableCellContent(cell));
- }
- }
- }
- tableContextList.add(cellContentList);
- }
- return tableContextList;
- }
- /**
- * 获得一个表格的单元格的内容
- *
- * @param cell
- * @return
- * @Author Huangxiaocong 2018年12月2日 下午7:39:23
- */
- public String getTableCellContent(XWPFTableCell cell) {
- StringBuffer sb = new StringBuffer();
- List<XWPFParagraph> cellParagList = cell.getParagraphs();
- if (cellParagList != null && cellParagList.size() > 0) {
- for (XWPFParagraph xwpfPr : cellParagList) {
- List<XWPFRun> runs = xwpfPr.getRuns();
- if (runs != null && runs.size() > 0) {
- for (XWPFRun xwpfRun : runs) {
- sb.append(xwpfRun.getText(0));
- }
- }
- }
- }
- return sb.toString();
- }
- /**
- * 得到表格内容,合并后的单元格视为一个单元格
- *
- * @param table
- * @return
- * @Author Huangxiaocong 2018年12月2日 下午7:47:19
- */
- public List<List<String>> getTableContent(XWPFTable table) {
- List<List<String>> tableContentList = new ArrayList<List<String>>();
- for (int rowIndex = 0, rowLen = table.getNumberOfRows(); rowIndex < rowLen; rowIndex++) {
- XWPFTableRow row = table.getRow(rowIndex);
- List<String> cellContentList = new ArrayList<String>();
- for (int colIndex = 0, colLen = row.getTableCells().size(); colIndex < colLen; colIndex++) {
- XWPFTableCell cell = row.getCell(colIndex);
- cellContentList.add(getTableCellContent(cell));
- }
- tableContentList.add(cellContentList);
- }
- return tableContentList;
- }
- /**
- * 跨列合并
- *
- * @param table
- * @param row 所合并的行
- * @param fromCell 起始列
- * @param toCell 终止列
- * @Description
- * @Author Huangxiaocong 2018年11月26日 下午9:23:22
- */
- public void mergeCellsHorizontal(XWPFTable table, int row, int fromCell, int toCell) {
- for (int cellIndex = fromCell; cellIndex <= toCell; cellIndex++) {
- XWPFTableCell cell = table.getRow(row).getCell(cellIndex);
- if (cellIndex == fromCell) {
- cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
- } else {
- cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
- }
- }
- }
- /**
- * 跨行合并
- *
- * @param table
- * @param col 合并的列
- * @param fromRow 起始行
- * @param toRow 终止行
- * @Description
- * @Author Huangxiaocong 2018年11月26日 下午9:09:19
- */
- public void mergeCellsVertically(XWPFTable table, int col, int fromRow, int toRow) {
- for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) {
- XWPFTableCell cell = table.getRow(rowIndex).getCell(col);
- //第一个合并单元格用重启合并值设置
- if (rowIndex == fromRow) {
- cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);
- } else {
- //合并第一个单元格的单元被设置为“继续”
- cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);
- }
- }
- }
- /**
- * @Author: huZhiHao
- * @Description: 将制定各自上下表格线隐藏 非合并
- * @Date: 2020/3/18
- * @Params: [table, col, fromRow, toRow]
- * @Return: void
- **/
- public void mergeCellsBordersVertically(XWPFTable table, int col, int fromRow, int toRow) {
- for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) {
- List<XWPFTableCell> tableCellList = table.getRow(rowIndex).getTableCells();
- for (XWPFTableCell tableCell : tableCellList) {
- CTTcBorders tblBorders = tableCell.getCTTc().addNewTcPr().addNewTcBorders();
- if (rowIndex == fromRow) {
- tblBorders.addNewBottom().setVal(STBorder.NIL);
- table.getRow(rowIndex).getCell(col).getCTTc().addNewTcPr().setTcBorders(tblBorders);
- } else if (rowIndex == toRow) {
- tblBorders.addNewTop().setVal(STBorder.NIL);
- table.getRow(rowIndex).getCell(col).getCTTc().addNewTcPr().setTcBorders(tblBorders);
- } else {
- tblBorders.addNewTop().setVal(STBorder.NIL);
- tblBorders.addNewBottom().setVal(STBorder.NIL);
- table.getRow(rowIndex).getCell(col).getCTTc().addNewTcPr().setTcBorders(tblBorders);
- }
- }
- }
- }
- /**
- * @Description: 创建表格, 创建后表格至少有1行1列, 设置列宽
- */
- public XWPFTable createTable(XWPFDocument xdoc, int rowSize, int cellSize,
- boolean isSetColWidth, int[] colWidths) {
- XWPFTable table = xdoc.createTable(rowSize, cellSize);
- if (isSetColWidth) {
- CTTbl ttbl = table.getCTTbl();
- CTTblGrid tblGrid = ttbl.addNewTblGrid();
- for (int j = 0, len = Math.min(cellSize, colWidths.length); j < len; j++) {
- CTTblGridCol gridCol = tblGrid.addNewGridCol();
- gridCol.setW(new BigInteger(String.valueOf(colWidths[j])));
- }
- }
- return table;
- }
- /**
- * @Description: 设置表格总宽度与水平对齐方式
- */
- public void setTableWidthAndHAlign(XWPFTable table, String width,
- STJc.Enum enumValue) {
- CTTblPr tblPr = getTableCTTblPr(table);
- // 表格宽度
- CTTblWidth tblWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr.addNewTblW();
- if (enumValue != null) {
- CTJc cTJc = tblPr.addNewJc();
- cTJc.setVal(enumValue);
- }
- // 设置宽度
- tblWidth.setW(new BigInteger(width));
- tblWidth.setType(STTblWidth.DXA);
- }
- /**
- * @Description: 得到Table的CTTblPr, 不存在则新建
- */
- public CTTblPr getTableCTTblPr(XWPFTable table) {
- CTTbl ttbl = table.getCTTbl();
- // 表格属性
- CTTblPr tblPr = ttbl.getTblPr() == null ? ttbl.addNewTblPr() : ttbl.getTblPr();
- return tblPr;
- }
- /**
- * 设置表格行高
- *
- * @param infoTable
- * @param heigth 高度
- * @param vertical 表格内容的显示方式:居中、靠右...
- * @Author Huangxiaocong 2018年12月16日
- */
- public void setTableHeight(XWPFTable infoTable, int heigth, STVerticalJc.Enum vertical) {
- List<XWPFTableRow> rows = infoTable.getRows();
- for (XWPFTableRow row : rows) {
- CTTrPr trPr = row.getCtRow().addNewTrPr();
- CTHeight ht = trPr.addNewTrHeight();
- ht.setVal(BigInteger.valueOf(heigth));
- List<XWPFTableCell> cells = row.getTableCells();
- for (XWPFTableCell tableCell : cells) {
- CTTcPr cttcpr = tableCell.getCTTc().addNewTcPr();
- cttcpr.addNewVAlign().setVal(vertical);
- }
- }
- }
- /**
- * @Author: huZhiHao
- * @Description: 设置列宽
- * @Date: 2020/3/30
- * @Params: [infoTable, colWidths]
- * @Return: void
- **/
- public void setTableWidth(XWPFTable infoTable, int[] colWidths) {
- List<XWPFTableRow> rows = infoTable.getRows();
- for (XWPFTableRow row : rows) {
- List<XWPFTableCell> cells = row.getTableCells();
- for (int j = 0; j < cells.size(); j++) {
- CTTcPr cttcpr = cells.get(j).getCTTc().addNewTcPr();
- CTTblWidth cellw = cttcpr.addNewTcW();
- cellw.setType(STTblWidth.DXA);
- cellw.setW(BigInteger.valueOf(colWidths[j]));
- }
- }
- }
- public void setTableGridCol(XWPFTable table, int[] colWidths) {
- CTTbl ttbl = table.getCTTbl();
- CTTblGrid tblGrid = ttbl.getTblGrid() != null ? ttbl.getTblGrid()
- : ttbl.addNewTblGrid();
- for (int j = 0, len = colWidths.length; j < len; j++) {
- CTTblGridCol gridCol = tblGrid.addNewGridCol();
- gridCol.setW(new BigInteger(String.valueOf(colWidths[j])));
- }
- }
- }
|