Get Excel SheetNames using POI jar
Asked Answered
G

2

6

I need all Excel sheet Names (what r all contains the datas) using POI jar. Like jxl jar - getSheetNames()

Graybeard answered 26/9, 2012 at 11:39 Comment(0)
L
16

You don't say how you want them, so I'll guess at a list. You just need to iterate over the sheet indicies, getting the name for each. Your code would be something like:

File myFile = new File("/path/to/excel.xls");
Workbook wb = WorkbookFactory.create(myFile);

List<String> sheetNames = new ArrayList<String>();
for (int i=0; i<wb.getNumberOfSheets(); i++) {
    sheetNames.add( wb.getSheetName(i) );
}
Luzon answered 26/9, 2012 at 13:14 Comment(2)
Is it possible to get excel sheet names without empty sheets.... As it is in JXL - getSheetNames()......Graybeard
You'd have to get each sheet as you went, and check the row count on it. All depends on how you define an empty sheet!Luzon
C
0

FileInputStream fis = new FileInputStream("C:\Users\Usr\Desktop\TestData.xlsx"); XSSFWorkbook wb = new XSSFWorkbook(fis);

    int sheetcount = wb.getNumberOfSheets();
    System.out.println("sheetcount: "+sheetcount);
    for(int i=0; i<sheetcount;i++)
    {
        System.out.print(wb.getSheetName(i)+ " ");
    }
Calathus answered 10/9, 2020 at 13:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.