XWPFHelperTable.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. package platform.common.util.word;
  2. import java.math.BigInteger;
  3. import java.util.ArrayList;
  4. import java.util.Iterator;
  5. import java.util.List;
  6. import org.apache.poi.xwpf.usermodel.BodyElementType;
  7. import org.apache.poi.xwpf.usermodel.IBodyElement;
  8. import org.apache.poi.xwpf.usermodel.XWPFDocument;
  9. import org.apache.poi.xwpf.usermodel.XWPFParagraph;
  10. import org.apache.poi.xwpf.usermodel.XWPFRun;
  11. import org.apache.poi.xwpf.usermodel.XWPFTable;
  12. import org.apache.poi.xwpf.usermodel.XWPFTableCell;
  13. import org.apache.poi.xwpf.usermodel.XWPFTableRow;
  14. import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
  15. /**
  16. * @Description 操作word的基本工具类
  17. * 2018年12月3日 上午11:12:18
  18. * @Author Huangxiaocong
  19. */
  20. public class XWPFHelperTable {
  21. /**
  22. * 删除指定位置的表格,被删除表格后的索引位置
  23. *
  24. * @param document
  25. * @param pos
  26. * @Author Huangxiaocong 2018年12月1日 下午10:32:43
  27. */
  28. public void deleteTableByIndex(XWPFDocument document, int pos) {
  29. Iterator<IBodyElement> bodyElement = document.getBodyElementsIterator();
  30. int eIndex = 0, tableIndex = -1;
  31. while (bodyElement.hasNext()) {
  32. IBodyElement element = bodyElement.next();
  33. BodyElementType elementType = element.getElementType();
  34. if (elementType == BodyElementType.TABLE) {
  35. tableIndex++;
  36. if (tableIndex == pos) {
  37. break;
  38. }
  39. }
  40. eIndex++;
  41. }
  42. document.removeBodyElement(eIndex);
  43. }
  44. /**
  45. * 获得指定位置的表格
  46. *
  47. * @param document
  48. * @param index
  49. * @return
  50. * @Author Huangxiaocong 2018年12月1日 下午10:34:14
  51. */
  52. public XWPFTable getTableByIndex(XWPFDocument document, int index) {
  53. List<XWPFTable> tableList = document.getTables();
  54. if (tableList == null || index < 0 || index > tableList.size()) {
  55. return null;
  56. }
  57. return tableList.get(index);
  58. }
  59. /**
  60. * 得到表格的内容(第一次跨行单元格视为一个,第二次跳过跨行合并的单元格)
  61. *
  62. * @param table
  63. * @return
  64. * @Author Huangxiaocong 2018年12月1日 下午10:46:41
  65. */
  66. public List<List<String>> getTableRConten(XWPFTable table) {
  67. List<List<String>> tableContextList = new ArrayList<List<String>>();
  68. for (int rowIndex = 0, rowLen = table.getNumberOfRows(); rowIndex < rowLen; rowIndex++) {
  69. XWPFTableRow row = table.getRow(rowIndex);
  70. List<String> cellContentList = new ArrayList<String>();
  71. for (int colIndex = 0, colLen = row.getTableCells().size(); colIndex < colLen; colIndex++) {
  72. XWPFTableCell cell = row.getCell(colIndex);
  73. CTTc ctTc = cell.getCTTc();
  74. if (ctTc.isSetTcPr()) {
  75. CTTcPr tcPr = ctTc.getTcPr();
  76. if (tcPr.isSetHMerge()) {
  77. CTHMerge hMerge = tcPr.getHMerge();
  78. if (STMerge.RESTART.equals(hMerge.getVal())) {
  79. cellContentList.add(getTableCellContent(cell));
  80. }
  81. } else if (tcPr.isSetVMerge()) {
  82. CTVMerge vMerge = tcPr.getVMerge();
  83. if (STMerge.RESTART.equals(vMerge.getVal())) {
  84. cellContentList.add(getTableCellContent(cell));
  85. }
  86. } else {
  87. cellContentList.add(getTableCellContent(cell));
  88. }
  89. }
  90. }
  91. tableContextList.add(cellContentList);
  92. }
  93. return tableContextList;
  94. }
  95. /**
  96. * 获得一个表格的单元格的内容
  97. *
  98. * @param cell
  99. * @return
  100. * @Author Huangxiaocong 2018年12月2日 下午7:39:23
  101. */
  102. public String getTableCellContent(XWPFTableCell cell) {
  103. StringBuffer sb = new StringBuffer();
  104. List<XWPFParagraph> cellParagList = cell.getParagraphs();
  105. if (cellParagList != null && cellParagList.size() > 0) {
  106. for (XWPFParagraph xwpfPr : cellParagList) {
  107. List<XWPFRun> runs = xwpfPr.getRuns();
  108. if (runs != null && runs.size() > 0) {
  109. for (XWPFRun xwpfRun : runs) {
  110. sb.append(xwpfRun.getText(0));
  111. }
  112. }
  113. }
  114. }
  115. return sb.toString();
  116. }
  117. /**
  118. * 得到表格内容,合并后的单元格视为一个单元格
  119. *
  120. * @param table
  121. * @return
  122. * @Author Huangxiaocong 2018年12月2日 下午7:47:19
  123. */
  124. public List<List<String>> getTableContent(XWPFTable table) {
  125. List<List<String>> tableContentList = new ArrayList<List<String>>();
  126. for (int rowIndex = 0, rowLen = table.getNumberOfRows(); rowIndex < rowLen; rowIndex++) {
  127. XWPFTableRow row = table.getRow(rowIndex);
  128. List<String> cellContentList = new ArrayList<String>();
  129. for (int colIndex = 0, colLen = row.getTableCells().size(); colIndex < colLen; colIndex++) {
  130. XWPFTableCell cell = row.getCell(colIndex);
  131. cellContentList.add(getTableCellContent(cell));
  132. }
  133. tableContentList.add(cellContentList);
  134. }
  135. return tableContentList;
  136. }
  137. /**
  138. * 跨列合并
  139. *
  140. * @param table
  141. * @param row 所合并的行
  142. * @param fromCell 起始列
  143. * @param toCell 终止列
  144. * @Description
  145. * @Author Huangxiaocong 2018年11月26日 下午9:23:22
  146. */
  147. public void mergeCellsHorizontal(XWPFTable table, int row, int fromCell, int toCell) {
  148. for (int cellIndex = fromCell; cellIndex <= toCell; cellIndex++) {
  149. XWPFTableCell cell = table.getRow(row).getCell(cellIndex);
  150. if (cellIndex == fromCell) {
  151. cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
  152. } else {
  153. cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
  154. }
  155. }
  156. }
  157. /**
  158. * 跨行合并
  159. *
  160. * @param table
  161. * @param col 合并的列
  162. * @param fromRow 起始行
  163. * @param toRow 终止行
  164. * @Description
  165. * @Author Huangxiaocong 2018年11月26日 下午9:09:19
  166. */
  167. public void mergeCellsVertically(XWPFTable table, int col, int fromRow, int toRow) {
  168. for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) {
  169. XWPFTableCell cell = table.getRow(rowIndex).getCell(col);
  170. //第一个合并单元格用重启合并值设置
  171. if (rowIndex == fromRow) {
  172. cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);
  173. } else {
  174. //合并第一个单元格的单元被设置为“继续”
  175. cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);
  176. }
  177. }
  178. }
  179. /**
  180. * @Author: huZhiHao
  181. * @Description: 将制定各自上下表格线隐藏 非合并
  182. * @Date: 2020/3/18
  183. * @Params: [table, col, fromRow, toRow]
  184. * @Return: void
  185. **/
  186. public void mergeCellsBordersVertically(XWPFTable table, int col, int fromRow, int toRow) {
  187. for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) {
  188. List<XWPFTableCell> tableCellList = table.getRow(rowIndex).getTableCells();
  189. for (XWPFTableCell tableCell : tableCellList) {
  190. CTTcBorders tblBorders = tableCell.getCTTc().addNewTcPr().addNewTcBorders();
  191. if (rowIndex == fromRow) {
  192. tblBorders.addNewBottom().setVal(STBorder.NIL);
  193. table.getRow(rowIndex).getCell(col).getCTTc().addNewTcPr().setTcBorders(tblBorders);
  194. } else if (rowIndex == toRow) {
  195. tblBorders.addNewTop().setVal(STBorder.NIL);
  196. table.getRow(rowIndex).getCell(col).getCTTc().addNewTcPr().setTcBorders(tblBorders);
  197. } else {
  198. tblBorders.addNewTop().setVal(STBorder.NIL);
  199. tblBorders.addNewBottom().setVal(STBorder.NIL);
  200. table.getRow(rowIndex).getCell(col).getCTTc().addNewTcPr().setTcBorders(tblBorders);
  201. }
  202. }
  203. }
  204. }
  205. /**
  206. * @Description: 创建表格, 创建后表格至少有1行1列, 设置列宽
  207. */
  208. public XWPFTable createTable(XWPFDocument xdoc, int rowSize, int cellSize,
  209. boolean isSetColWidth, int[] colWidths) {
  210. XWPFTable table = xdoc.createTable(rowSize, cellSize);
  211. if (isSetColWidth) {
  212. CTTbl ttbl = table.getCTTbl();
  213. CTTblGrid tblGrid = ttbl.addNewTblGrid();
  214. for (int j = 0, len = Math.min(cellSize, colWidths.length); j < len; j++) {
  215. CTTblGridCol gridCol = tblGrid.addNewGridCol();
  216. gridCol.setW(new BigInteger(String.valueOf(colWidths[j])));
  217. }
  218. }
  219. return table;
  220. }
  221. /**
  222. * @Description: 设置表格总宽度与水平对齐方式
  223. */
  224. public void setTableWidthAndHAlign(XWPFTable table, String width,
  225. STJc.Enum enumValue) {
  226. CTTblPr tblPr = getTableCTTblPr(table);
  227. // 表格宽度
  228. CTTblWidth tblWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr.addNewTblW();
  229. if (enumValue != null) {
  230. CTJc cTJc = tblPr.addNewJc();
  231. cTJc.setVal(enumValue);
  232. }
  233. // 设置宽度
  234. tblWidth.setW(new BigInteger(width));
  235. tblWidth.setType(STTblWidth.DXA);
  236. }
  237. /**
  238. * @Description: 得到Table的CTTblPr, 不存在则新建
  239. */
  240. public CTTblPr getTableCTTblPr(XWPFTable table) {
  241. CTTbl ttbl = table.getCTTbl();
  242. // 表格属性
  243. CTTblPr tblPr = ttbl.getTblPr() == null ? ttbl.addNewTblPr() : ttbl.getTblPr();
  244. return tblPr;
  245. }
  246. /**
  247. * 设置表格行高
  248. *
  249. * @param infoTable
  250. * @param heigth 高度
  251. * @param vertical 表格内容的显示方式:居中、靠右...
  252. * @Author Huangxiaocong 2018年12月16日
  253. */
  254. public void setTableHeight(XWPFTable infoTable, int heigth, STVerticalJc.Enum vertical) {
  255. List<XWPFTableRow> rows = infoTable.getRows();
  256. for (XWPFTableRow row : rows) {
  257. CTTrPr trPr = row.getCtRow().addNewTrPr();
  258. CTHeight ht = trPr.addNewTrHeight();
  259. ht.setVal(BigInteger.valueOf(heigth));
  260. List<XWPFTableCell> cells = row.getTableCells();
  261. for (XWPFTableCell tableCell : cells) {
  262. CTTcPr cttcpr = tableCell.getCTTc().addNewTcPr();
  263. cttcpr.addNewVAlign().setVal(vertical);
  264. }
  265. }
  266. }
  267. /**
  268. * @Author: huZhiHao
  269. * @Description: 设置列宽
  270. * @Date: 2020/3/30
  271. * @Params: [infoTable, colWidths]
  272. * @Return: void
  273. **/
  274. public void setTableWidth(XWPFTable infoTable, int[] colWidths) {
  275. List<XWPFTableRow> rows = infoTable.getRows();
  276. for (XWPFTableRow row : rows) {
  277. List<XWPFTableCell> cells = row.getTableCells();
  278. for (int j = 0; j < cells.size(); j++) {
  279. CTTcPr cttcpr = cells.get(j).getCTTc().addNewTcPr();
  280. CTTblWidth cellw = cttcpr.addNewTcW();
  281. cellw.setType(STTblWidth.DXA);
  282. cellw.setW(BigInteger.valueOf(colWidths[j]));
  283. }
  284. }
  285. }
  286. public void setTableGridCol(XWPFTable table, int[] colWidths) {
  287. CTTbl ttbl = table.getCTTbl();
  288. CTTblGrid tblGrid = ttbl.getTblGrid() != null ? ttbl.getTblGrid()
  289. : ttbl.addNewTblGrid();
  290. for (int j = 0, len = colWidths.length; j < len; j++) {
  291. CTTblGridCol gridCol = tblGrid.addNewGridCol();
  292. gridCol.setW(new BigInteger(String.valueOf(colWidths[j])));
  293. }
  294. }
  295. }