how to read a workbook and sheet using jexcel

how to read a workbook and sheet using jexcel

Previous Home Next

 

This example shows how can you read  the contents of a workbook using the JExcel API.

To read a workbook i.e an excel sheet  firstly you have to import the jx,.read.biff package. This package contains the various methods for reading the excel worksheet. Please also note that reading a spreadsheet will also throw a BiffException which must be handled.
 In this example we have used the method getWorkbook(File file), to open the existing workbook. After that we have used the method getSheet(int arg), to get the sheet contained in the workbook. Also we have used the getName() method on the sheet to get its name.

Here in this example we have created a class name ReadSheetXL. In its main method we have opened an existing workbook named firstsheet.xls and then opened a sheet which is at 0th index and then we have displayed its name via using getName() method in the sheet.

It should be noted that reading of the workbook will cause a BiffException, so it is required that it should be handled explicitly using the try-catch block.

 
package r4r.co.in;
import jxl.*;
import jxl.Workbook;
import jxl.read.biff.*;
import java.io.File;
import java.io.IOException;
import jxl.write.WriteException;
import java.util.Locale;
public class ReadSheetXL {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws IOException, WriteException, BiffException{
		// TODO Auto-generated method stub
        try
        {
        	File f1=new File("F:/JAVA PROJECTS/Jexcel/src/r4r/co/in/firstsheet.xls");
        	WorkbookSettings ws=new WorkbookSettings();
        	ws.setLocale(new Locale("er","ER"));
        	Workbook workbook=Workbook.getWorkbook(f1,ws);
        	
        	Sheet readsheet=workbook.getSheet(0);
        	System.out.println(readsheet.getName());
        	
        }
        catch(IOException e)
        {
        	e.printStackTrace();
        }
       
        catch(BiffException e)
        {
        	e.printStackTrace();
        }
	}

}


Previous Home Next