2013-06-12 2 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 파일과 동일한 작업을 수행 할 수 있습니까? 나는 그것을하려고 노력했다. 그러나 '범위'클래스의 대체를 찾을 수 없습니다.

도와주세요.

+1

당신은 [아파치 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에서

, 당신은 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(); 
+0

docx 파일에서 테이블을 읽고 싶지만이 테이블이 어느 단락인지 그리고 docx 파일에 어디에 있는지 알고 싶습니다. 단락 목록에서 표를 가져 오는 방법이 있습니까? 감사합니다. @ Gagravarr –

관련 문제