How to read .xls file in Java
Need to download dependency JXL
http://search.maven.org/#artifactdetails|net.sourceforge.jexcelapi|jxl|2.6.12|jar
...
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>
....
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import java.io.File;
import java.io.IOException;
public class MyDataProvider {
public Object[][] ReadDataFromExcelFile(String filePath, String sheetName, int columnSize) throws IOException, BiffException {
//I want to open excel file
Workbook workbookObj = Workbook.getWorkbook(new File(filePath));
//I want to open sheet
Sheet sheetObj = workbookObj.getSheet(sheetName);
//I want to know rowCount
int rowSize = sheetObj.getRows() - 1; // Remove Header Row
//Need to return Object[][] object
Object[][] data = new Object[rowSize][columnSize];
//Read One by one Cell info add store in data
for (int i = 0; i < rowSize; i++) {
for (int j = 0; j < columnSize; j++) {
data[i][j] = sheetObj.getCell(j, i + 1).getContents();
}
}
return data;
}
}
http://search.maven.org/#artifactdetails|net.sourceforge.jexcelapi|jxl|2.6.12|jar
...
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>
....
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import java.io.File;
import java.io.IOException;
public class MyDataProvider {
public Object[][] ReadDataFromExcelFile(String filePath, String sheetName, int columnSize) throws IOException, BiffException {
//I want to open excel file
Workbook workbookObj = Workbook.getWorkbook(new File(filePath));
//I want to open sheet
Sheet sheetObj = workbookObj.getSheet(sheetName);
//I want to know rowCount
int rowSize = sheetObj.getRows() - 1; // Remove Header Row
//Need to return Object[][] object
Object[][] data = new Object[rowSize][columnSize];
//Read One by one Cell info add store in data
for (int i = 0; i < rowSize; i++) {
for (int j = 0; j < columnSize; j++) {
data[i][j] = sheetObj.getCell(j, i + 1).getContents();
}
}
return data;
}
}
Comments
Post a Comment