gpt4 book ai didi

java - Apache poi 表 (XWPFTable) 未正确生成

转载 作者:行者123 更新时间:2023-12-01 06:13:40 27 4
gpt4 key购买 nike

关于 apache poi 有很多教程,但是我在创建表时遇到问题。我正在尝试这段代码:

public class CreateTable
{
public static void main(String[] args)throws Exception
{
//Blank Document
XWPFDocument document= new XWPFDocument();

//Write the Document in file system
FileOutputStream out = new FileOutputStream(
new File("create_table.docx"));

//create table
XWPFTable table = document.createTable();
//create first row
XWPFTableRow tableRowOne = table.getRow(0);
tableRowOne.getCell(0).setText("col one, row one");
tableRowOne.addNewTableCell().setText("col two, row one");
tableRowOne.addNewTableCell().setText("col three, row one");
//create second row
XWPFTableRow tableRowTwo = table.createRow();
tableRowTwo.getCell(0).setText("col one, row two");
tableRowTwo.getCell(1).setText("col two, row two");
tableRowTwo.getCell(2).setText("col three, row two");
//create third row
XWPFTableRow tableRowThree = table.createRow();
tableRowThree.getCell(0).setText("col one, row three");
tableRowThree.getCell(1).setText("col two, row three");
tableRowThree.getCell(2).setText("col three, row three");

document.write(out);
out.close();
System.out.println("create_table.docx written successully");
}
}

但是在 Libre Office ubuntu 中,表格具有无限宽度,当我将其加载到 google doc 时,该文档中什么也没有。设置表格宽度没有帮助。我做错了什么?请帮忙((

最佳答案

您可以通过以下方式设置宽度:

XWPFDocument doc = new XWPFDocument();
XWPFTable table = doc.createTable(1,2);
table.getCTTbl().addNewTblGrid().addNewGridCol().setW(BigInteger.valueOf(6000));
table.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(2000));
table.getRow(0).getCell(0).setText("1A");
table.getRow(0).getCell(1).setText("1B");
XWPFTableRow newrow = table.createRow();
newrow.getCell(0).setText("2A");
newrow.getCell(1).setText("2B");

关于java - Apache poi 表 (XWPFTable) 未正确生成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29331816/

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