2013-06-12 61 views
0

我能夠從doc文件中讀取表格。 (見下面的代碼)使用Apache POI從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'類的替代品。

請幫忙。

+1

你嘗試在看[Apache的POI XWPF表示例] (http://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/xwpf/usermodel/SimpleTable.java)?乍一看,這應該涵蓋你... – Gagravarr

+0

非常感謝尼克... –

+0

@Gagravarr爲什麼不作爲答案? :) – Szundi

回答

1

徇衆要求,推進到一個答案評論只是想法...

Apache POI code examples,你可以找到​​

這顯示瞭如何創建一個簡單的表格,以及如何創建一個有很多花哨樣式的表格。

假設你只是想從頭開始一個簡單的表格,在一個全新的工作簿,然後你需要的代碼線沿線的雲:

// 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(); 
+0

我想要從docx文件中讀取表格,但我想知道這些表格在哪些段落以及它們位於docx文件中的哪些位置。有沒有辦法從段落列表中獲取表格?謝謝。@ Gagravarr –

相關問題