tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Excel > JExcel API > How to get Excel Workbook Sheet by index

How to get Excel Workbook Sheet by index 

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 Excel workbook spread sheet by its index.

sheet-names.gif

File Name  :  
com/bethecoder/tutorials/jexcelapi/read/GetSheetsByIndexTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.jexcelapi.read;

import java.io.File;
import java.io.IOException;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class GetSheetsByIndexTest {

  /**
   @param args
   @throws IOException 
   @throws BiffException 
   */
  public static void main(String[] argsthrows 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());
    
    Sheet sheet = null;
    
    for (int i = ; i < workbook.getNumberOfSheets() ; i ++ ) {
      sheet = workbook.getSheet(i);
      System.out.println("Sheet Name[" + i + "] = " + sheet.getName());
    }
    
    //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



 
  


  
bl  br