java使用poi读取doc和docx文件

maven构建的项目-->pom.xml文件

  • eclipse提供Dependencies直接添加依赖jar包的工具:直接搜索poi以及poi-ooxml即可,maven会自动依赖需要的jar包:
  1. poi提供microsoft office旧版本支持,eg .xls Excel
  2. poi-ooxml提供microsoft office新版本支持,eg .xlsx Excel
  • 或者手动修改pom.xml,在添加jar包依赖的地方加入

 

翻过这道山,就有人听到你的故事。
  1. 1 <dependency>
    2         <groupId>org.apache.poi</groupId>
    3         <artifactId>poi</artifactId>
    4         <version>3.10-FINAL</version>
    5     </dependency>
    1 <dependency>
    2         <groupId>org.apache.poi</groupId>
    3         <artifactId>poi-ooxml</artifactId>
    4         <version>3.10-FINAL</version>
    5     </dependency>

 

java使用poi读取doc和docx文件(maven自动导入依赖包)

 
 

于是在网上搜寻了一阵之后才发现原来doc文档和excel一样不能用普通的io流的方法来读取,而是也需要用poi,于是进行了一番尝试后,终于以正确的编码格式读取了这个doc文件。

在网上搜索的过程中发现doc和docx的读取方法是不一样的,于是顺带也学了一下docx文件的简单读取。

 

一、导包: 
doc文件的读取,需要导入poi-scratchpad的jar包和相关依赖包: 
这里写图片描述

docx文件读取,需要导入poi-ooxml的jar包和相关依赖包: 
这里写图片描述

我用的是maven构建项目,相关的依赖包会自动导入,maven导包配置如下:

复制代码
 1 <dependency>
 2         <groupId>org.apache.poi</groupId>
 3         <artifactId>poi-ooxml</artifactId>
 4         <version>3.8</version>
 5     </dependency>
 6     <dependency>
 7         <groupId>org.apache.poi</groupId>
 8         <artifactId>poi-scratchpad</artifactId>
 9         <version>3.8</version>
10     </dependency>
复制代码

 

 

二、读取文件的代码: 
1、doc文件读取简单示例:

复制代码
 1 public static void readAndWriterTest3() throws IOException {
 2         File file = new File("C:\\Users\\tuzongxun123\\Desktop\\aa.doc");
 3         String str = "";
 4         try {
 5             FileInputStream fis = new FileInputStream(file);
 6             HWPFDocument doc = new HWPFDocument(fis);
 7             String doc1 = doc.getDocumentText();
 8             System.out.println(doc1);
 9             StringBuilder doc2 = doc.getText();
10             System.out.println(doc2);
11             Range rang = doc.getRange();
12             String doc3 = rang.text();
13             System.out.println(doc3);
14             fis.close();
15         } catch (Exception e) {
16             e.printStackTrace();
17         }
18     }
复制代码

 

 

2、docx文件读取简单示例:

复制代码
 1 public static void readAndWriterTest4() throws IOException {
 2         File file = new File("C:\\Users\\tuzongxun123\\Desktop\\aa.docx");
 3         String str = "";
 4         try {
 5             FileInputStream fis = new FileInputStream(file);
 6             XWPFDocument xdoc = new XWPFDocument(fis);
 7             XWPFWordExtractor extractor = new XWPFWordExtractor(xdoc);
 8             String doc1 = extractor.getText();
 9             System.out.println(doc1);
10             fis.close();
11         } catch (Exception e) {
12             e.printStackTrace();
13         }
14     }
复制代码

 

 

//20171218修改 
我并没有在工作中操作过word,这篇博客也只是一时兴起所做,因此写的很简单。 
而最近陆续有朋友找我询问相关的问题,其中有好几个都在询问依赖包有哪些,为了避免一再回答这种问题,特将依赖包截图: 
这里写图片描述

 

 
范仁义 2018-01-01 14:30 阅读:1933 评论:0 推荐:0 编辑
 
范仁义 2018-01-01 14:09 阅读:176 评论:0 推荐:0 编辑
 
范仁义 2017-12-31 09:35 阅读:915 评论:0 推荐:1 编辑
 
范仁义 2017-12-31 09:33 阅读:1341 评论:0 推荐:1 编辑
 
范仁义 2017-12-30 08:54 阅读:1388 评论:0 推荐:0 编辑
 
范仁义 2017-12-30 02:13 阅读:1534 评论:0 推荐:0 编辑
 
范仁义 2017-12-30 02:10 阅读:322 评论:0 推荐:0 编辑
 
范仁义 2017-12-30 01:53 阅读:8700 评论:1 推荐:0 编辑
 
范仁义 2017-12-30 01:33 阅读:293 评论:0 推荐:0 编辑
 
范仁义 2017-09-27 14:57 阅读:520 评论:0 推荐:0 编辑
 
范仁义 2017-09-27 10:57 阅读:624 评论:0 推荐:1 编辑
 
 
posted @ 2020-08-17 17:24  ls1519🎈  阅读(3220)  评论(0编辑  收藏  举报