0

我能够从 doc 文件中读取表格。(见以下代码)

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 文件做同样的事情?我试图这样做。但找不到“范围”类的替代品。

请帮忙。

4

3 回答 3

2

应普遍要求,对答案发表评论...

Apache POI 代码示例中,您可以找到XWPF SimpleTable 示例

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

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

// 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();
于 2015-08-13T20:38:37.153 回答
0

以下代码段使用 Apache POI 5.0.0,在读取 docx 表数据时效果很好

public void readDocxTables(String docxFilePath) throws FileNotFoundException, IOException {
    XWPFDocument doc = new XWPFDocument(new FileInputStream(docxFilePath));
    for(XWPFTable table : doc.getTables()) {
        for(XWPFTableRow row : table.getRows()) {
            for(XWPFTableCell cell : row.getTableCells()) {
                System.out.println("cell text: " + cell.getText());
            }
        }
    }
}
于 2021-09-22T07:06:03.850 回答
-1

这不是 Apache POI,但使用第三方组件发现它更容易。 如何从 docx 文件中获取表格的示例

当然,如果您没有找到 POI 的解决方案,请想一想,

于 2013-06-26T17:51:51.343 回答