我正在尝试使用 docx4j 库创建一个表并将其添加到 MainDocumentPart,如下所示,但它破坏了文档并且它没有打开(我使用的是带有 docx 格式的 MS Word 2010)
// creating docx
WordprocessingMLPackage wordPackage = WordprocessingMLPackage.createPackage();
// creating a table
// use ObjectFactory for creating the table, table row and table cell
ObjectFactory factory = Context.getWmlObjectFactory();
// create the table
Tbl table = factory.createTbl();
// create a row
R row = factory.createR();
// create a cell
Tc cell = factory.createTc();
// add the content to cell
cell.getContent().add(wordPackage.getMainDocumentPart().createParagraphOfText("table cell data added"));
// add the cell to row
row.getContent().add(cell);
// add the row to table
table.getContent().add(row);
// adding the table to main document part
wordPackage.getMainDocumentPart().addObject(table);
wordPackage.save(new File("D:\\Programs\\test\\Doc222.docx"));