如何在Apache POI中格式化XWPFTable中的单元格 [英] How to format cell in XWPFTable in Apache POI
问题描述
我能够在Apache POI中创建一个表格,我的表格中有适当的值,但我想要的是我想减少表格中列大小的单元格大小如何做,请帮助..以下是我所做的事情
I am able to create a table in Apache POI in word ,my table is coming with proper value but what i want i want to decrease the cell size of the column size in the table how to do that ,please help..Here is what i have done upto
XWPFTable table = document.createTable(5,3);
r3.setText("MSH Activity Score Card");
r3.setBold(true);
//creating first row
table.getRow(0).getCell(1).setText("Job ID#");
table.getRow(0).getCell(2).setText("1362");
//creating second row
table.getRow(1).getCell(1).setText("Job Title#");
table.getRow(1).getCell(2).setText("Global Network Architect Consultant");
//creating third row
table.getRow(2).getCell(1).setText("Client");
table.getRow(2).getCell(2).setText("Carnival Corporation");
//creating fourth row
table.getRow(3).getCell(1).setText("Start Date");
table.getRow(3).getCell(2).setText("11/13/2014");
//creating fifth row
table.getRow(4).getCell(1).setText("Days Old");
table.getRow(4).getCell(2).setText("33");
CTTblWidth width = table.getCTTbl().addNewTblPr().addNewTblW();
width.setType(STTblWidth.DXA);
width.setW(BigInteger.valueOf(1500));
我正在获取一个包含数据的表,但我希望最小化
I am getting a table with data but i want to minimize the size
但是我想要这个尺码
推荐答案
首先你需要确保你已经声明了库的导入:
First you need to make sure that you've declared the library's imports :
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
然后它就像假设你要格式化 cell-0
on row-0
:
Then it's just like , assuming you want to format cell-0
on row-0
:
table.getRow(0).getCell(0).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2000));
或者在创建最后一行后设置此片段以格式化所有单元格:
Or put this snippet after creating your last row to format all cells:
for(int x = 0;x < table.getNumberOfRows(); x++){
XWPFTableRow row = table.getRow(x);
int numberOfCell = row.getTableCells().size();
for(int y = 0; y < numberOfCell ; y++){
XWPFTableCell cell = row.getCell(y);
cell.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2000));
}
}
*但请记住,单元格的宽度始终为增加以跟随字符串的长度。
*But remember that the width of the cell will always increase to follow the length of the string.
这篇关于如何在Apache POI中格式化XWPFTable中的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!