apache poi - XLSX removing sheets OutOfMemory Exception -


i trying load xlsx file using poi library has 5 sheets. size of file 5 mb. total records in sheets around 30,000. once file loaded need delete 1 or more sheets on fly based on sheet neame input.

here snippet.

 public void generatereportworkbook(string[] requestedreports) throws exception {       // read file       string dailyticketreport = reportconstants.report_path + reportconstants.file_name + reportconstants.xlsx_file_extn;        fileinputstream fis = null;       xssfworkbook book  = null;        try {           fis = new fileinputstream(dailyticketreport);            book = new xssfworkbook(fis);           (int = book.getnumberofsheets() - 1; >= 0; i--) {               xssfsheet tmpsheet = book.getsheetat(i);               if (!arrayutils.contains(requestedreports, tmpsheet.getsheetname())) {                   book.removesheetat(i);               }           }       } catch (exception e) {           logger.error("error occured while removing sheets workbook");           throw e;       } {           ioutils.closequietly(fis);       }     }   

when execute program. outofmemory exception. how can remove sheets without memory issue.

i faced same issue of oom while parsing xlsx file...after 2 days of struggle, found out below code perfect;

this code based on sjxlsx. reads xlsx , stores in hssf sheet.

            // read xlsx file        simplexlsxworkbook = new simplexlsxworkbook(new file("c:/test.xlsx"));          hssfworkbook hsfworkbook = new hssfworkbook();          org.apache.poi.ss.usermodel.sheet hsfsheet = hsfworkbook.createsheet();          sheet sheettoread = workbook.getsheet(0, false);          sheetrowreader reader = sheettoread.newreader();         cell[] row;         int rowpos = 0;         while ((row = reader.readrow()) != null) {             org.apache.poi.ss.usermodel.row hfsrow = hsfsheet.createrow(rowpos);             int cellpos = 0;             (cell cell : row) {                 if(cell != null){                     org.apache.poi.ss.usermodel.cell hfscell = hfsrow.createcell(cellpos);                     hfscell.setcelltype(org.apache.poi.ss.usermodel.cell.cell_type_string);                     hfscell.setcellvalue(cell.getvalue());                 }                 cellpos++;             }             rowpos++;         }         return hsfsheet; 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -