gpt4 book ai didi

java - 如何在 Java 中更改 Apache POI 中的 XWPFTableCell 边距?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:34:15 25 4
gpt4 key购买 nike

我尝试为此搜索了太多但没有得到想要的结果。我正在使用 Apache POI 使用 XWPF 将特定数据从 Excel 工作表复制到 MS Word 2010 文件中的表格。我已经完成了。

我想做的最后一件事是为每个单元格添加一个小的左右边距,这样文本就不会粘在单元格边框上。我在互联网上搜索了所有内容,但无法搜索。也许我遗漏了什么。

最佳答案

您可以在表格级别设置单元格边距:

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();
}

关于java - 如何在 Java 中更改 Apache POI 中的 XWPFTableCell 边距?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46468753/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com