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. 欧拉OpenEuler安装MySQL8

    1. 安装mysql tar -xvf mysql-8.0.21-linux-glibc2.12-x86_64.tar mv mysql-8.0.21-linux-glibc2.12-x86_64 / ...

  2. [转]火狐浏览器访问github提示:未连接:有潜在的安全问题...github.com 启用了被称为 HTTP 严格传输安全(HSTS)的安全策略,Firefox 只能与其建立安全连接。

    火狐浏览器访问github,提示:        未连接:有潜在的安全问题:        Firefox 检测到潜在的安全威胁,并因 github.com 要求安全连接而没有继续.如果这种情况是因为 ...

  3. 推荐 5 个 火火火火 的CMS开源.Net项目

    下面推荐5个基于.NetCore开发的CMS开源项目. 一.OrchardCore 基于ASP.NET Core 构建的.模块化和多租户应用程序框架,采用文档数据库,非常高性能,跨平台的系统. 1.跨 ...

  4. Python学习(一)——配套《PyTorch深度学习实战》

    记录一下Python学习过程中的一些问题: 1. 在JupyterLab中查询当前文件的地址 import os print(os.getcwd()) #查询该文件的地址 2. 新建cell 在 Ju ...

  5. Windows下的GDAL环境配置

    Windows下的GDAL环境配置 在Windows下GDAL环境的配置方式有很多种,我尝试了很多不同的配置方式,包括Anaconda. GISInternals.QGIS.OSGeo4W等, 我这里 ...

  6. h5 页面播放base64编码的audio数据

    例子: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...

  7. Java基础总结,超级全的面试题-copy

    1. static关键字是什么意思?Java 中是否可以覆盖(override)一个 private 或者是 static 的方法?是否可以在 static 环境中访问非static 变量? stat ...

  8. 第三章 (Nginx+Lua)Redis/SSDB安装与使用

    目前对于互联网公司不使用Redis的很少,Redis不仅仅可以作为key-value缓存,而且提供了丰富的数据结果如set.list.map等,可以实现很多复杂的功能:但是Redis本身主要用作内存缓 ...

  9. K8S故障处理:临时设置节点为不可调度(cordon与drain区别)

    在Kubernetes中,节点驱逐是一种管理和维护集群的重要操作,允许节点在维护.升级或者发生故障时从集群中移除,等到节点修复后,再重新承担pod调度功能. 1.K8s节点驱逐 节点驱逐是指将节点上运 ...

  10. 对rpc长连接与短连接的思考

    大家好,我是思无邪,某go中厂开发工程师,也是OSPP2024的学生参与者! 如果你觉得我的文章有帮助,记得三连支持一下哦! 目前正在深入研究源码,与你们一起进步,共同攻克编程难关! 欢迎关注我的公众 ...