JExcel Reada data from Excel sheet

Monday, July 20, 2009

Java Excel API is a java API enabling developers to read, write, and modify Excel spreadsheets dynamically. Any operating system which can run a Java virtual machine can both process and deliver Excel spreadsheets. One nice thing about JExcelApi is that it has no dependencies on any third party libraries.

The following example will explains reading data from excel sheet dynamically.

For this application you need one jar file
That is : jxl.jar


JExcelWriteExample.java


/**
* File Name : JExcelReadExample.java
* Created By : Nagaraju V
* Created Date : Jun 19, 2009
* Purpose : Reading data from excel cells.
*/
package com.raj.jexcel;

import java.io.File;

import jxl.Sheet;
import jxl.Workbook;

/**
* @author nagarajuv
*
*/
public class JExcelReadExample {

/**
* @param args
*/
public static void main(String[] args) {

//Reading Data from ExcelSheet
String file = "employee_xls.xls";
File fp = new File(file);
try {
Workbook wb = Workbook.getWorkbook(fp);
Sheet sheet = wb.getSheet(0);
int columns = sheet.getColumns();
int rows = sheet.getRows();

System.out.println("No of Columns-->"+columns);
System.out.println("No of rows-->"+rows);

String data;
for(int row = 0;row < rows;row++) {
for(int col = 0;col < columns;col++) {
data = sheet.getCell(col, row).getContents();
if(col>0)
System.out.print(", ");
System.out.print(data);
}
System.out.println("");
}
}catch(Exception ioe) {
System.out.println("Error: " + ioe);
}
}
}

Enjoy with this is nice jxlread example
Image Hosted by ImageShack.us
Image Hosted by ImageShack.us

0 comments: