POI讀word docx 07 文件的兩種方法


POI在讀寫word docx文件時是通過xwpf模塊來進行的,其核心是XWPFDocument。一個XWPFDocument代表一個docx文檔,其可以用來讀docx文檔,也可以用來寫docx文檔。XWPFDocument中主要包含下面這幾種對象:

XWPFParagraph:代表一個段落。

XWPFRun:代表具有相同屬性的一段文本。

XWPFTable:代表一個表格。

XWPFTableRow:表格的一行。

XWPFTableCell:表格對應的一個單元格。 

1讀docx文件

跟讀doc文件一樣,POI在讀docx文件的時候也有兩種方式,通過XWPFWordExtractor和通過XWPFDocument。在XWPFWordExtractor讀取信息時其內部還是通過XWPFDocument來獲取的。

1.1通過XWPFWordExtractor讀

在使用XWPFWordExtractor讀取docx文檔的內容時,我們只能獲取到其文本,而不能獲取到其文本對應的屬性值。下面是一段使用XWPFWordExtractor來讀取docx文檔內容的示例代碼:

public class XwpfTest {
 
   /**
    * 通過XWPFWordExtractor訪問XWPFDocument的內容
    * @throws Exception
    */
   @Test
   public void testReadByExtractor() throws Exception {
      InputStream is = new FileInputStream("D:\\test.docx");
      XWPFDocument doc = new XWPFDocument(is);
      XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
      String text = extractor.getText();
      System.out.println(text);
      CoreProperties coreProps = extractor.getCoreProperties();
      this.printCoreProperties(coreProps);
      this.close(is);
   }
  
   /**
    * 輸出CoreProperties信息
    * @param coreProps
    */
   private void printCoreProperties(CoreProperties coreProps) {
      System.out.println(coreProps.getCategory());   //分類
      System.out.println(coreProps.getCreator()); //創建者
      System.out.println(coreProps.getCreated()); //創建時間
      System.out.println(coreProps.getTitle());   //標題
   }
  
   /**
    * 關閉輸入流
    * @param is
    */
   private void close(InputStream is) {
      if (is != null) {
         try {
            is.close();
         } catch (IOException e) {
            e.printStackTrace();
         }
      }
   }
  
}

1.2 通過XWPFDocument讀

       在通過XWPFDocument讀取docx文檔時,我們就可以獲取到文本比較精確的屬性信息了。比如我們可以獲取到某一個XWPFParagraph、XWPFRun或者是某一個XWPFTable,包括它們對應的屬性信息。下面是一個使用XWPFDocument讀取docx文檔的示例:

public class XwpfTest {
 
   /**
    * 通過XWPFDocument對內容進行訪問。對於XWPF文檔而言,用這種方式進行讀操作更佳。
    * @throws Exception
    */
   @Test
   public void testReadByDoc() throws Exception {
      InputStream is = new FileInputStream("D:\\table.docx");
      XWPFDocument doc = new XWPFDocument(is);
      List<XWPFParagraph> paras = doc.getParagraphs();
      for (XWPFParagraph para : paras) {
         //當前段落的屬性
//       CTPPr pr = para.getCTP().getPPr();
         System.out.println(para.getText());
      }
      //獲取文檔中所有的表格
      List<XWPFTable> tables = doc.getTables();
      List<XWPFTableRow> rows;
      List<XWPFTableCell> cells;
      for (XWPFTable table : tables) {
         //表格屬性
//       CTTblPr pr = table.getCTTbl().getTblPr();
         //獲取表格對應的行
         rows = table.getRows();
         for (XWPFTableRow row : rows) {
            //獲取行對應的單元格
            cells = row.getTableCells();
            for (XWPFTableCell cell : cells) {
                System.out.println(cell.getText());;
            }
         }
      }
      this.close(is);
   }
  
   /**
    * 關閉輸入流
    * @param is
    */
   private void close(InputStream is) {
      if (is != null) {
         try {
            is.close();
         } catch (IOException e) {
            e.printStackTrace();
         }
      }
   }
  
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM