Search code examples
javaexcelexcel-2003

Count number of worksheets in Excel File


How to count number of worksheets in a Microsoft Excel file using Java SE?


Solution

  • There's no standard class/library files in Java SE that interfaces with MS Excel. In Apache POI, you can use HSSFWorkbook.getNumberOfSheets() method which returns you the number of worksheet from a workbook.


    To open an Excel file and get HSSFWorkbook, do this:

    String fileName = "C://Excel.xls";
    POIFSFileSystem fileSystem = new POIFSFileSystem(new FileInputStream(fileName));
    HSSFWorkbook workbook = new HSSFWorkbook(fileSystem);