Hutool Java tool class library export Excel, super simple!

Posted by BluntedbyNature on Sat, 08 Feb 2020 09:27:50 +0100

When developing an application system, exporting files is a necessary function.

I've used the import and export functions of POI, easyexcel and other tools before, but I always feel that it's too troublesome, there are too many codes, and I don't think it's very easy to use them.

Today, I'd like to introduce a new tool, the java tool class library Hutool.
Hutool introduction
Hutool is a small and complete Java tool class library. Through static method encapsulation, it can reduce the learning cost of relevant API, improve the working efficiency, make java have the elegance of functional language, and make users more relaxed.

The tools and methods in Hutool come from each user's elaboration. It covers all aspects of the Java development underlying code. It is not only a sharp tool to solve small problems in the development of large projects, but also an efficiency person in small projects;

Hutool is a "util" package friendly alternative in the project. It saves the time for developers to encapsulate the common classes and methods in the project, makes the development focus on the business, and at the same time, it can avoid the bug s caused by incomplete encapsulation to the greatest extent.
First, add GAV to POM.xml
















And then use it in the control layer



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", Date())));

// Create writer through tool class, and create xls format by default

ExcelWriter writer = ExcelUtil.getWriter();

//Custom title alias

writer.addHeaderAlias("name", "Full name");

writer.addHeaderAlias("age", "Age");

writer.addHeaderAlias("birthDay", "Birthday");

// Title row after merging cells, using default title style

writer.merge(2, "Applicant information");

// Write out the content once, use the default style, and force the title output

writer.write(list, true);

//Out is OutputStream, the target stream to be written out

//response is the HttpServletResponse object


//test.xls is the file name of the pop-up download dialog box. It cannot be Chinese. Please code the Chinese by yourself

String name = StringUtils.toUtf8String("Application College");


ServletOutputStream out= null;

try {

out = response.getOutputStream();

writer.flush(out, true);

} catch (IOException e) {


}finally {

// Close writer and free memory



//Remember to close the output Servlet stream here




Published 60 original articles, won praise 4, visited 20000+
Private letter follow

Topics: Java Apache xml Excel