/**
* 新建子表的方法
* @param tableRow 表格的行
* @param XapiKey 无用字段
* @param XapiKeySon 子表的主键
*/
public void newChildTable(XWPFTableRow tableRow,String XapiKey,String XapiKeySon){
XWPFParagraph cellPara = tableRow.getCell(0).getParagraphArray(0); //row.getCell(j)指的是外部表格的单元格
cellPara.setAlignment(ParagraphAlignment.CENTER);
List<FieldTable> fieldTablesSon = fieldService.getFieldTableByXapiKey(XapiKeySon);
XObjects xObjects = xObjectsService.selectXObjectsLax(XapiKeySon);
//子表名字,以及新创建的子表
if(xObjects == null){
cellPara.createRun().setText(XapiKeySon);
}else {
cellPara.createRun().setText(xObjects.getLabel());
}
//在此游标处插入新表格
XWPFTable cellTable = tableRow.getCell(0).insertNewTbl(cellPara.getCTP().newCursor());
//这个是在子表新建表格外框的
cellTable.getCTTbl().addNewTblPr().addNewTblBorders().addNewLeft()
.setVal(org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder.SINGLE);
cellTable.getCTTbl().getTblPr().getTblBorders().addNewRight()
.setVal(org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder.SINGLE);
cellTable.getCTTbl().getTblPr().getTblBorders().addNewTop()
.setVal(org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder.SINGLE);
cellTable.getCTTbl().getTblPr().getTblBorders().addNewBottom()
.setVal(org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder.SINGLE);
cellTable.getCTTbl().getTblPr().getTblBorders().addNewInsideH()
.setVal(org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder.SINGLE);
cellTable.getCTTbl().getTblPr().getTblBorders().addNewInsideV()
.setVal(org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder.SINGLE);
cellTable.getCTTbl().getTblPr().addNewJc().setVal(STJc.CENTER); // 内部表格水平居中
tableRow.getCell(0).setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
//在新建好的表格出新建行
XWPFTableRow cellRow_Header = cellTable.createRow();
fillCellValueHeader(cellRow_Header,"字段名称",0,false,true);
fillCellValueHeader(cellRow_Header,"apiKey",1,false,true);
fillCellValueHeader(cellRow_Header,"字段名称",2,false,true);
fillCellValueHeader(cellRow_Header,"apiKey",3,false,true);
}
上面就是在表格单元格新建表的相关方法,代码有注释