使用Apache POI从docx文件中读取表

时间:2013-06-12 10:09:44

标签: java class apache-poi fetch docx

我能够从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文件执行相同的操作? 我试着这样做。但无法找到'Range'类的替代品。

请帮忙。

2 个答案:

答案 0 :(得分:2)

根据受欢迎的要求,推广对答案的评论......

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();

答案 1 :(得分:0)

这不是Apache POI,但使用第三方组件发现它更容易。 An example how to get tables from a docx file

当然,如果你没有找到POI的解决方案,那就明白了,