文件:
input.docx (14.3 KB)
table.docx (10.7 KB)
问题一:
public static void main(String[] args) throws Exception {
String filename1 = "D:\\input.docx";
String filename2 = "D:\\output.docx";
Document document = new Document(filename1);
DocumentBuilder documentBuilder = new DocumentBuilder(document);
// 文档指针移动到书签位置
documentBuilder.moveToBookmark("bookmark", true, true);
// 新建表格
documentBuilder.startTable();
documentBuilder.insertCell();
documentBuilder.write("姓名");
documentBuilder.insertCell();
documentBuilder.write("年龄");
// 结束当前行
documentBuilder.endRow();
// 结束表格
documentBuilder.endTable();
document.save(filename2);
}
写入表格报错:Cannot insert a node of this type at this location.
问题二:
public static void main(String[] args) throws Exception {
String filename1 = "D:\\input.docx";
String filename2 = "D:\\output.docx";
String filename3 = "D:\\table.docx";
Document document = new Document(filename1);
Document document3 = new Document(filename3);
Bookmark bookmark = document.getRange().getBookmarks().get("bookmark");
if (bookmark != null) {
bookmark.setText("");
}
DocumentBuilder documentBuilder = new DocumentBuilder(document);
// 文档指针移动到书签位置
documentBuilder.moveToBookmark("bookmark", true, true);
// 新建表格
documentBuilder.insertDocument(document3, ImportFormatMode.KEEP_SOURCE_FORMATTING);
document.save(filename2);
}
插入内容存在表格的文档后,表格格式错误:
output.docx (25.3 KB)

