clex
clex

Reputation: 665

Apache POI & SXSSF: Number of rows always 0

I use SXSSFWorkbook and SXSSFSheet objects. I am able to write successfully into the excel file:

FileOutputStream fos = new FileOutputStream(fileName);
wb.write(fos);
fos.close();
wb.dispose();

After writing into the file I am loading/reading it again for further changes. Here is the problem, when I try to get the number of rows with getLastRowNum() I always get 0 as result. Here is a piece of code:

    SXSSFWorkbook wb = null;

    try {
        wb = new SXSSFWorkbook(new XSSFWorkbook(new FileInputStream(fileName)));
    } catch (FileNotFoundException e) {e.printStackTrace();
    } catch (IOException e) {e.printStackTrace();}

    SXSSFSheet sheet = wb.getSheet(sheetName);
    System.out.println(sheet.getLastRowNum());

Result is 0! I tried to create my own method using the iterator but that gives me the same result.

I don't have any error messages and if check the name of the sheet (sheet.getSheetName()) it returns the right name but not the right number of rows although the excel file has many simple rows.

Upvotes: 2

Views: 2390

Answers (1)

clex
clex

Reputation: 665

It is working for XSSFWorkbook

XSSFWorkbook workbook = null;
try {
    workbook = new XSSFWorkbook(new FileInputStream(fileName));
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
XSSFSheet sheet = workbook.getSheet(sheetName);
System.out.println(sheet.getLastRowNum());

This code snippet returns the result I need. I guess the SXSSF objects (e.g. SXSSFWorkbook are not suitable for reading although they have the appropriate methods for some reason.

Upvotes: 4

Related Questions