People who use Excel often should know that excel2007 and above can easily store millions data, but how large amounts of data in the system can be quickly and accurately imported into Excel this seems like a challenge, for the general web system, we are going to solve the cost, Basic is the use of the entry-level Web server TOMCAT,JDK in 32 for the system to support the memory can not exceed 2 G, but there is no limit in 64, but in 64-bit system, performance is not very good, so in order to solve the appeal problem, we have to address our code to solve the problem we want to solve, After POI3.8 a new class, Sxssfworkbook, which can control the memory occupied by Excel data, he through the control of the number of rows in memory to achieve resource management, when more than the set number of rows, he will automatically refresh memory, write data such as files. But someone will say, I used this class ah, he does not seem to be able to completely solve, when the amount of data beyond certain will be memory overflow, and time is very long. You just use this class, but you're not designing for your needs, just using it, so the next thing I'm going to say is, if you're using Sxssfworkbook and the corresponding write design to make millions data fast written.
Let me give an example, before we have a large number of data in the database, we want to query, how to do? We do this in the absence of design, write a collection, then execute JDBC, assign the returned result to the list, and then return to the page, but when the data volume is large, there will be data can not be returned, memory overflow situation, so we in a limited time and space, By paging the data one page by page, this can avoid the large data volume data on the memory footprint, but also improve the user's experience, in the millions of data we want to export is also a reason, memory burst occupancy, we can limit the amount of memory used to export data, we set up a container, This container is a list,list open 10000 rows of storage space, each time the storage of 10000 rows, run out of content, and then reuse, so that you can effectively control the memory, so our design ideas are basically formed, so paging data export has the following 3 steps:
- Find the number of rows in the database to export data
- Extract number of data based on number of rows
- Write data to a file by number of times
The above steps have a high level of efficiency and user experience, and the following code
public void Exportamountexceldata (valuedatadto valuedatadto,string path) throws IOException {Sxssfworkbook wb = new SXSSF Workbook (15); Sheet sh = wb.createsheet (); Row row = Sh.createrow (0);//--------------------------------------------------Cell cel0 = Row.createcell (0); Cel0.setcellvalue ("1"); Cell Cel2 = Row.createcell (1); Cel2.setcellvalue ("2"); Cell cel3 = Row.createcell (2); Cel3.setcellvalue ("3"); Cell Cel4 = Row.createcell (3);//------------------define header----------------------------------------list< valuedatabean> list = new arraylist<valuedatabean> (), int page_size = 10000;//The number of rows of data stored in the database int list_count = THIS.D Aoutils.querylistcount (This.valueDataDao.queryExportSQL (valuedatadto). Get ("Count_sql")); int export_times = List_ Count% Page_size > 0? list_count/page_size+ 1:list_count/page_size;for (int j = 0; J < Export_times; J + +) {list = This.valueDataDao.que Rypagelist (This.valueDataDao.queryExportSQL (valuedatadto). Get ("List_sql"), J + 1,page_size); int len = List.size () &Lt Page_size? List.size (): page_size;for (int i = 0; i < len; i++) {Row Row_value = Sh.createrow (J * page_size + i + 1); Cell cel0_value = Row_value.createcell (0); Cel0_value.setcellvalue (List.get (i). Getaa ()); Cell Cel2_value = Row_value.createcell (1); Cel2_value.setcellvalue (List.get (i). Getaa ()); Cell Cel3_value = Row_value.createcell (2); Cel3_value.setcellvalue (List.get (i). Getaa_person ());} List.clear ();} FileOutputStream fileout = new FileOutputStream (path); Wb.write (fileout); Fileout.close (); Wb.dispose ();}
How Java uses POI to export millions data