前言
在开发应用系统的时候,导出文件是必不可放的功能。
以前用过POI、easyexcel等工具的导入导出功能,但总感觉太麻烦了,代码特别多,感觉并不是很好用。
今天给大家介绍一款新工具,java工具类库Hutool。
Hutool简介
Hutool是一个小而全的Java工具类库,通过静态方法封装,降低相关API的学习成本,提高工作效率,使Java拥有函数式语言般的优雅,让使用者更轻松。
Hutool中的工具方法来自于每个用户的精雕细琢,它涵盖了Java开发底层代码中的方方面面,它既是大型项目开发中解决小问题的利器,也是小型项目中的效率担当;
Hutool是项目中“util”包友好的替代,它节省了开发人员对项目中公用类和公用工具方法的封装时间,使开发专注于业务,同时可以最大限度的避免封装不完善带来的bug。
使用
首先在POM.xml中加入GAV
pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.taotao</groupId>
<artifactId>hutool</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hutool</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--hutool-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.0.7</version>
</dependency>
<!--hutool-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.1</version>
</dependency>
<!--hutool-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-commons</artifactId>
<version>RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
实体:
package com.taotao.hutool.entity;
import cn.hutool.core.date.DateTime;
import lombok.Data;
import java.util.Date;
@Data
public class User {
private String name;
private String age;
private Date birthDay;
public User(String name, String age, Date birthDay) {
this.name = name;
this.age = age;
this.birthDay = birthDay;
}
}
controller
package com.taotao.hutool.controller;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.taotao.hutool.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* 通过hutool 工具导出excel
*/
@Slf4j
@RestController
public class HutoolController {
@RequestMapping("/export")
public void export(HttpServletResponse response){
Date date=new Date();
List<User> list=new ArrayList<>();
list.add(new User("zhangsan","1231", date));
list.add(new User("zhangsan1","1232", date));
list.add(new User("zhangsan2","1233",date));
list.add(new User("zhangsan3","1234", date));
list.add(new User("zhangsan4","1235", date));
list.add(new User("zhangsan5","1236",date));
//通过工具类创建writer,默认创建xls格式
ExcelWriter writer= ExcelUtil.getWriter();
//自定义标题别名
writer.addHeaderAlias("name","姓名");
writer.addHeaderAlias("age","年龄");
writer.addHeaderAlias("birthDay", "生日");
//合并单元格后的标题行,使用默认标题样式
writer.merge(2,"申请人员信息");
//一次性写出内容,使用默认样式,强制输出标题
writer.write(list,true);
//out 为 OutputStream, 需要写出到的目标流
//response为HttpServletResponse对象
response.setContentType("application/vnd.ms-excel;charset=utf-8");
//test.xls是弹出下载对话框的文件名,不能中文,中文请自行编码
String name = "申请单位";
response.setHeader("Content-Disposition","attachment;filename="+name+".xls");
ServletOutputStream out=null;
try {
out=response.getOutputStream();
writer.flush(out,true);
}catch (Exception e){
e.getCause();
}finally {
//关闭writer,释放内存
writer.close();
}
//此处记得关闭输出Servlet流
IoUtil.close(out);
}
}