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();
         }
      }
   }
  
}
原文地址:https://www.cnblogs.com/estellez/p/4091429.html