【问题标题】:How to change XWPFTableCell margins in Apache POI in Java?如何在 Java 中更改 Apache POI 中的 XWPFTableCell 边距?
【发布时间】:2017-09-28 12:07:35
【问题描述】:
我为此尝试了太多搜索,但没有得到想要的结果。我正在使用 Apache POI 使用 XWPF 将特定数据从 Excel 工作表复制到 MS Word 2010 文件中的表中。我已经完成了。
我要做的最后一件事是为每个单元格添加一个小的左右边距,以便文本不会粘在单元格边框上。我在互联网上搜索了所有内容,但无法这样做。也许我错过了什么。
【问题讨论】:
标签:
java
apache
ms-word
apache-poi
xwpf
【解决方案1】:
您可以在表格级别设置单元格边距:
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();
}