Java word 内容读取
发布日期:2021-06-30 17:38:19
浏览次数:3
分类:技术文章
本文共 1298 字,大约阅读时间需要 4 分钟。
1、添加依赖关系(网上好多帖子没有写依赖,害我找半天)
<dependency>
<groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.0.0</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.0.0</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-scratchpad</artifactId> <version>4.0.0</version> </dependency>不是maven项目到这里找JAR包,在页面中找“Files”右边就是“jar”点击即可下载
2、写工具类
package com.xxx.util;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import org.apache.poi.hwpf.extractor.WordExtractor;public class DocUtil { /** * 读取doc文件内容 * * @param file * 想要读取的文件对象 * @return 返回文件内容 * @throws IOException */ public static String doc2String(FileInputStream fs) throws IOException { StringBuilder result = new StringBuilder(); WordExtractor re = new WordExtractor(fs); result.append(re.getText()); re.close(); return result.toString(); } public static String doc2String(File file) throws IOException { return doc2String(new FileInputStream(file)); } public static void main(String[] args) { File file = new File("C:\\Users\\frank.li\\Desktop\\产品购销合同范本1.doc"); try { System.out.println(doc2String(file)); } catch (IOException e) { e.printStackTrace(); } }}
3、运行结果
5、word内容截图
转载地址:https://lemonstone.blog.csdn.net/article/details/82793018 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
很好
[***.229.124.182]2024年04月10日 04时08分03秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Python计算文本BLEU分数
2019-04-30
swap内存(linux)
2019-04-30
人脸au
2019-04-30
torch.distributed 分布式
2019-04-30
OpenMP编程模型(OMP)
2019-04-30
混合精度训练(FP16 & FP32)
2019-04-30
PyPy
2019-04-30
打印CSDN文章
2019-04-30
MATLAB与CUDA
2019-04-30
Linux png转jpg (convert命令)
2019-04-30
NAS (Network Attached Storage 网络附属存储)
2019-04-30
Ubuntu更新后终端中字体的颜色全是白色
2019-04-30
Ninja
2019-04-30
lmdb数据库的读取与转换(一) —— 基本操作
2019-04-30
opencv相关操作(cv2) (python)
2019-04-30
lmdb数据库的读取与转换(二) —— 数据集操作
2019-04-30
Lua语言
2019-04-30
Python __doc__获得模块的文档字符串内容
2019-04-30
Python sys.path和模块搜索路径
2019-04-30