如何更改Java中的Apache POI中的XWPFTableCell页边距?
问题描述:
我试图寻找太多,但没有得到所需的结果。我正在使用Apache POI将特定的ata从excel工作表复制到使用XWPF的ms word 2010文件中的表中。我已经完成了。我想要做的最后一件事是为每个单元格添加一个小的左右边距,以便文本不会粘到单元格边框上。我在网上搜索了一切,但无法做到。可能是我错过了一些东西。请帮忙。如何更改Java中的Apache POI中的XWPFTableCell页边距?
太谢谢你了:)
答
您可以在表级别设置单元格边距:
table.setCellMargins(0, 500, 0, 500);
完整的例子是这样的:
public static void main(String[] args) throws IOException {
XWPFDocument doc = new XWPFDocument();
FileOutputStream out = new FileOutputStream(new File(FILENAME));
XWPFParagraph para = doc.createParagraph();
XWPFRun run = para.createRun();
//table
XWPFTable table = doc.createTable();
table.setCellMargins(0, 500, 0, 500); //set margins here
//rows
XWPFTableRow row1 = table.getRow(0);
row1.getCell(0).setText("Hello1");
row1.addNewTableCell().setText("Hello2");
row1.addNewTableCell().setText("Hello3");
XWPFTableRow row2 = table.createRow();
row2.getCell(0).setText("Hello4");
row2.getCell(1).setText("Hello5");
row2.getCell(2).setText("Hello6");
doc.write(out);
out.close();
doc.close();
}