Help with POI HSSF
I am trying to read a excel file by using HSSF API. It is not reading file in correct order of cells ;Below is my code:
class TestExcel{
HSSFWorkbook wb;
HSSFSheet sheet;
HSSFRow row;
HSSFCell cell;
int k=0;
java.util.Iterator iter,iter1;
FileInputStream file;
int i=0,j=0;
@SuppressWarnings("deprecation") TestExcel(){
try{
file=new FileInputStream("C:/ReportFormat/test.xls");
//file=new FileInputStream("C:/Documents and Settings/user/Desktop/excel/DCB OCTOBER 2006-CSC.xls");
}
catch(FileNotFoundException fnf){
fnf.printStackTrace();
}
try{
wb =new HSSFWorkbook(file);
}
catch(IOException ioe){
ioe.printStackTrace();
}
for(i=0;i<wb.getNumberOfSheets();i++){
sheet=wb.getSheetAt(i);
iter=sheet.rowIterator();
while(iter.hasNext()){
row=(HSSFRow)iter.next();
iter1=row.cellIterator();
while(iter1.hasNext()){
cell=(HSSFCell)iter1.next();
if(cell.getCellType()==HSSFCell.CELL_TYPE_STRING){
System.out.println("The value is :"+cell.getStringCellValue().toString());
}
if(cell.getCellType()==HSSFCell.CELL_TYPE_NUMERIC){
System.out.println("The numeric value is :"+cell.getNumericCellValue());
}
if(cell.getCellType()==HSSFCell.CELL_TYPE_FORMULA){
System.out.println("The formula is :"+cell.getCellFormula());
}
}
}
}
}
publicstaticvoid main(String args[]){
TestExcel obj=new TestExcel();
}
}
The input is :
First Sheet: First row :First cell=Title
2 nd Row :1 st cell=Column1
2 nd Row :2nd cell=Column2
2nd Row :3rd cell=Column3
2 nd Row :4th cell=Column4
Expected O/p is:
Title
Column1
Column2
Column3
Column4
But output got is:
Title
Column3
Column2
Column1
Column4
Let me know what I should modify so as to read the cells correctly.>

