gpt4 book ai didi

java - HWPF-POI : inserting table in word with java

转载 作者:行者123 更新时间:2023-12-01 11:57:20 27 4
gpt4 key购买 nike

我想在 Word 中使用 POI-HWPF 创建一个表格(例如 doc 格式)。我的示例代码是:

Table table = document.getRange().insertTableBefore((short) 2, 2);

表格已插入,但我看不到它 - 就好像表格的宽度为 0。有人可以帮助我吗?

最佳答案

this 中链接的文件旧的错误报告应该可以让您了解如何做到这一点。

本质上:您可能需要添加一些内容(即单元格中的段落),以便 Word 可以呈现一些内容。

以下是错误报告中使用的示例代码:

private static void test (int rows, int columns) throws Exception {
// POI apparently can't create a document from scratch,
// so we need an existing empty dummy document
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("empty.doc"));
HWPFDocument doc = new HWPFDocument(fs);

Range range = doc.getRange();
Table table = range.insertBefore(new TableProperties(columns), rows);

for (int rowIdx=0; rowIdx<table.numRows(); rowIdx++) {
TableRow row = table.getRow(rowIdx);
System.out.println("row "+rowIdx);
for (int colIdx=0; colIdx<row.numCells(); colIdx++) {
TableCell cell = row.getCell(colIdx);
System.out.println("column "+colIdx+", num paragraphs "+cell.numParagraphs());
try {
Paragraph par = cell.getParagraph(0);
par.insertBefore(""+(rowIdx*row.numCells()+colIdx));
} catch (Exception ex) {
ex.printStackTrace();
}
}
}

关于java - HWPF-POI : inserting table in word with java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28341881/

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