简体   繁体   English

表没有使用docx4j库在docx中创建

[英]table not getting created in docx using docx4j library

I'm trying to create a table and add it to MainDocumentPart using docx4j library as below but its corrupting the document and it's not opening ( im using MS Word 2010 with docx format ) 我正在尝试使用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"));

R is not a row; R不是一排; it is a run. 这是一次奔跑。

To generate your table, the easiest way is to create a suitable docx in Microsoft Word (or LibreOffice/OpenOffice or whatever), and upload it to the docx4j demo webapp . 要生成表格,最简单的方法是在Microsoft Word(或LibreOffice / OpenOffice或其他)中创建合适的docx,并将其上传到docx4j演示webapp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM