java使用apache poi 读取 doc,docx,ppt,pptx,xls,xlsx,txt,csv格式的文件示例代码

1、maven依赖添加

在 pom 文件中添加如下依赖

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.4</version>
</dependency>

2、文件读取代码示例

doc 格式文件

// --------- doc -----------
File file = new File("E:\\search-file\\22.doc");
FileInputStream fis = null;
HWPFDocument document = null;
WordExtractor extractor = null;
try {
fis = new FileInputStream(file);
document = new HWPFDocument(fis);
extractor = new WordExtractor(document);
log.info("extractor.getText:{}", extractor.getText());
} catch (Exception e) {
e.printStackTrace();
}

docx 格式文件

// --------- docx -----------
File file = new File("E:\\search-file\\11.docx");
FileInputStream fis = null;
XWPFDocument document = null;
XWPFWordExtractor extractor = null;
try {
fis = new FileInputStream(file);
document = new XWPFDocument(fis);
extractor = new XWPFWordExtractor(document);
log.info("extractor.getText:{}", extractor.getText());
} catch (Exception e) {
e.printStackTrace();
}

pptx 格式文件

// --------- pptx -----------
File file = new File("E:\\search-file\\33.pptx");
FileInputStream fis = null;
XMLSlideShow document = null;
SlideShowExtractor extractor = null;
try {
fis = new FileInputStream(file);
document = new XMLSlideShow(fis);
extractor = new SlideShowExtractor(document);
log.info("extractor.getText:{}", extractor.getText());
} catch (Exception e) {
e.printStackTrace();
}

ppt 格式文件

// --------- ppt -----------
File file = new File("E:\\search-file\\44.ppt");
FileInputStream fis = null;
HSLFSlideShow document = null;
SlideShowExtractor extractor = null;
try {
fis = new FileInputStream(file);
document = new HSLFSlideShow(fis);
extractor = new SlideShowExtractor(document);
log.info("extractor.getText:{}", extractor.getText());
} catch (Exception e) {
e.printStackTrace();
}

xlsx 格式文件

// --------- xlsx -----------

File file = new File("E:\\search-file\\55.xlsx");
FileInputStream fis = null;
XSSFWorkbook document = null;
XSSFExcelExtractor extractor = null;
try {
fis = new FileInputStream(file);
document = new XSSFWorkbook(fis);
extractor = new XSSFExcelExtractor(document);
log.info("extractor.getText:{}", extractor.getText());
} catch (Exception e) {
e.printStackTrace();
}

xls 格式文件

// --------- xls -----------
File file = new File("E:\\search-file\\66.xls");
FileInputStream fis = null;
HSSFWorkbook document = null;
ExcelExtractor extractor = null;
try {
fis = new FileInputStream(file);
document = new HSSFWorkbook(fis);
extractor = new ExcelExtractor(document);
log.info("extractor.getText:{}", extractor.getText());
} catch (Exception e) {
e.printStackTrace();
}

txt,csv 格式文件

// --------- txt,csv -----------
File file = new File("E:\\search-file\\77.txt");
StringBuffer buffer = new StringBuffer();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf8"))){
String line = null;
while ((line = reader.readLine()) != null) {
buffer.append(line).append('\n');
}
} catch (Exception e) {
e.printStackTrace();
}
log.info("txt-context:{}", buffer);

