平时很少用到生产excel文档,避免遗忘特此记录
我用的是 hutool 工具类,这是一个很全面的工具类
maven
<!--hutool -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.5.1</version>
</dependency>
<!-- poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.0.0</version>
</dependency>
代码可以直接跑,在url测试会直接下载excel
@RestController
public class TestController {
/**
* 下载 excel 到本地
* @param response
* @throws IOException
*/
@RequestMapping("download1")
public void download1(HttpServletResponse response) throws IOException {
//随便创建一点数据
Map<String, Object> row1 = new LinkedHashMap<>();
row1.put("姓名", "张三");
row1.put("年龄", 23);
row1.put("成绩", 88.32);
row1.put("是否合格", true);
row1.put("考试日期", DateUtil.date());
Map<String, Object> row2 = new LinkedHashMap<>();
row2.put("姓名", "李四");
row2.put("年龄", 33);
row2.put("成绩", 59.50);
row2.put("是否合格", false);
row2.put("考试日期", DateUtil.date().toString());
ArrayList<Map<String, Object>> rows = CollUtil.newArrayList(row1, row2);
// 通过工具类创建writer,默认创建xls格式
ExcelWriter writer = ExcelUtil.getWriter();
// 一次性写出内容,使用默认样式,强制输出标题
writer.write(rows, true);
//out为OutputStream,需要写出到的目标流
//response为HttpServletResponse对象
response.setContentType("application/vnd.ms-excel;charset=utf-8");
//test.xls是弹出下载对话框的文件名,不能为中文,中文请自行编码
//response.setHeader("Content-Disposition", "attachment;filename=测试文件.xls");
String excelName="测试文件.xls";
// 在设置response.setHeader时,如果含有中文字符,一定要转换成ISO8859-1格式,否则设置的中文会出现错误。
response.setHeader("content-disposition","attachment;filename="+new String(excelName.getBytes("gb2312"), "ISO8859-1"));
ServletOutputStream out = response.getOutputStream();
writer.flush(out, true);
// 关闭writer,释放内存
writer.close();
//此处记得关闭输出Servlet流
IoUtil.close(out);
}
/**
* 如果实体类里面是name excel 里面想变成 姓名字段 用如下方法
* @param response
* @throws IOException
*/
@RequestMapping("download2")
public void download2(HttpServletResponse response) throws IOException {
//构造假数据
TestBean bean1 = new TestBean();
bean1.setName("张三");
bean1.setAge(22);
bean1.setPass(true);
bean1.setScore(66.30);
bean1.setExamDate(DateUtil.date());
TestBean bean2 = new TestBean();
bean2.setName("李四");
bean2.setAge(28);
bean2.setPass(false);
bean2.setScore(38.50);
bean2.setExamDate(DateUtil.date());
List<TestBean> rows = CollUtil.newArrayList(bean1, bean2);
// 通过工具类创建writer,默认创建xls格式
ExcelWriter writer = ExcelUtil.getWriter();
//自定义标题别名
writer.addHeaderAlias("name", "姓名");
writer.addHeaderAlias("age", "年龄");
writer.addHeaderAlias("score", "分数");
writer.addHeaderAlias("isPass", "是否通过");
writer.addHeaderAlias("examDate", "考试时间");
// 合并单元格后的标题行,使用默认标题样式
writer.merge(4, "一班成绩单");
// 一次性写出内容,使用默认样式,强制输出标题
writer.write(rows, true);
//out为OutputStream,需要写出到的目标流
//response为HttpServletResponse对象
response.setContentType("application/vnd.ms-excel;charset=utf-8");
//test.xls是弹出下载对话框的文件名,不能为中文,中文请自行编码
//response.setHeader("Content-Disposition", "attachment;filename=测试文件.xls");
String excelName="测试文件.xls";
// 在设置response.setHeader时,如果含有中文字符,一定要转换成ISO8859-1格式,否则设置的中文会出现错误。
response.setHeader("content-disposition","attachment;filename="+new String(excelName.getBytes("gb2312"), "ISO8859-1"));
ServletOutputStream out = response.getOutputStream();
writer.flush(out, true);
// 关闭writer,释放内存
writer.close();
//此处记得关闭输出Servlet流
IoUtil.close(out);
}
}
会用到的实体类
@Data
public class TestBean {
private String name;
private int age;
private double score;
private boolean isPass;
private Date examDate;
}