如下图:一个table表格,需要作为表格插入到word中;
1、首先对表格做拆分处理
代码如下:
private String simplifyTable(String tableContent) { if(StringUtils.isEmpty(tableContent)) return null; Document tableDoc = Jsoup.parse(tableContent); Elements trElements = tableDoc.getElementsByTag("tr"); if(trElements !=null){ Iterator<Element> eleIterator = trElements.iterator(); Integer rowNum = 0; // 针对于colspan操作 while(eleIterator.hasNext()){ rowNum ++; Element trElement = eleIterator.next(); //去除所有样式 trElement.removeAttr("class"); Elements tdElements = trElement.getElementsByTag("td"); List<Element> tdEleList = covertElements2List(tdElements); for(int i=0;i<tdEleList.size();i++){ Element curTdElement = tdEleList.get(i); //去除所有样式 curTdElement.removeAttr("class"); Element ele = curTdElement.clone(); String colspanValStr = curTdElement.attr("colspan"); if(!StringUtils.isEmpty(colspanValStr)){ ele.removeAttr("colspan"); Integer colspanVal = Integer.parseInt(colspanValStr); for(int k=0;k<colspanVal-1;k++){ curTdElement.after(ele.outerHtml()); } } } } // 针对于rowspan操作 List<Element> trEleList = covertElements2List(trElements); Element firstTrEle = trElements.first(); Elements tdElements = firstTrEle.getElementsByTag("td"); Integer tdCount = tdElements.size(); for(int i=0;i<tdElements.size();i++){ //获取该列下所有单元格 for(Element trElement:trEleList){ List<Element> tdElementList = covertElements2List(trElement.getElementsByTag("td")); try{ tdElementList.get(i); }catch(Exception e){ continue; } Node curTdNode = tdElementList.get(i); Node cNode = curTdNode.clone(); String rowspanValStr = curTdNode.attr("rowspan"); if(!StringUtils.isEmpty(rowspanValStr)){ cNode.removeAttr("rowspan"); Element nextTrElement = trElement.nextElementSibling(); Integer rowspanVal = Integer.parseInt(rowspanValStr); for(int j=0;j<rowspanVal-1;j++){ Node tempNode = cNode.clone(); List<Node> nodeList = new ArrayList<Node>(); nodeList.add(tempNode); if(j > 0) nextTrElement = nextTrElement.nextElementSibling(); Integer indexNum = i+1; if(i == 0) indexNum = 0; if(indexNum == tdCount) nextTrElement.appendChild(tempNode); else nextTrElement.insertChildren(indexNum,nodeList); } } } } } Element tableEle = tableDoc.getElementsByTag("table").first(); String tableHtml = tableEle.outerHtml(); return tableHtml; } private static List<Element> covertElements2List(Elements curElements){ List<Element> elementList = new ArrayList<Element>(); Iterator<Element> eleIterator = curElements.iterator(); while(eleIterator.hasNext()){ Element curlement = eleIterator.next(); elementList.add(curlement); } return elementList; }
处理后的表格会稍有变形,如下图:
2、将处理后的表格写入word
String tableHtmlStr = ele.outerHtml(); System.out.println(tableHtmlStr); String simpleTableHtml = simplifyTable(tableHtmlStr); System.out.println(simpleTableHtml); Document tableDoc = Jsoup.parse(simpleTableHtml); Elements trList = tableDoc.getElementsByTag("tr"); Elements tdList = trList.get(0).getElementsByTag("td"); XWPFTable xwpfTable = document.createTable(trList.size(),tdList.size()); Map<String,Boolean>[][] array = new Map[trList.size()][tdList.size()]; for (int row = 0; row < trList.size(); row++) { Element trElement = trList.get(row); Elements tds = trElement.getElementsByTag("td"); for(int col = 0; col < tds.size(); col++) { Element colElement = tds.get(col); String colspan = colElement.attr("colspan"); String rowspan = colElement.attr("rowspan"); String style = colElement.attr("style"); StringBuilder styleSB = new StringBuilder(); if(!StringUtils.isEmpty(colspan)){ int colCount = Integer.parseInt(colspan); for(int i=0;i<colCount-1;i++){ array[row][col+i+1] = new HashMap<String, Boolean>(); array[row][col+i+1].put("mergeCol", true); } } if(!StringUtils.isEmpty(rowspan)){ int rowCount = Integer.parseInt(rowspan); for(int i=0;i<rowCount-1;i++){ array[row+i+1][col] = new HashMap<String, Boolean>(); array[row+i+1][col].put("mergeRow", true); } } XWPFTableCell tableCell = xwpfTable.getRow(row).getCell(col); if(StringUtils.isEmpty(colspan)){ if(col == 0){ if(tableCell.getCTTc().getTcPr() == null){ tableCell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART); }else{ if(tableCell.getCTTc().getTcPr().getHMerge() == null){ tableCell.getCTTc().getTcPr().addNewHMerge().setVal(STMerge.RESTART); }else{ tableCell.getCTTc().getTcPr().getHMerge().setVal(STMerge.RESTART); } } }else{ if(array[row][col]!=null && array[row][col].get("mergeCol")!=null && array[row][col].get("mergeCol")){ if(tableCell.getCTTc().getTcPr() == null){ tableCell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE); }else{ if(tableCell.getCTTc().getTcPr().getHMerge() == null){ tableCell.getCTTc().getTcPr().addNewHMerge().setVal(STMerge.CONTINUE); }else{ tableCell.getCTTc().getTcPr().getHMerge().setVal(STMerge.CONTINUE); } } continue; }else{ if(tableCell.getCTTc().getTcPr() == null){ tableCell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART); }else{ if(tableCell.getCTTc().getTcPr().getHMerge() == null){ tableCell.getCTTc().getTcPr().addNewHMerge().setVal(STMerge.RESTART); }else{ tableCell.getCTTc().getTcPr().getHMerge().setVal(STMerge.RESTART); } } } } }else{ if(tableCell.getCTTc().getTcPr() == null){ tableCell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART); }else{ if(tableCell.getCTTc().getTcPr().getHMerge() == null){ tableCell.getCTTc().getTcPr().addNewHMerge().setVal(STMerge.RESTART); }else{ tableCell.getCTTc().getTcPr().getHMerge().setVal(STMerge.RESTART); } } } if(StringUtils.isEmpty(rowspan)){ if(array[row][col]!=null && array[row][col].get("mergeRow")!=null && array[row][col].get("mergeRow")){ if(tableCell.getCTTc().getTcPr() == null){ tableCell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE); }else{ if(tableCell.getCTTc().getTcPr().getVMerge() == null){ tableCell.getCTTc().getTcPr().addNewVMerge().setVal(STMerge.CONTINUE); }else{ tableCell.getCTTc().getTcPr().getVMerge().setVal(STMerge.CONTINUE); } } continue; }else{ if(tableCell.getCTTc().getTcPr() == null){ tableCell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART); }else{ if(tableCell.getCTTc().getTcPr().getVMerge() == null){ tableCell.getCTTc().getTcPr().addNewVMerge().setVal(STMerge.RESTART); }else{ tableCell.getCTTc().getTcPr().getVMerge().setVal(STMerge.RESTART); } } } }else{ if(tableCell.getCTTc().getTcPr() == null){ tableCell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART); }else{ if(tableCell.getCTTc().getTcPr().getVMerge() == null){ tableCell.getCTTc().getTcPr().addNewVMerge().setVal(STMerge.RESTART); }else{ tableCell.getCTTc().getTcPr().getVMerge().setVal(STMerge.RESTART); } } } tableCell.removeParagraph(0); XWPFParagraph paragraph = tableCell.addParagraph(); paragraph.setStyle(styleSB.toString()); if(!StringUtils.isEmpty(style) && style.contains("text-align:center")){ paragraph.setAlignment(ParagraphAlignment.CENTER); } XWPFRun run = paragraph.createRun(); run.setText(colElement.text()); } }
写入word后表格如下图:
注:上述内容处理的表格为html代码的table表格
时间: 2024-11-06 03:51:10