首先我们来认识下hutool是个什么东西?
简介
Hutool是一个小而全的Java工具类库,通过静态方法封装,降低相关API的学习成本,提高工作效率,使Java拥有函数式语言般的优雅,让使用者更轻松。
以前使用POI、easyexcel等工具的导入导出功能,但总感觉太麻烦了,代码特别多
基于hutool导入导出代码简洁明快
话不多说请看:
pom导入相关依赖
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.17</version>
</dependency>
/**
* @Title: downloadExcle
* @ProjectName plan
* @Description: 基于hutool导出excle
* @Author dingyf
* @Version 2.0
*/
@Api(tags = "下载", description = "基于hutool导出excle接口")
@RestController
@RequestMapping("/businessClassify")
public class downloadExcle {
@ApiOperation("基于hutool导出excle")
@PostMapping("/downloadExcle")
public ResponseResult downloadExcle() {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletResponse response = attributes.getResponse();
List<Tag> tags = new ArrayList<Tag>();
for (int i = 0; i < tags.size(); i++) {
tags.add(new Tag(1,2) );
}
ExcelWriter writer = ExcelUtil.getWriter();
writer.addHeaderAlias("userName", "姓名");
writer.addHeaderAlias("age", "年龄");
writer.merge(1, "XXX表");
writer.write(tags, true);
response.setContentType("application/vnd.ms-excel;charset=utf-8");
String name = "XXX研发体系";
response.setHeader("Content-Disposition", "attachment;filename=" + name + ".xls");
ServletOutputStream out = null;
try {
out = response.getOutputStream();
writer.flush(out, true);
} catch (IOException e) {
e.printStackTrace();
} finally {
writer.close();
}
IoUtil.close(out);
}
}
上述是简单列表导出
复杂动态跨行跨列合并的导出如下:
要求如下
1:相同行合并
2:相同列合并
3:深度自定义
参考我的博客地址:记录了详细的实现方式
https://blog.csdn.net/duck_zixi?spm=1011.2124.3001.5343
总结:
hutool工具包十分强大有劲 内容丰富 值得推荐
api址:
https://apidoc.gitee.com/loolly/hutool/cn/hutool/poi/excel/ExcelWriter.html
文档地址:
https://hutool.cn/docs/#/





