我想用POI(例如doc格式)在Word中创建一个表。我的示例代码是:
Table table = document.getRange().insertTableBefore((short) 2, 2);该表被插入,但我看不到它--就好像该表的宽度为0。有人能帮我吗?
发布于 2015-12-31 15:32:03
在这旧bug报告中链接的文件应该会给您一个如何实现它的想法。
因此,本质上:您可能需要添加一些内容(即单元格中的段落),这样单词就有了一些要呈现的内容。
下面是bug报告中使用的示例代码:
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();
}
}
}https://stackoverflow.com/questions/28341881
复制相似问题