java - 使用 Apache POI 从 docx 文件读取表

标签 java class apache-poi fetch docx

我能够从文档文件中读取表格。 (见下面的代码)

public String readDocFile(String filename, String str) {
        try {
            InputStream fis = new FileInputStream(filename);
            POIFSFileSystem fs = new POIFSFileSystem(fis);
            HWPFDocument doc = new HWPFDocument(fs);

            Range range = doc.getRange();
            boolean intable = false;
            boolean inrow = false;

            for (int i = 0; i < range.numParagraphs(); i++) {
                Paragraph par = range.getParagraph(i);
                //System.out.println("paragraph "+(i+1));
                //System.out.println("is in table: "+par.isInTable());
                //System.out.println("is table row end: "+par.isTableRowEnd());
                //System.out.println(par.text());

                if (par.isInTable()) {
                    if (!intable) {//System.out.println("New table creating"+intable);
                        str += "<table border='1'>";
                        intable = true;
                    }
                    if (!inrow) {//System.out.println("New row creating"+inrow);
                        str += "<tr>";
                        inrow = true;
                    }
                    if (par.isTableRowEnd()) {
                        inrow = false;
                    } else {
                        //System.out.println("New text adding"+par.text());
                        str += "<td>" + par.text() + "</td>";
                    }
                } else {
                    if (inrow) {//System.out.println("Closing Row");
                        str += "</tr>";
                        inrow = false;
                    }
                    if (intable) {//System.out.println("Closing Table");
                        str += "</table>";
                        intable = false;
                    }
                    str += par.text() + "<br/>";
                }
            }
        } catch (Exception e) {
            System.out.println("Exception: " + e);
        }

        return str;
    }

有人可以建议我如何对 docx 文件执行相同的操作吗? 我尝试这样做。但无法找到“Range”类的替代品。

请帮忙。

最佳答案

根据大众的要求,将评论提升为答案...

Apache POI code examples ,您可以找到XWPF SimpleTable example

这展示了如何创建一个简单的表格,以及如何创建一个具有大量精美样式的表格。

假设您只想在一个全新的工作簿中从头开始一个简单的表格,那么您需要的代码如下:

// Start with a new document
XWPFDocument doc = new XWPFDocument();

// Add a 3 column, 3 row table
XWPFTable table = doc.createTable(3, 3);

// Set some text in the middle
table.getRow(1).getCell(1).setText("EXAMPLE OF TABLE");

// table cells have a list of paragraphs; there is an initial
// paragraph created when the cell is created. If you create a
// paragraph in the document to put in the cell, it will also
// appear in the document following the table, which is probably
// not the desired result.
XWPFParagraph p1 = table.getRow(0).getCell(0).getParagraphs().get(0);

XWPFRun r1 = p1.createRun();
r1.setBold(true);
r1.setText("The quick brown fox");
r1.setItalic(true);
r1.setFontFamily("Courier");
r1.setUnderline(UnderlinePatterns.DOT_DOT_DASH);
r1.setTextPosition(100);

// And at the end
table.getRow(2).getCell(2).setText("only text");

// Save it out, to view in word
FileOutputStream out = new FileOutputStream("simpleTable.docx");
doc.write(out);
out.close();

关于java - 使用 Apache POI 从 docx 文件读取表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17062841/

相关文章:

java - 尝试用密码保护中等大小的 XLSX 工作簿 (~=80MB) 时,Apache POI 抛出 OOM 错误

java - 使用 Apache POI 双向处理 Word 文档

java - 如何在 Java 中修改 OpenOffice.org Draw 图表?

java - 如何对对象使用不同的枚举类型来构建具有唯一值的 GUI 滚动列表

python - 如何使我的类中的变量成为全局变量,但仅使其在类中成为全局变量?

c# - 在C#中实现接口(interface)时如何传递子类参数?

java - 有没有办法将 Concordion 测试结果输出为 pdf 格式?

java - 为什么在创建新实例时进行 DUP

ios - 如何确定 iOS (id) 对象的结构?

java - 使用 Apache POI 获取大型 excel 文件的 excel 工作表名称