Count number of worksheets in Excel File

Shumon Saha picture Shumon Saha · Aug 1, 2011 · Viewed 17.3k times · Source

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

Answer

Buhake Sindi picture Buhake Sindi · Aug 1, 2011

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);