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

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

XWPFParagraph:表明一個段落。spa

XWPFRun:表明具備相同屬性的一段文本。code

XWPFTable:表明一個表格。對象

XWPFTableRow:表格的一行。blog

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

1讀docx文件

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

1.1經過XWPFWordExtractor讀

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

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文檔的示例:io

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();
         }
      }
   }
  
}
相關文章
相關標籤/搜索