Count number of worksheets in Excel File

18,501

Solution 1

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

Solution 2

Use getNumberOfSheets() in the WritableWorkbook class.

Take a look at these:

jxl.Workbook;
jxl.write.Label;
jxl.write.WritableSheet;
jxl.write.WritableWorkbook;

http://jexcelapi.sourceforge.net/resources/javadocs/2_6_10/docs/jxl/write/WritableWorkbook.html

Solution 3

USE THE FOLLOWING CODE TO GET number of worksheets

FileInputStream file = new FileInputStream(new File(FILE PATH));            

XSSFWorkbook workbook = new XSSFWorkbook(file);         

System.out.println("number of sheet::"+ workbook.getNumberOfSheets());
Share:
18,501
Shumon Saha
Author by

Shumon Saha

Updated on June 29, 2022

Comments

  • Shumon Saha
    Shumon Saha almost 2 years

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