首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HWPF-POI:使用java在word中插入表

HWPF-POI:使用java在word中插入表
EN

Stack Overflow用户
提问于 2015-02-05 10:45:04
回答 1查看 1.9K关注 0票数 0

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

代码语言:javascript
复制
Table table = document.getRange().insertTableBefore((short) 2, 2);

该表被插入,但我看不到它--就好像该表的宽度为0。有人能帮我吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-31 15:32:03

旧bug报告中链接的文件应该会给您一个如何实现它的想法。

因此,本质上:您可能需要添加一些内容(即单元格中的段落),这样单词就有了一些要呈现的内容。

下面是bug报告中使用的示例代码:

代码语言:javascript
复制
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();
            }
        }
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28341881

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档