当前位置: 首页 > news >正文

【Java Easypoi Apache poi】 Word导入与导出

引入依赖

<dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-spring-boot-starter</artifactId>
</dependency>
<!-- 下面的版本需要对应上面依赖中的版本 否则可能会起冲突 -->
<!-- 下面的依赖主要是为了使用Apache原生的WordExtractor对doc后缀文件的解析 -->
<dependency><groupId>org.apache.poi</groupId><artifactId>poi-scratchpad</artifactId><version>4.1.1</version>
</dependency>
<!-- 糊涂Api工具 -->
<dependency><groupId>cn.hutool</groupId><artifactId>hutool-core</artifactId><version>5.8.10</version>
</dependency>

工具类封装

public class WordDocumentUtil {/*** 解析文档文件** @param file 文档文件* @return 文档内容*/public static String parseWord(MultipartFile file) {String wordTxt = "";InputStream stream = null;try {if (file.getOriginalFilename().endsWith(".doc")) {stream = file.getInputStream();// Apache PoiWordExtractor ex = new WordExtractor(stream);wordTxt = ex.getText();} else if (file.getOriginalFilename().endsWith(".docx")) {stream = file.getInputStream();// EasyPoiXWPFDocument document = new XWPFDocument(stream);XWPFWordExtractor ex = new XWPFWordExtractor(document);wordTxt = ex.getText();}} catch (Exception e) {// 此处建议抛出异常 "文档解析有误"e.printStackTrace();} finally {if (stream != null) {try {stream.close();} catch (IOException e) {e.printStackTrace();}}}return wordTxt;}/*** 判断文档类型进行不同的分割方式* ".doc"后缀需要以"\r\n"切割 而".docx"后缀需要以"\n"切割** @param file 文件名:以file.getOriginalFilename()传入* @param wordTxt 文件内容* @return 内容数组*/public static String[] judgeType(String file, String wordTxt) {boolean suffixFlag = file.endsWith(".doc");return suffixFlag ? Arrays.stream(wordTxt.split("\r\n")).toArray(String[]::new): Arrays.stream(wordTxt.split("\n")).toArray(String[]::new);}/*** 导出resources下的word模板表** @param fileName 文件名* @param response {@link HttpServletResponse}*/public void exportTemplate(String fileName, HttpServletResponse response) {InputStream inputStream = null;try {String path = "/word/" + fileName;inputStream = this.getClass().getResourceAsStream(path);String newFileName = IdUtil.simpleUUID() + StrUtil.DOT + FileUtil.extName(fileName);byte[] bytes = new byte[1024 * 1024];// 输入流读取文件if (inputStream != null) {inputStream.read(bytes);}response.setCharacterEncoding("UTF-8");response.setContentType("application/msword");response.setHeader("Access-Control-Expose-Headers","Content-disposition");response.setHeader("Content-Disposition","attachment;filename=" + newFileName);response.getOutputStream().write(bytes);} catch (Exception e) {e.printStackTrace();} finally {if (inputStream != null) {try {inputStream.close();} catch (IOException e) {e.printStackTrace();}}}}
}

乱码问题

        如果这里造成了读取resources下的文件返回前端乱码问题:除了HttpServletResponse响应中设置字体问题,还有可能是因为在编译期文件就已经乱码了,所以需要在pom.xml中增加以下配置。

<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-resources-plugin</artifactId><version>2.6</version><configuration><encoding>UTF-8</encoding><nonFilteredFileExtensions><nonFilteredFileExtension>doc</nonFilteredFileExtension></nonFilteredFileExtensions></configuration></plugin></plugins>
</build>
http://www.lryc.cn/news/146578.html

相关文章:

  • Java稀疏数组
  • 内存管理框架 --- 基础知识
  • React + Next.js 搭建项目(配有对比介绍一起食用)
  • 【Java】Java基础
  • Spring-SpringBoot-SpringMVC-MyBatis常见面试题
  • 15.MyCat数据库分片
  • 【Python】PySpark
  • pycharm 打开Terminal时报错activate.ps1,因为在此系统上禁止运行脚本,并因此无法进入虚拟环境
  • [C++][C#]yolox TensorRT C++ C#部署
  • 根据源码,模拟实现 RabbitMQ - 网络通讯设计,自定义应用层协议,实现 BrokerServer (8)
  • MongoDB入门
  • vr智慧党建主题展厅赋予企业数字化内涵
  • go中mutex的sema信号量是什么?
  • LeetCode笔记:Weekly Contest 360
  • 【树DP】2021ICPC南京 H
  • Leedcode19. 删除链表的倒数第 N 个结点
  • Mysql-索引查询相关
  • C++ Pimpl
  • rust学习-类型转换
  • 算法通过村第四关-栈青铜笔记|手写栈操作
  • Python计算加速利器
  • PyTorch 深度学习实践 第10讲刘二大人
  • Linux特殊指令
  • MPI之主从模式的一般编程示例
  • 基于野狗算法优化的BP神经网络(预测应用) - 附代码
  • C语言面向对象的编程思想
  • MPI之非阻塞通信中通信完成检测接口简介
  • Excel:如何实现分组内的升序和降序?
  • 深度学习论文: Segment Any Anomaly without Training via Hybrid Prompt Regularization
  • 【算法训练-字符串】一 最长无重复子串