Java Solution for POI export Excel memory Overflow

Source: Internet
Author: User
Java Solution for POI export Excel memory Overflow

In our use of Java development process, we often want to export the data obtained by the query, which is generally stored in Excel, so we export the data to use the Java POI Library, which is mainly for the various Windows Platform data format operation, here, We are working on Excel.

The process of generating Excel is like this, first of all, we query the database, get the corresponding result set, is generally a list collection, and then generate the workbook object, according to the generated workbook object gets the sheet object, according to this sheet object to get the Row object, Then we get the cell object according to the row object, finally set the cell cell value, loop to build the row and cell object, write the value in the list to the cell, write the workbook object to the corresponding stream object.

In the above process, if the list does not have a lot of data, there will be no problem, if the list of data enough (more than tens of thousands of), it will cause memory overflow, the export failed.

The solution is that we export it in batches, each time we query a fixed number of list data sets, write the data into a Workbook object, loop through it, and then write all of the data as workbook, which generates multiple workbook objects. There is also a problem when it comes to downloading the browser, so we will also pack and compress the generated multiple Excel files, that is, the tar command that invokes the operating system (which is typically deployed in a Linux environment) to generate a compressed package that can be downloaded.

Note: In the OutputStream stream where the Workbook object is written, its path cannot be a path in the Web container when the OutputStream is built, otherwise a workbook object can never be generated for unknown reasons

Java Solution for POI export Excel memory Overflow

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.