java - Appending to a workbook using poi sxssfworkbook -
i need append rows sheet of workbook. using org.apache.poi.xssf.streaming.sxssfworkbook not able achieve low memory footprint. following code:
import java.io.bufferedinputstream; import java.io.fileinputstream; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstream; import java.util.arraylist; import java.util.hashmap; import java.util.iterator; import org.apache.poi.ss.usermodel.cell; import org.apache.poi.ss.usermodel.row; import org.apache.poi.ss.usermodel.sheet; import org.apache.poi.xssf.streaming.sxssfworkbook; import org.apache.poi.xssf.usermodel.xssfworkbook; public class excelhelper { public static void createexcelfilewithlowmemfootprint( arraylist<hashmap<string, object>> data, arraylist<string> fieldnames, string filename, int rownum) { try { if (rownum == 0) { // creating new workbook , writing top heading here sxssfworkbook workbook = new sxssfworkbook(1000); sheet worksheet = workbook.createsheet("sheet 1"); int = 0; iterator<string> it0 = fieldnames.iterator(); row row = worksheet.createrow(i); int j = 0; while (it0.hasnext()) { cell cell = row.createcell(j); string fieldname = it0.next(); cell.setcellvalue(fieldname); j++; } rownum++; fileoutputstream fileout = new fileoutputstream(filename); workbook.write(fileout); fileout.flush(); fileout.close(); } inputstream filein = new bufferedinputstream(new fileinputstream( filename), 1000); sxssfworkbook workbook = new sxssfworkbook( new xssfworkbook(filein), 1000); sheet worksheet = workbook.getsheetat(0); iterator<hashmap<string, object>> = data.iterator(); int = rownum; while (it.hasnext()) { row row = worksheet.createrow(i); int j = 0; hashmap<string, object> rowcontent = it.next(); iterator<string> it1 = fieldnames.iterator(); while (it1.hasnext()) { cell cell = row.createcell(j); string key = it1.next(); object o = rowcontent.get(key); if (o instanceof string) { cell.setcellvalue((string) o); } else if (o instanceof double) { cell.setcelltype(cell.cell_type_numeric); cell.setcellvalue((double) o); } j++; } i++; } filein.close(); fileoutputstream fileout = new fileoutputstream(filename); workbook.write(fileout); fileout.flush(); fileout.close(); } catch (filenotfoundexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } } }
i appending file passing content in batches(so save on jvm memory) , incrementing variable rownum.
as per understanding, when re-opening file
sxssfworkbook workbook = new sxssfworkbook(new xssfworkbook(filein),1000);
the constructor xssworkbook reloads complete file in memory, result in gc limit exceeded.
i went through http://poi.apache.org/spreadsheet/how-to.html unable find suitable solution usecase.
can guys please suggest how fix achieve low memory footprint appending rows workbook?
sxssfworkbook
doesn't need output loaded in memory management. write of data @ once. if try loading whole workbook stores in memory, when writing @ once uses storage space instead. 1000
lot put in constructor on computers. if want, try putting 100
or other lower number in constructor instead of 1000
.
Comments
Post a Comment