使用POI读取word文档内容

word doc文件2中方式

1.1     通过WordExtractor读文件(WordExtractor内部进行信息读取时还是通过HWPFDocument来获取的。)

1.2     通过HWPFDocument读文件

Apache poi的hwpf模块是专门用来对word doc文件进行读写操作的。在hwpf里面我们使用HWPFDocument来表示一个word doc文档。在HWPFDocument里面有这么几个概念:

l  Range:它表示一个范围,这个范围可以是整个文档,也可以是里面的某一小节(Section),也可以是某一个段落(Paragraph),还可以是拥有共同属性的一段文本(CharacterRun)。

l  Section:word文档的一个小节,一个word文档可以由多个小节构成。

l  Paragraph:word文档的一个段落,一个小节可以由多个段落构成。

l  CharacterRun:具有相同属性的一段文本,一个段落可以由多个CharacterRun组成。

l  Table:一个表格。

l  TableRow:表格对应的行。

l  TableCell:表格对应的单元格。

       Section、Paragraph、CharacterRun和Table都继承自Range。

1.1     通过WordExtractor读文件

       在使用WordExtractor读文件时我们只能读到文件的文本内容和基于文档的一些属性,至于文档内容的属性等是无法读到的。如果要读到文档内容的属性则需要使用HWPFDocument来读取了。下面是使用WordExtractor读取文件的一个示例:

public class HwpfTest {  
   
   @SuppressWarnings("deprecation")  
   @Test  
   public void testReadByExtractor() throws Exception {  
      InputStream is = new FileInputStream("D:\\test.doc");  
      WordExtractor extractor = new WordExtractor(is);  
      //输出word文档所有的文本  
      System.out.println(extractor.getText());  
      System.out.println(extractor.getTextFromPieces());  
      //输出页眉的内容  
      System.out.println("页眉:" + extractor.getHeaderText());  
      //输出页脚的内容  
      System.out.println("页脚:" + extractor.getFooterText());  
      //输出当前word文档的元数据信息,包括作者、文档的修改时间等。  
      System.out.println(extractor.getMetadataTextExtractor().getText());  
      //获取各个段落的文本  
      String paraTexts[] = extractor.getParagraphText();  
      for (int i=0; i<paraTexts.length; i++) {  
         System.out.println("Paragraph " + (i+1) + " : " + paraTexts[i]);  
      }  
      //输出当前word的一些信息  
      printInfo(extractor.getSummaryInformation());  
      //输出当前word的一些信息  
      this.printInfo(extractor.getDocSummaryInformation());  
      this.closeStream(is);  
   }  
    
   /** 
    * 输出SummaryInfomation 
    * @param info 
    */  
   private void printInfo(SummaryInformation info) {  
      //作者  
      System.out.println(info.getAuthor());  
      //字符统计  
      System.out.println(info.getCharCount());  
      //页数  
      System.out.println(info.getPageCount());  
      //标题  
      System.out.println(info.getTitle());  
      //主题  
      System.out.println(info.getSubject());  
   }  
    
   /** 
    * 输出DocumentSummaryInfomation 
    * @param info 
    */  
   private void printInfo(DocumentSummaryInformation info) {  
      //分类  
      System.out.println(info.getCategory());  
      //公司  
      System.out.println(info.getCompany());  
   }  
    
   /** 
    * 关闭输入流 
    * @param is 
    */  
   private void closeStream(InputStream is) {  
      if (is != null) {  
         try {  
            is.close();  
         } catch (IOException e) {  
            e.printStackTrace();  
         }  
      }  
   }  
    
} 


1.2     通过HWPFDocument读文件

HWPFDocument 是当前 Word 文档的代表,它的功能比 WordExtractor 要强。通过它我们可以读取文档中的表格、列表等,还可以对文档的内容进行新增、修改和删除操作。只是在进行完这些新增、修改和删除后相关信息是保存在 HWPFDocument 中的,也就是说我们改变的是 HWPFDocument ,而不是磁盘上的文件。如果要使这些修改生效的话,我们可以调用 HWPFDocument write 方法把修改后的 HWPFDocument 输出到指定的输出流中。这可以是原文件的输出流,也可以是新文件的输出流(相当于另存为)或其它输出流。下面是一个通过 HWPFDocument 读文件的示例:

