简体   繁体   中英

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 )

// 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; 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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