Hutool Java class libraries tool to export Excel, super easy!

Introduction
In the development and application of the system, the export file is indispensable to put the function.

Previously used POI, easyexcel tools such as import and export functions, but the overall feeling too much trouble, particularly the code, I feel not very good use.

Today to introduce a new tool, java utility libraries Hutool.
Hutool About
Hutool tool is a small but complete Java class libraries, static method of packaging, reduce learning costs associated API, improve work efficiency, so that Java has a functional language like elegance, allowing users easier.

The utility methods Hutool from each user's crafted, it covers all aspects of the development of the underlying Java code, it is both large-scale project development tool to solve small problems, but also the efficiency of small projects in play;

Hutool is the project "util" package friendly alternative, it saves time for developers to package the project in public classes and public utility methods, allowing developers to focus on business, while the maximum to avoid the bug caused by imperfect package.
Use
first added in the GAV in POM.xml

<dependency>

<groupId>cn.hutool</groupId>

<artifactId>hutool-all</artifactId>

<version>5.0.7</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>

Then the layer is formed using the control line

@RequestMapping("/export")

@ResponseBody

public void export(HttpServletResponse response){

List<User> list = new ArrayList<>();

list.add(new User("zhangsan","1231",new Date()));

list.add(new User("zhangsan1","1232",new Date()));

list.add(new User("zhangsan2","1233",new Date()));

list.add(new User("zhangsan3","1234",new Date()));

list.add(new User("zhangsan4","1235",new Date()));

list.add(new User("zhangsan5","1236", DateUtil.date(new 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 = StringUtils.toUtf8String("申请学院");

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,释放内存

writer.close();

}

//此处记得关闭输出Servlet流

IoUtil.close(out);

}

effect
Here Insert Picture Description

Published 60 original articles · won praise 4 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_20282955/article/details/104224085