centic9 / poi-on-android

A sample project that shows how Apache POI can be used in an Android application
Apache License 2.0
343 stars 89 forks source link

A quick question regarding performance (fastness of reading writing) #117

Open Tanzin01 opened 2 months ago

Tanzin01 commented 2 months ago

Hello there, hope you are having a good day

Short version: Is this wrapper faster?

here i go: I tried the apacge poi library for a file which have 62 cells. Only 7 cells have minimal text, rests are empty. However to my wonder, its taking quite some time to read the cells and store them in a list variable as a result recyclerView appears a lot later than my other views makes ui screen unintuitive and odd.

So, i was wondering if your wrapper version would be faster? I used them-

implementation 'org.apache.poi:poi-ooxml:5.2.3' // Use an older version that doesn't rely on MethodHandle
    implementation 'org.apache.poi:poi:5.2.3'// Use an older version that doesn't rely on MethodHandle

And the code(if it explains more)-

public List<List<String>> readExcel() {
    List<List<String>> rows = new ArrayList<>();
    try (FileInputStream fis = new FileInputStream(filePath);
         Workbook workbook = WorkbookFactory.create(fis)) {

        Sheet sheet = workbook.getSheetAt(0); // Assuming we are reading the first sheet

        int batchSize = Math.min(100, sheet.getPhysicalNumberOfRows()); // Read rows in batches, up to 100 rows
        int rowCount = sheet.getPhysicalNumberOfRows();
        for (int startRow = 0; startRow < rowCount; startRow += batchSize) {
            int endRow = Math.min(startRow + batchSize, rowCount);
            for (int rowNum = startRow; rowNum < endRow; rowNum++) {
                Row row = sheet.getRow(rowNum);
                if (row != null) {
                    List<String> cells = new ArrayList<>();
                    for (int colNum = 0; colNum < row.getLastCellNum(); colNum++) {
                        Cell cell = row.getCell(colNum, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
                        String cellValue = getCellValueAsString(cell);
                        cells.add(cellValue);
                    }
                    rows.add(cells);
                }
            }
        }
    } catch (IOException e) {
        Log.e(TAG, "Error reading Excel file", e);
    }
    return rows;
}

or am i doing something terribly wrong?

centic9 commented 2 months ago

Code looks fine on a quick look.

But with the description above I am not sure what exactly is slow for you: do you see slowness when using the poi-on-android jar-file compared to some other approach or do you see slowness in one version compared to another?

For analyzing performance issues it is often a very good idea to use some sort of profiling to analyze what actually causes the issue as the problem very often is not what you initially expect.