Wednesday, August 29, 2012

Read XL file in Java

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
[sourcecode language="java"]
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();
}
}
[/sourcecode]

No comments:

Post a Comment