Here is the Class designed for reading Excel file...
You can download the JAR files needed to work with Apache POI here.
package Excel;
import java.io.File;
import java.io.FileInputStream;
import org.apache.poi.ss.usermodel.*;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Hashtable;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.dom4j.DocumentException;
public class POI {
int rowIndex = 0, columnIndex = 0;
Workbook wb;
Sheet ws;
Row wr;
String fileName, sheetName;
Hashtable <String, String> [] data = null;
public void open() throws IOException {
if (fileName.indexOf("xlsx") < 0) {
wb = new HSSFWorkbook(new FileInputStream(new File(fileName)));
ws = wb.getSheet(sheetName);
} else {
wb = new XSSFWorkbook(fileName);
ws = (XSSFSheet) wb.getSheet(sheetName);
}
}
@SuppressWarnings("unchecked")
public Object[][] getData() throws IOException {
data = new Hashtable[ws.getPhysicalNumberOfRows()];
wr = ws.getRow(0);
for(rowIndex = 1; rowIndex < ws.getPhysicalNumberOfRows(); rowIndex++) {
data[rowIndex - 1] = new Hashtable <String, String>();
for (columnIndex = 0; columnIndex < ws.getRow(rowIndex).getPhysicalNumberOfCells(); columnIndex++) {
data[rowIndex - 1].put(wr.getCell(columnIndex).toString(), ws.getRow(rowIndex).getCell(columnIndex).toString());
}
}
Object[][] obj = new Object[data.length - 1][1];
for(int i = 0; i < data.length - 1; i++) {
obj[i][0] = data[i];
}
return obj;
}
public void close() throws IOException {
wb = null
ws = null
wr = null
data = null
}
}
You can download the JAR files needed to work with Apache POI here.
package Excel;
import java.io.File;
import java.io.FileInputStream;
import org.apache.poi.ss.usermodel.*;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Hashtable;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.dom4j.DocumentException;
public class POI {
int rowIndex = 0, columnIndex = 0;
Workbook wb;
Sheet ws;
Row wr;
String fileName, sheetName;
Hashtable <String, String> [] data = null;
public void open() throws IOException {
if (fileName.indexOf("xlsx") < 0) {
wb = new HSSFWorkbook(new FileInputStream(new File(fileName)));
ws = wb.getSheet(sheetName);
} else {
wb = new XSSFWorkbook(fileName);
ws = (XSSFSheet) wb.getSheet(sheetName);
}
}
@SuppressWarnings("unchecked")
public Object[][] getData() throws IOException {
data = new Hashtable[ws.getPhysicalNumberOfRows()];
wr = ws.getRow(0);
for(rowIndex = 1; rowIndex < ws.getPhysicalNumberOfRows(); rowIndex++) {
data[rowIndex - 1] = new Hashtable <String, String>();
for (columnIndex = 0; columnIndex < ws.getRow(rowIndex).getPhysicalNumberOfCells(); columnIndex++) {
data[rowIndex - 1].put(wr.getCell(columnIndex).toString(), ws.getRow(rowIndex).getCell(columnIndex).toString());
}
}
Object[][] obj = new Object[data.length - 1][1];
for(int i = 0; i < data.length - 1; i++) {
obj[i][0] = data[i];
}
return obj;
}
public void close() throws IOException {
wb = null
ws = null
wr = null
data = null
}
}
thanx for jar ......
ReplyDeletewill u plz elaborate about Excel package.
regards
manvendra
XSSF class for Excel 2007 onwards and HSSF class for excel 2003 and below.
ReplyDeleteJust reading row by row and getting all column values.. thats all...
Again converting my data as 2D Object to pass in my DataProvider annotation of TestNG framework...