Preface
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.
Use
First, add GAV to 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>
And then use it in the control layer
@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()))); // 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 response.setContentType("application/vnd.ms-excel;charset=utf-8"); //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"); 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 { // Close writer and free memory writer.close(); } //Remember to close the output Servlet stream here IoUtil.close(out); }
Effect