public class HwpfTest {  
    
   @Test  
   public void testReadByDoc() throws Exception {  
      InputStream is = new FileInputStream("D:\\test.doc");  
      HWPFDocument doc = new HWPFDocument(is);  
      //输出书签信息  
      this.printInfo(doc.getBookmarks());  
      //输出文本  
      System.out.println(doc.getDocumentText());  
      Range range = doc.getRange();  
      //this.insertInfo(range);  
      this.printInfo(range);  
      //读表格  
      this.readTable(range);  
      //读列表  
      this.readList(range);  
      //删除range  
      Range r = new Range(2, 5, doc);  
      r.delete();//在内存中进行删除,如果需要保存到文件中需要再把它写回文件  
      //把当前HWPFDocument写到输出流中  
      doc.write(new FileOutputStream("D:\\test.doc"));  
      this.closeStream(is);  
   }  
    
   /** 
    * 关闭输入流 
    * @param is 
    */  
   private void closeStream(InputStream is) {  
      if (is != null) {  
         try {  
            is.close();  
         } catch (IOException e) {  
            e.printStackTrace();  
         }  
      }  
   }  
    
   /** 
    * 输出书签信息 
    * @param bookmarks 
    */  
   private void printInfo(Bookmarks bookmarks) {  
      int count = bookmarks.getBookmarksCount();  
      System.out.println("书签数量:" + count);  
      Bookmark bookmark;  
      for (int i=0; i<count; i++) {  
         bookmark = bookmarks.getBookmark(i);  
         System.out.println("书签" + (i+1) + "的名称是:" + bookmark.getName());  
         System.out.println("开始位置:" + bookmark.getStart());  
         System.out.println("结束位置:" + bookmark.getEnd());  
      }  
   }  
    
   /** 
    * 读表格 
    * 每一个回车符代表一个段落,所以对于表格而言,每一个单元格至少包含一个段落,每行结束都是一个段落。 
    * @param range 
    */  
   private void readTable(Range range) {  
      //遍历range范围内的table。  
      TableIterator tableIter = new TableIterator(range);  
      Table table;  
      TableRow row;  
      TableCell cell;  
      while (tableIter.hasNext()) {  
         table = tableIter.next();  
         int rowNum = table.numRows();  
         for (int j=0; j<rowNum; j++) {  
            row = table.getRow(j);  
            int cellNum = row.numCells();  
            for (int k=0; k<cellNum; k++) {  
                cell = row.getCell(k);  
                //输出单元格的文本  
                System.out.println(cell.text().trim());  
            }  
         }  
      }  
   }  
    
   /** 
    * 读列表 
    * @param range 
    */  
   private void readList(Range range) {  
      int num = range.numParagraphs();  
      Paragraph para;  
      for (int i=0; i<num; i++) {  
         para = range.getParagraph(i);  
         if (para.isInList()) {  
            System.out.println("list: " + para.text());  
         }  
      }  
   }  
    
   /** 
    * 输出Range 
    * @param range 
    */  
   private void printInfo(Range range) {  
      //获取段落数  
      int paraNum = range.numParagraphs();  
      System.out.println(paraNum);  
      for (int i=0; i<paraNum; i++) {  
         //this.insertInfo(range.getParagraph(i));  
         System.out.println("段落" + (i+1) + ":" + range.getParagraph(i).text());  
         if (i == (paraNum-1)) {  
            this.insertInfo(range.getParagraph(i));  
         }  
      }  
      int secNum = range.numSections();  
      System.out.println(secNum);  
      Section section;  
      for (int i=0; i<secNum; i++) {  
         section = range.getSection(i);  
         System.out.println(section.getMarginLeft());  
         System.out.println(section.getMarginRight());  
         System.out.println(section.getMarginTop());  
         System.out.println(section.getMarginBottom());  
         System.out.println(section.getPageHeight());  
         System.out.println(section.text());  
      }  
   }  
    
   /** 
    * 插入内容到Range,这里只会写到内存中 
    * @param range 
    */  
   private void insertInfo(Range range) {  
      range.insertAfter("Hello");  
   }  
    
} 

  • 9
    点赞
  • 61
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值