Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Today we are going to talk about implementing requirements for imports and exports:

Background:

After the data synchronization after, the boss said today, I want to let customers directly intuitive can see the data, but it does not want to build a new, you and 🈶 what can be efficient method, import ah;

One solution I proposed is to directly import the data we need into the system platform according to a template, so as to meet the requirement of displaying new data.

Import and export is we often used to direct the new data set, export more than for data statistics, such as the month’s performance, attendance, such as nailing will support the export of clock in that month, that is a more typical export, export format is EXCEL, which also want to make sure that the export is a version of the 2007-13, suffix is XSLX, XSL format,

For the import and export function, we are common according to the POI, Apache open source organization for EXCEL export, for details, you can see my last article: xie.infoq.cn/article/8c0… (About POI), this is also the official class library, support office suite data statistics tools;

Now it is 2021, and class libraries have emerged in an endless stream. Therefore, THIS time I recommend a tool about direct use, named Hutool, also known as “confused”, that is, you can be confused to know that it is used, because its bottom layer has encapsulated many good class libraries. Official website address www.hutool.cn/(home page)

This is one of my favorite tools, and for Java developers, it can greatly improve your development efficiency. The underlying package is very efficient editing, which mainly includes: we have JSONutil (JSON dependency), ID (custom increment ID), timestamp, HTTP request, filter, type conversion, And data verification, of course, also lack, today’s small topic:

www.hutool.cn/docs/#/

For common components, we can import a single POM file and add dependencies. If we want to use all of them, we can also use hutool-all.

Next time, how to use Hutool to export specific functions

Today, we are going to talk about how HUTOOL is familiar with how to display exported data files. Here, we use Excel export as a scheme.

Excel export:

Users want to make statistics and display the data intuitively. They don’t like to compare the data line by line. They want to summarize a table and report to the boss.

Hutool writes and encapsulates Excel as ExcelWriter, which encapsulates the underlying layer of the previous POI. The supported version is better.

We start by adding dependencies:

<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>${poi.version}</version> </dependency> <groupId>cn. Hutool </groupId> <artifactId>hutool-poi</artifactId> The < version > 4.3.5 < / version > < / dependency >Copy the code

With this dependency, we can use EXCEL directly

1. Add dependencies

Here I mainly write about the export file, the response stream is processing, that is, directly sent to the servlet client for download;

We put the data in the file, and the file is sent in the return stream;

ExcelWriter writer = ExcelUtil.getwriter (); Write (rows, true); // Write the content once, using the default style, forcing the output title writer.write(rows, true); // Response is the HttpServletResponse object response.setContentType(" Application /vnd.ms-excel; charset=utf-8"); Response. setHeader(" content-disposition ","attachment; filename=test.xls"); ServletOutputStream out=response.getOutputStream(); writer.flush(out, true); Writer.close (); // Close writer.close(); // Remember to close the output Servlet stream ioutil.close (out);Copy the code

2. Notice that I/O is disabled

When exporting, close the I/O

private void write(ExcelWriter writer, HttpServletResponse response) { OutputStream out = null; try { out = response.getOutputStream(); writer.flush(out); Writer.close (); // Close writer.close(); // Remember to close the output Servlet stream ioutil.close (out); } catch (IOException e) {log.error(" error ", e); } finally { IoUtil.close(out); }}Copy the code

For how data is written to a file:

3. Write data

The most important method is:

ExcelWriter writer = ExcelUtil.getWriter(true);
        this.exportWriter(writer);
        writer.write(rows);
Copy the code

Where row is writing our own data to Excel,

List<Test> rows = CollUtil.newArrayList(); // Create a new data set. For writing data, create one row at a timeCopy the code

Because the example on the official website has been very clear, the above code is the actual development I encountered in the enterprise, I hope you can help you,

www.hutool.cn/docs/#/poi/… (Excel data generation)