Monday 13 February 2017

Read Cell value From Excel.xlsx (Generic)

Reading Excel cell value::

Below code is taken based Database Reading.


public String  readData(String sheetname,String row,String column
{
try{
FileInputStream fis=new FileInputStream("./TestData.xlsx");
Workbook wb=WorkbookFactory.create(fis);
        Sheet sh=wb.getSheet(sheetname);
int noofrows=sh.getLastRowNum()+1;
int noofcolumns=sh.getRow(1).getLastCellNum();
for(int i=0;i<noofrows;i++)
          {
String r=sh.getRow(i).getCell(0).toString();
if(r.equals(row))
{
for(int j=0;j<noofcolumns;j++)
         {
String c=sh.getRow(0).getCell(j).toString();
if(c.equals(column)){
try{
double value=sh.getRow(i).getCell(j).getNumericCellValue();
        String data=BigDecimal.valueOf(value).toPlainString();
return data;
}catch(Exception e){
        String data1=sh.getRow(i).getCell(j).getStringCellValue();
return data1;
}}}}}
return column;
}catch(Exception e){}
return column;

}

No comments:

Post a Comment