Hutool-poi是针对Apache POI的封装,因此需要用户自行引入POI库,Hutool默认不引入。到目前为止,Hutool-poi支持:

Excel文件(xls, xlsx)的读取(ExcelReader)
Excel文件(xls,xlsx)的写出(ExcelWriter)

使用

引入POI依赖

推荐引入poi-ooxml,这个包会自动关联引入poi包,且可以很好的支持Office2007+的文档格式

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi.version}</version>
</dependency>

如果需要使用Sax方式读取Excel,需要引入以下依赖(POI-4.x以上这个非必须):

<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<version>${xerces.version}</version>
</dependency>

说明

hutool-4.x的poi-ooxml 版本需高于 3.17(别问我3.8版本为啥不行,因为3.17 > 3.8 )

hutool-5.x的poi-ooxml 版本需高于 4.1.2

hutool-5.6.x支持poi-ooxml 版本高于 5.0.0

xercesImpl版本高于2.12.0(非必须)

引入后即可使用Hutool的方法操作Office文件了,Hutool提供的类有:

ExcelUtil Excel工具类,读取的快捷方法都被封装于此

ExcelReader Excel读取器,Excel读取的封装,可以直接构造后使用。

ExcelWriter Excel生成并写出器,Excel写出的封装(写出到流或者文件),可以直接构造后使用。

常见问题

Maven依赖

<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.22</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>

示例接口

/**
* 导出对账单
*
* @param request request
* @param response response
*/
@RequestMapping(value = "/exportPaymentInfo", method = {RequestMethod.GET, RequestMethod.POST})
@ResponseBody
public void export(HttpServletRequest request, HttpServletResponse response) throws IOException {
SessionUser sessionUser = super.getSessionUser();
if (null == sessionUser) {
throw new BusinessException("用户登录状态过期");
}
OutputStream outputStream = null;
File file = null;
Object paymentVIewVOStr = redisTemplate.opsForValue().get("paymentVIewVO");
PaymentVIewVO paymentVIewVO;
if (null != paymentVIewVOStr) {
paymentVIewVO = JSON.parseObject(paymentVIewVOStr.toString(), PaymentVIewVO.class);
} else {
response.setContentType("application/json;charset=utf-8");
response.getWriter().write("{\n" +
" \"@type\": \"com.cztech.common.domain.model.Result\",\n" +
" \"msg\": \"ErrorCode:500, 导出失败,请重新查询数据再尝试导出\",\n" +
" \"success\": false\n" +
"}");
return;
}
paymentVIewVO.setFlag(true);
try {
String os = System.getProperty("os.name");
String filePath = "/tmp/tempPayment.xlsx";
if (os.toLowerCase().startsWith("win")) {
filePath = "d:/tmp/tempPayment.xlsx";
}
ExcelWriter writer = ExcelUtil.getWriter(filePath);
//自定义标题别名
writer.addHeaderAlias("successTime", "交易成功时间");
writer.addHeaderAlias("createTime", "交易创建时间");
writer.addHeaderAlias("orderNo", "业务订单号");
writer.addHeaderAlias("channelReturnMessage", "原业务订单号");
writer.addHeaderAlias("goodsName", "业务来源");
writer.addHeaderAlias("transactionType", "交易类型");
writer.addHeaderAlias("amount", "交易金额");
writer.addHeaderAlias("goodsType", "分账方收款主体");
writer.addHeaderAlias("price", "分账金额");
writer.addHeaderAlias("paymentNo", "渠道的商户订单号"); // 默认的,未添加alias的属性也会写出,如果想只写出加了别名的字段,可以调用此方法排除之
writer.setOnlyAlias(true);
Result<List> paymentVOList = paymentService.listReconciliationListALl(paymentVIewVO);
if (!paymentVOList.isSuccess()) {
response.setContentType("application/json;charset=utf-8");
response.getWriter().write("{\n" +
" \"@type\": \"com.cztech.common.domain.model.Result\",\n" +
" \"msg\": "+paymentVOList.getMsg()+",\n" +
" \"success\": false\n" +
"}");
return;
}
writer.write(paymentVOList.getResponse(), true);
writer.flush();
String code = paymentVOList.getCode();
if (StringUtils.isNotBlank(code)) {
paymentVIewVO.setFlag(false);
int currentNumber = Integer.parseInt(code);
int index = currentNumber % 1000;
for (int i = 0; i < index + 1; i++) {
paymentVIewVO.setCurrentPage(i + 2);
paymentVIewVO.setPageSize(currentNumber);
Result<List> paymentVOList2 = paymentService.listReconciliationListALl(paymentVIewVO);
if (paymentVOList2.isSuccess()) {
writer.write(paymentVOList2.getResponse(), true);
writer.flush();
}
}
}
writer.close();
response.setContentType("application/vnd.ms-excel;charset=utf-8");
String yyyyMMdd = DateUtil.format(new Date(), "yyyyMMdd");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(yyyyMMdd + "对账明细数据.xlsx", "UTF-8"));
outputStream = response.getOutputStream();
file = new File(filePath);
FileInputStream inputStream = new FileInputStream(file);
IOUtils.copy(inputStream, outputStream);
} catch (Exception e) {
e.printStackTrace(); } finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (file != null) {
if (file.exists()) {
file.delete();
}
}
}
}