java中使用apache poi 读取 doc,docx,ppt,pptx,xls,xlsx,txt,csv格式的文件示例代码的更多相关文章

  1. java读取txt/pdf/xls/xlsx/doc/docx/ppt/pptx

    环境准备txt利用common-iopdf利用pdfbox剩下的用POI关于POI,读取xls没啥特别的,主要是读取doc和ppt,需要下载poi源代码,然后将poi-src-3.7-20101029 ...

  2. java使用org.apache.poi读取与保存EXCEL文件

    一.读EXCEL文件 package com.ruijie.wis.cloud.utils; import java.io.FileInputStream; import java.io.FileNo ...

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

    java使用poi读取doc和docx文件(maven自动导入依赖包) 于是在网上搜寻了一阵之后才发现原来doc文档和excel一样不能用普通的io流的方法来读取,而是也需要用poi,于是进行了一番尝 ...

  4. Java开发小技巧(六):使用Apache POI读取Excel

    前言 在数据仓库中,ETL最基础的步骤就是从数据源抽取所需的数据,这里所说的数据源并非仅仅是指数据库,还包括excel.csv.xml等各种类型的数据接口文件,而这些文件中的数据不一定是结构化存储的, ...

  5. 使用poi读取word2007(.docx)中的复杂表格

    使用poi读取word2007(.docx)中的复杂表格 最近工作需要做一个读取word(.docx)中的表格,并以html形式输出.经过上网查询,使用了poi. 对于2007及之后的word文档,需 ...

  6. 项目一:第四天 1、快递员的条件分页查询-noSession,条件查询 2、快递员删除(逻辑删除) 3、基于Apache POI实现批量导入区域数据 a)Jquery OCUpload上传文件插件使用 b)Apache POI读取excel文件数据

    1. 快递员的条件分页查询-noSession,条件查询 2. 快递员删除(逻辑删除) 3. 基于Apache POI实现批量导入区域数据 a) Jquery OCUpload上传文件插件使用 b) ...

  7. Java下使用Apache POI生成具有三级联动下拉列表的Excel文档

    使用Apache POI生成具有三级联动下拉列表的Excel文档: 具体效果图与代码如下文. 先上效果图: 开始贴代码,代码中部分测试数据不影响功能. 第一部分(核心业务处理): 此部分包含几个方面: ...

  8. 内容写到 csv 格式的文件中 及 读取 csv 格式的文件内容

    <?php/*把内容写到 csv 格式的文件中 基本思路是:1.用 $fp = fopen("filename", 'mode')打开一个csv文件,可以是打开时才建立的2. ...

  9. C# winfrom 写的一个搜索助手,可以按照标题和内容搜索,支持doc,xls,ppt,pdf,txt等格式的文件搜索

    C# winfrom 写的一个搜索助手,可以按照标题和内容搜索,指定目录后,遍历搜索文件和子目,现在只写了支持.DOC.DOCX.XLS.XLSX.PPT.PPTX.PDF.HTML.HTM.TXT等 ...

  10. python 将json格式的数据写入csv格式的文件中

    # coding=utf-8 import json import csv # 重新进行配置读写数据时的默认编码 import sys reload(sys) sys.setdefaultencodi ...

随机推荐

  1. 关于Redhat-7.x-下docker的安装记录

    今天因公司项目,需要部署docker环境,能根据指定的镜像创建容器 于是首先就得先部署docker环境,过程记录如下: 在Redhat 7.x - (aws上的Redhat) 环境下部署过程 1.安装 ...

  2. sg函数入门理解

    首先理解sg函数必须先理解mex函数 mex是求除它集合内的最小大于等于0的整数,例:mex{1,2}=0:mex{2}=0:mex{0,1,2}=3:mex{0,5}=1. 而sg函数是啥呢? 对于 ...

  3. 多线程的使用(springboot)

    预备知识 业务使用多线程的原因 目的是面对高并发的时候,提高运行速度 场景一: 一个业务逻辑有很多次的循环,每次循环之间没有影响,比如验证1万条url路径是否存在,正常情况要循环1万次,逐个去验证每一 ...

  4. abstract关键字的使用

    1.abstract:抽象的 2.abstract可以用来修饰的结构:类.方法 3.abstract修饰类:抽象类 此类不能实例化 抽象类中一定有构造器,便于子类实例化时调用(涉及:子类对象实例化的全 ...

  5. 小程序 wx.navigateTo和 wx.redirectTo区别

    wx.navigateTo 官方解释: 意思就是说. A页面跳转B页面 B页面做了操作,点击保存,再跳转回A页面 此时,如果点击左上返回按钮,仍然可以跳转回B页面,而且里面的数据是操作之前的数据 wx ...

  6. python实现鼠标手动截图(类似于QQ截图)

    由于在网上找了很久,只找到按像素位置截图和全屏截图的,所以决定自己写一个. 本程序实现原理是现用PIL里的ImageGrab进行全屏截图,然后通过模拟鼠标操作,进行截图,最后删除全屏截图,只留下鼠标截 ...

  7. C#--String.Substring方法

    第一种:String.SubString(int start,int length)    截取指定长度的字符串 这里有两个int型的参数  string表示字符串截取的起始位置,length表截取的 ...

  8. 还在用双层for循环吗?太慢了

    前情提要 我们在开发中经常碰到这样的场景,查出两个 list 集合数据,需要根据他们相同的某个属性为连接点,进行聚合.但是平时我们使用的时候关注过性能吗?下面让我们一起来看看它的表现如何. 来个例子 ...

  9. 详细了解JVM运行时内存

    详细了解JVM运行时内存 1.程序计数器 概念 程序计数器也叫作PC寄存器,是一块很小的内存区域,可以看做是当前线程执行的字节码的行号指示器.字节码的解释工作就是通过改变程序计数器里面的值来获得下一条 ...

  10. redis的缓存穿透、击穿、雪崩以及实用解决方案

    今天来聊聊redis的缓存穿透.击穿.雪崩以及解决方案,其中解决方案包括类似于布隆过滤器这种网上一搜一大片但是实际生产部署有一定复杂度的,也有基于spring注解通过一行代码就能解决的,其中各有优劣, ...