利用书签插入表格和图片 插入table
@Test
public void testCreatTable() throws Exception {
Document doc = new Document("D:\\test.doc");
DocumentBuilder builder = new DocumentBuilder(doc);
BookmarkCollection bookmarks = doc.getRange().getBookmarks();
for (Bookmark bookmark : bookmarks) {
switch (bookmark.getName()) {
case "table1":
createTable(builder, bookmarks, bookmark);
}
}
doc.save("D:\\test1.doc");
}
void createTable(DocumentBuilder builder, BookmarkCollection bookmarks, Bookmark bookmark) throws Exception {
builder.moveToBookmark(bookmark.getName());
builder.startTable();
builder.insertCell();
builder.write("日期");
builder.insertCell();
builder.write("数量");
builder.endRow();
builder.insertCell();
builder.write("2022-10-1");
builder.insertCell();
builder.write("100");
builder.endRow();
builder.endTable();
bookmarks.remove(bookmark);
}
|