Reading xls using HSSF(Horrible SpreadhSheet Format)!!!

Program to Read the xlsx using the HSSF as Follows:

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

public class ReadExcelHSSF {
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("inputData.xls");

HSSFWorkbook workbook = new HSSFWorkbook(fis);

HSSFSheet worksheet = workbook.getSheetAt(0);

System.out.println(worksheet.getRow(0).getCell(0).getStringCellValue());

Iterator<Row> ite = worksheet.rowIterator();

while(ite.hasNext())
{
Row row =  ite.next();

Iterator<Cell> cellIterator = row.cellIterator();

while (cellIterator.hasNext())
{
Cell cell = cellIterator.next();
//Check the cell type and format accordingly
switch (cell.getCellType())
{
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue());
break;
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue()+"--");
break;
}
}
System.out.println();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

}

.xls file



Reading xlsx using XSSF(XML SpreadhSheet Format)..

Program to Read the xlsx using the XSSF as Follows:

import java.io.File;
import java.io.FileInputStream;
import java.util.Iterator;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ReadExcelXSSF
{

public static void main(String []args){

try {
File src=new File("Book1.xlsx");

FileInputStream fis=new FileInputStream(src);

XSSFWorkbook wb=new XSSFWorkbook(fis);

XSSFSheet sheet= wb.getSheetAt(0);

////// // getRow specify which row we want to read and getCell which column

System.out.println(sheet.getRow(0).getCell(0).getNumericCellValue());

System.out.println(sheet.getRow(0).getCell(1).getStringCellValue());

Iterator<Row> ite = sheet.rowIterator();

while(ite.hasNext())
{
Row row =   ite.next();

Iterator<Cell> cellIterator = row.cellIterator();

while (cellIterator.hasNext())
               {
                   Cell cell = cellIterator.next();
                   //Check the cell type and format accordingly
                   switch (cell.getCellType())
                   {
                 
                 
                       case Cell.CELL_TYPE_NUMERIC:
                           System.out.print(cell.getNumericCellValue()+"--");
                           break;
                       case Cell.CELL_TYPE_STRING:
                           System.out.print(cell.getStringCellValue()+"--");
                           break;
                   }
               }
System.out.println();
}
fis.close();

} catch (Exception e) {
System.out.println(e.getMessage());
}

}


}
.xlsx File