publicStringreadexcel(Stringsheet,intintRow,intintCol){try{//Create a workbook object from the file at specified location.//Change the path of the file as per the location on your computer.Workbookwrk1=Workbook.getWorkbook(newFile(dataPath));//Obtain the reference to the first sheet in the workbookSheetsheet1=wrk1.getSheet(sheet);//Obtain reference to the Cell using getCell(int col, int row) method of sheet// Add " - 1" to both intRow , intCol depending how whether we consider excel start with row & column number as 0 or 1CellcolArow1=sheet1.getCell(intRow,intCol);//Read the contents of the Cell using getContents() method, which will return//it as a StringStringstrReturn=colArow1.getContents();returnstrReturn;/* //Display the cell contents System.out.println("Contents of cell Col A Row 1: \""+str_colArow1 + "\""); System.out.println("Contents of cell Col B Row 1: \""+str_colBrow1 + "\""); System.out.println("Contents of cell Col A Row 2: \""+str_colArow2 + "\"");*/}catch(BiffExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}returnsheet;}
Below is an example of how to read from a cell based on row number and Column NAME
publicStringGetDataBasedOnRowNumAndColName(Stringsheet,Stringfield_name,intRow){try{Workbookwrk1=Workbook.getWorkbook(newFile(dataPath));//Obtain the reference to the first sheet in the workbookSheetsheet1=wrk1.getSheet(sheet);intx=0;inty=0;intCol=0;booleanFOUND=false;// Find corresponding Column number based on NameCellcolArow1=sheet1.getCell(x,y);do{colArow1=sheet1.getCell(x,y);if(colArow1.getContents().equalsIgnoreCase(field_name)){Col=colArow1.getColumn();// System.out.println(Col);FOUND=true;break;}x=x+1;}while(colArow1.getContents()!="");if(FOUND){colArow1=sheet1.getCell(Col,Row-1);// System.out.println(colArow1.getContents());returncolArow1.getContents();}else{return" ";}}catch(BiffExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}catch(IndexOutOfBoundsExceptione){return"";}returnsheet;}