简体   繁体   中英

HSSFWorkbook read Performance is slow

I am fairly new to programming with Android and am looking for some help with a program I am reading that reads and excel file that currently contains 30 sheets. When HSSFWorkbook read the file it can take between 10 to 20 seconds before the app continues running. Is there any way I can improve this? each sheet contains 40 rows and 13 columns. I currently look for a specific sheet one at a time, so I dont need the rest until a later call.

I cant use XSSF either due to problems with javax.xml.stream

HSSFWorkbook wb = new HSSFWorkbook(pfile);

        trimDate();
        HSSFSheet sheet;
        sheetD = wb.getSheetIndex(finalDate);
        sheet = wb.getSheetAt(sheetD);
        FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
        // TODO: 8/16/2017  remove try when current Excel File is used
        Row row = null;
        try{row = sheet.getRow(2);}
        catch (NullPointerException e){
            error();
            e.printStackTrace();
            return;
        }
        Cell cell = row.getCell(11);
        TextView pt = (TextView) findViewById(R.id.period_total);
        CellValue cellValue = evaluator.evaluate(cell);

You can try using SXSSFWorkbook instead, it works fine with large Excel sheets as it does not store the whole footprint into the memory. Here's the documentation of it:

SXSSF is an API-compatible streaming extension of XSSF to be used when very large spreadsheets have to be produced, and heap space is limited. SXSSF achieves its low memory footprint by limiting access to the rows that are within a sliding window, while XSSF gives access to all rows in the document. Older rows that are no longer in the window become inaccessible, as they are written to the disk.

As this does not store the whole file in memory, you might need to tweak your logic a bit as you won't be able to go back to any of the previous rows once you have read the contents.

Here are the examples of reading a file with SXSSFWorkbook .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM