RSS

Read XL file in Java

29 Aug

This is basic sample class for read the xl sheet in java
Download source with “jxl-2.6.jar” and “users.xls” here

To run this you have to put the “jxl-2.6.jar” in your class path

package mysamples;

import java.io.File;
import java.io.IOException;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
/**
*
* @author dinuka
*/
public class ReadXL {
private String inputFile;

public void setInputFile(String inputFile)
{
         this.inputFile = inputFile;
}

public void read() throws IOException
{
       File inputWorkbook = new File(inputFile);
       Workbook w;
       try
{
w = Workbook.getWorkbook(inputWorkbook);
// Get the first sheet
Sheet sheet = w.getSheet(0);

for (int j = 0; j < sheet.getColumns(); j++)
{
    for (int i = 0; i < sheet.getRows(); i++)
    {
         Cell cell = sheet.getCell(j, i);
         System.out.println(cell.getContents());
     }
}
}
catch (BiffException e)
{
      e.printStackTrace();
}
}

public static void main(String[] args) throws IOException
{
       ReadXL test = new ReadXL();
       test.setInputFile("users.xls");
       test.read();
}
}
Advertisements
 
Leave a comment

Posted by on August 29, 2012 in java

 

Tags:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: