通过已有模板进行进行动态行,aspose版本为21.6
问题,使用insertAfter插入新行后,没有与原表格合并,而是形成了新的表格,这明显不符合要求,最后使用克隆行的方法进行插入,与原表格完美融合
主要代码如下:
private void replaceExpertInfoNew(List<ErisNewExpertInfo> expertList, Document doc) {
Bookmark realName = doc.getRange().getBookmarks().get("realName");
//书签->-单元格->行->表格
Row row = (Row) realName.getBookmarkStart().getParentNode().getParentNode().getParentNode();
Table table = (Table) row.getParentNode();
for (ErisNewExpertInfo expert : expertList) {
Row nextRow = (Row) row.deepClone(true);
插入一行
table.insertAfter(nextRow, row);
CellCollection cells = nextRow.getCells();
int index = 0;
for (Cell cell : cells) {
cell.removeAllChildren();
Paragraph paragraph = new Paragraph(doc);
cell.appendChild(paragraph);
switch (index) {
case 0:
paragraph.appendChild(new Run(doc, expert.getRealName()));
break;
case 1:
paragraph.appendChild(new Run(doc, expert.getWorkUnit()));
break;
case 2:
paragraph.appendChild(new Run(doc, expert.getDepartment()));
break;
case 3:
paragraph.appendChild(new Run(doc, expert.getPosition()));
break;
case 4:
paragraph.appendChild(new Run(doc, expert.getPhone()));
break;
case 5:
paragraph.appendChild(new Run(doc, expert.getEmail()));
break;
case 6:
paragraph.appendChild(new Run(doc, ExpertTypeStateEnum.getEnum(expert.getExpertTypeState()).getDescription()));
break;
default:
break;
}
index++;
}
}
}
表格模板如图:
说明:模板中动态插入专家信息