Thursday, March 12, 2009

Java Excel Reading

To read the excel sheet data from java, Only few open source API's available currently.
The most popular one is Apache POI API. But some ppl prefer to use jexcelapi.

Whatever, Most of us will get stop when they read's Formula cells
in Formula cell some ppl use to read data from some other excel sheet or from other remote excel sheet.

To over come all this issues i have done a prototype which will read all kind of formula's.
This code i developed by using POI 3.5 API.

public class ExcelReaderHelper {

private InputStream excelStream;
private HSSFWorkbook workbook;
private POIFSFileSystem poiFileStream;
private HSSFSheet sheet;
private FormulaEvaluator evaluator;

public ExcelReaderHelper(String excelFileName) throws FileNotFoundException,IOException{

//excelStream = ExcelReaderHelper.class.getResourceAsStream(excelFileName);
excelStream = new FileInputStream(excelFileName);
if(excelStream == null)
throw new FileNotFoundException(excelFileName+ " File not found in spectfied path");

poiFileStream = new POIFSFileSystem( excelStream );
workbook = new HSSFWorkbook(poiFileStream);
evaluator = workbook.getCreationHelper().createFormulaEvaluator();
}


public getCellValue(final int columnId, final int rowId) throws NullPointerException, Exception{

String cellVal = null;

HSSFRow row = (HSSFRow)sheet.getRow(rowId);
if(row == null)
throw new NullPointerException("Row is null");

HSSFCell cell = (HSSFCell)row.getCell(columnId);
if(cell == null)
return cellVal = "";

try{//evaluateInCell(Cell cell) will check to see if the supplied cell is a formula cell.
//If it isn't, then no changes will be made to it. If it is, then the formula is evaluated,
//and the new value saved into the cell, in place of the old formula.
evaluator.evaluateInCell(cell);
}catch(Exception e){
//log.error("Cell evaluation error");
}

switch (cell.getCellType()) {

case HSSFCell.CELL_TYPE_NUMERIC:
if(DateUtil.isCellDateFormatted(cell)) {
cellVal = cell.getDateCellValue().toString();
} else {
cellVal = new Double(cell.getNumericCellValue()).toString();
}
break;

case HSSFCell.CELL_TYPE_STRING:
cellVal = cell.getStringCellValue();
break;

case HSSFCell.CELL_TYPE_FORMULA:
try{//Re-calculating all formulas in a Workbook
evaluator.evaluateFormulaCell(cell);
}catch(Exception e){
//log.error("Cell evaluation formula error");
}

if (cell.getCachedFormulaResultType() == HSSFCell.CELL_TYPE_STRING){
cellVal = cell.getStringCellValue();

}else if(cell.getCachedFormulaResultType() == HSSFCell.CELL_TYPE_NUMERIC){
double cellValDoub = cell.getNumericCellValue();
DecimalFormat df = new DecimalFormat("##");
cellVal = df.format(cellValDoub).toString();

}else if(cell.getCachedFormulaResultType() == HSSFCell.CELL_TYPE_FORMULA){
cellVal = "";
}else if(cell.getCachedFormulaResultType() == HSSFCell.CELL_TYPE_BOOLEAN){
cellVal = new Boolean(cell.getBooleanCellValue()).toString();
}else if(cell.getCachedFormulaResultType() == HSSFCell.CELL_TYPE_ERROR){
cellVal = "";
}else{
cellVal = "";
}

break;

case HSSFCell.CELL_TYPE_BLANK:
cellVal = "";
break;

case HSSFCell.CELL_TYPE_ERROR:
cellVal = "";
break;

default:
cellVal = cell.getStringCellValue();
break;
}

return cellVal;
}
}