Source: http://lyjilu.iteye.com/
To analyze the export implementation code, XLSX supports:
/*** Generate <span style= "white-space:normal; Background-color: #ffffff;" >xlsx</span>,2007 version of Excel, each sheet without a 6.5W line limit, but to a certain number, may be memory overflow, * The sub-method is suitable for the prediction of 10W or less data export use, native test, 14W can be exported. The number of columns is around 8 columns * *@paramfileout * Output stream *@paramSheetmap * Data information to set *@throwsSQLException*/ Public Static voidCreatexslxbyresultset (OutputStream fileout, Writexlsbean ... beans)throwsSQLException {Try { //Key WorkbookWorkbook WB =NewXssfworkbook (); for(inti = 0, len = beans.length; i < Len; i++) {Writexlsbean Xlsbean=Beans[i]; Sheet Sheet=Wb.createsheet (Xlsbean.getsheetname ()); ResultSet RS=Xlsbean.getrs (); ResultSetMetaData RSMD=Rs.getmetadata (); Typehandlerregistry TR=beancontext.tr; Map<string, string> th =Xlsbean.getth (); intindex = 0; while(Rs.next ()) {LongT1 =System.currenttimemillis (); Org.apache.poi.ss.usermodel.Row Row=sheet. CreateRow (index); for(intj = 0, NumberOfColumns = Rsmd.getcolumncount (); J < NumberOfColumns; J + +) {String key= Rsmd.getcolumnlabel (j + 1). toLowerCase (); if(Th.containskey (key)) {Typehandler<?> type =Tr.gettypehandler (jdbctype. Forcode (Rsmd.getcolumntype (J+ 1))); Object obj=Type.getresult (RS, key); Row.createcell (j). Setcellvalue (obj==NULL? "": Obj.tostring ()); }} System.out.println (Index+ ":" + (System.currenttimemillis ()-t1)); Index++; } } //Key WorkbookWb.write (fileout); } Catch(IOException e) {e.printstacktrace (); Throw NewServiceruntimeexception ("Production XLS document Error", E); } finally { } }
The above points are highlighted in two places, namely:
1. Building an Excel Object
2. Write the object to a OutputStream
In the construction process, there is no place to write Outputsteam, that is, the entire Excel must be built in memory in order to write operations, in the case of large data, this will cause all the data to load into memory, and not output, resulting in the last memory overflow.
Depending on the operating environment, there may be a different memory overflow situation
Depending on the situation, if the amount of data reaches above 10W, it is recommended to use
1, multiple Excel, one sheet per Excel, because all sheet are part of the workbook. If you don't divide multiple Excel, even sheet is useless,
2, each excel in a moderate number of columns, such as: 5W line each Excel file, the implementation of multiple export and paging query principle of the same
3, to multiple Excel export to a temporary directory, and through the program compression, and then provide to the customer download
Version 2003 is stored in the list with database results and then produced: Table is the list Row is the map
/*** Production of xls,2003 version of Excel, each sheet has a 6.5W line number limit * *@paramfileout * Output stream, not closed *@paramSheetmap * Data information to export*/ Public Static voidCreatexslbymap (OutputStream fileout, map<string, table<row<string, object>>>Sheetmap) { Try{Hssfworkbook WB=NewHssfworkbook (); Set<String> keys =Sheetmap.keyset (); for(Iterator<string> Iterator =Keys.iterator (); iterator. Hasnext ();) {String Sheetkey=Iterator.next (); Sheet Sheet=Wb.createsheet (Sheetkey); List<row<string, object>> sheetrows =Sheetmap.get (Sheetkey); for(inti = 0, Len = sheetrows.size (); i < Len; i++) {Map<string, object> cellmap =Sheetrows.get (i); Set<String> CellSet =Cellmap.keyset (); Org.apache.poi.ss.usermodel.Row Row=Sheet.createrow (i); intj = 0; for(Iterator<string> Itercell =cellset.iterator (); Itercell. Hasnext (); J++) {String Cellkey=Itercell.next (); Object obj=Cellmap.get (Cellkey); Row.createcell (j). Setcellvalue (obj==NULL? "": Obj.tostring ()); }}} wb.write (Fileout); } Catch(IOException e) {e.printstacktrace (); Throw NewServiceruntimeexception ("Production XLS document Error", E); } finally { } }
New version Poi+office 2007 version Excel can export hundreds of thousands of without memory overflow, see details:
Export large amounts of data to an xlsx file in Excel
Static String src= " Abcdefafslfelgtryjukjhgfdadertjdsfghjkjghfertuioabcdefafslfelgtryjukjhgfdadertjdsfghjkdertjdsfghjkjghfertuioabcdefafslfel Gtryjukjhgfdadertjdsfghjkjghfertuio ";PublicStaticvoid Main (string[] args)Throwsthrowable {Sxssfworkbook WB =New Sxssfworkbook (100);//Here 100 is the amount in memory, if greater than this number, will be written to the hard disk to avoid causing memory overflow in memory Sheet sh =Wb.createsheet ();for (int rownum = 0; rownum < 1000000; rownum++) {row row = Sh.createrow (rownum); For (int cellnum = 0; cellnum < cellnum++) {cell cell = Row.createcell (cellnum); String address = new cellreference (cell). formatasstring (); Cell.setcellvalue (Address+src.substring (rownum% 10*10+1, (rownum%10+1) *10)); }} File File = new file ("F:/aa.xlsx"); File.createnewfile (); FileOutputStream out = new fileoutputstream (file), Wb.write (out); Out.close ();}
Memory usage:
Java Export large amount of Excel data