SpringBoot+Hutool 文件导出Excel的更多相关文章

  1. Vue + axios + SpringBoot 2实现导出Excel

    Vue + axios + SpringBoot 2实现导出Excel 1. 前端js代码-发送Http请求 /** * 文件下载 * @param url 下载地址 * @param fileNam ...

  2. SpringBoot使用Easypoi导出excel示例

    SpringBoot使用Easypoi导出excel示例 https://blog.csdn.net/justry_deng/article/details/84842111

  3. springboot通过poi导出excel

    Maven引入依赖 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi< ...

  4. 文件导出Excel、Word、Pdf

    如果要将查询结果导出Excel,只要将页面的Context-Type修改下: header( "Content-Type: application/vnd.ms-excel"> ...

  5. SpringBoot之导入导出Excel

    1.添加springBoot支持 <dependency> <groupId>org.apache.poi</groupId> <artifactId> ...

  6. 使用hutool工具导出excel,使用jodd工具压缩流,使用oss上传流

    最近写了一个业务,用到一些比较实用的工具记录下. List<RobotCard> robotCardList = null; if (BizRouteEnum.ROUTE_1.getRou ...

  7. Springboot导出Excel并下载

    引入相关依赖 <!--数据导出excel--> <!-- https://mvnrepository.com/artifact/org.apache.poi/poi --> & ...

  8. Java基于注解和反射导入导出Excel

    代码地址如下:http://www.demodashi.com/demo/11995.html 1. 构建项目 使用Spring Boot快速构建一个Web工程,并导入与操作Excel相关的POI包以 ...

  9. [poi使用]使用excel模版导出excel

    ​ Apache POI是基于Office Open XML标准(OOXML)和Microsoft的OLE 2复合文档格式(OLE2)处理各种文件格式的开源项目.简而言之,您可以使用Java读写MS ...

  10. vite + vue3 + js + xlsx 导出excel

    安装依赖 1 npm install xlsx --save 使用版本 封装js /* 导出excel文件 */ /** * 导出excel文件实现思路分析 * * 1.通过XLSX插件的 XLSX. ...

随机推荐

  1. 解决pip命令报错及Python环境配置指南:从安装到优化

    1. 错误日志 当我在 Linux 机器(使用 Debian 或 Ubuntu 或衍生发行版)上运行 pip install xyz 时,会出现这样的错误: error: externally-man ...

  2. Vue.js 监听属性的使用

    示例源码: <div id = "computed_props"> 千米 : <input type = "text" v-model = & ...

  3. Slate文档编辑器-Decorator装饰器渲染调度

    Slate文档编辑器-Decorator装饰器渲染调度 在之前我们聊到了基于文档编辑器的数据结构设计,聊了聊基于slate实现的文档编辑器类型系统,那么当前我们来研究一下slate编辑器中的装饰器实现 ...

  4. 记一次cannot access its superinterface问题的的排查 → 强如Spring也一样写Bug

    开心一刻 昨天在幼儿园,领着儿子在办公室跟他班主任聊他的情况 班主任:皓瑟,你跟我聊天是不是紧张呀 儿子:是的,老师 班主任:不用紧张,我虽然是你的班主任,但我也才22岁,你就把我当成班上的女同学 班 ...

  5. 基于STC8G1K08的CH549单键进入USB下载模式实验

    一.实验原因 CH552或CH549进入USB下载,通常需要两个按键,一个控制电源的通断,一个通过串联电阻(一头接VCC或V33)冷启动时抬高UDP电平.时序上是这样的:断电--按下接UDP的轻触开关 ...

  6. Docker 多平台打包错误

    1. Multi-platform build is not supported for the docker driver. 问题 因为 Docker 默认使用的 builder 不支持多架构构建镜 ...

  7. Windows环境配置Nginx服务实现负载均衡

    系统环境:win10 测试服务:.net6.0+webapi 一.本地创建一个webapi项目 二.新建一个api控制器,里面编写一个测试方法 三.我直接把这一个项目复制了3份,然后修改控制器方法中的 ...

  8. UWP Shadow 阴影

    参考文字: https://mtaulty.com/2016/08/10/windows-10-uwp-and-composition-light-and-shade/ <Grid Backgr ...

  9. 云辅助隐私集合求交(Server-Aided PSI)协议介绍:学习

    原文来自:云辅助隐私集合求交(Server-Aided PSI)协议介绍,下面学习一波,并记录一些笔记. 背景 总结: 1.PSI-CA和PSI相比,前者在乎的是交集的大小,后者在乎的是交集本身.另外 ...

  10. 使用 Dify + LLM 构建精确任务处理应用

    在构建基于大语言模型(LLM)的应用时,如何确保返回结果的准确性和可重复性是一个常见的挑战.本文将结合 Dify + LLM 的使用经验,介绍如何设计一个精确的 LLM 任务处理流程,避免传统 LLM ...