簡體   English   中英

使用Apache POI從docx文件中讀取表

[英]Read table from docx file using Apache POI

我能夠從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'類的替代品。

請幫忙。

根據受歡迎的要求,推廣對答案的評論......

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

這不是Apache POI,但使用第三方組件發現它更容易。 如何從docx文件獲取表的示例

當然,如果你沒有找到POI的解決方案,

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM