开发者

Format table XWPFTable using Apache Poi

开发者 https://www.devze.com 2023-02-17 10:06 出处:网络
How can I format a table while creating docx file using Apache POI. I want to set the size of the cells, font of the text, color, etc..

How can I format a table while creating docx file using Apache POI. I want to set the size of the cells, font of the text, color, etc..

I found this example, but the cell size is set according the text size.

             XWPFDocument documen开发者_StackOverflow中文版t = new XWPFDocument();  

             // New 2x2 table  

             XWPFTable tableOne = document.createTable();  

             XWPFTableRow tableOneRowOne = tableOne.getRow(0);      
             tableOneRowOne.getCell(0).setText("Hello");  
             tableOneRowOne.addNewTableCell().setText("World");  
             XWPFTableRow tableOneRowTwo = tableOne.createRow();  
             tableOneRowTwo.getCell(0).setText("This is");  
             tableOneRowTwo.getCell(1).setText("a table");  

I want to center the table and increase the cells size.


At the moment, I don't believe that the XWPFTable* classes have direct setters for this. Instead, you'll need to grab the appropriate CT* object from the Table, Row or Cell, and set the properties directly.

A .docx file is simply a zip file of xml files. So, what I'd suggest you do is create two simple word documents. One has a simple table in it, and the other has that table but with the formatting applied. Next, unzip both, and diff them to see what parameters were set to apply the formatting you want.

Finally, get the appropriate CT object and make an appropriate call. eg if the table cell had foo="bar" set on it, you'd do: XWPFTableCell cell = xwpfTable.getRow(0).getCell(0); CTTc rawCell = cell.getCTTc(); rawCell.setFoo("bar");

If you do find the appropriate trick, please consider sending in a patch to add the wrappers on the XWPFTable classes. Just open a bug on the POI bugzilla (https://issues.apache.org/bugzilla/) and upload the patch.


You can place the table to center using : table.setTableAlignment(TableRowAlign.CENTER);

0

精彩评论

暂无评论...
验证码 换一张
取 消