How to get Excel Workbook Sheet names
Java Excel API is an open source java library to read, write and modify Excel spread sheets.
This requires the library jxl-2.6.12.jar to be in classpath.
The following example shows how to get the names of all spread sheets in an Excel workbook.
package com.bethecoder.tutorials.jexcelapi.read;
import java.io.File;
import java.io.IOException;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class GetSheetNamesTest {
/**
* @param args
* @throws IOException
* @throws BiffException
*/
public static void main ( String [] args ) throws BiffException, IOException {
//Read the given XL sheet
Workbook workbook = Workbook.getWorkbook ( new File ( "C:/JXL/Sheet-Names.xls" )) ;
System.out.println ( "Number of sheets in this workbook : " + workbook.getNumberOfSheets ()) ;
String [] sheetNames = workbook.getSheetNames () ;
for ( int i = 0 ; i < sheetNames.length ; i ++ ) {
System.out.println ( "Sheet Name[" + i + "] = " + sheetNames [ i ]) ;
}
//Close and free allocated memory
workbook.close () ;
}
}
It gives the following output,
Number of sheets in this workbook : 3
Sheet Name[0] = ABCD
Sheet Name[1] = PQR
Sheet Name[2] = XYZ