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






Downloading the POI to read xlsx...

What is Apache POI?
Apache POI is a popular API that allows programmers to create, modify, and display MS Office files using Java programs. It is an open source library developed and distributed by Apache Software Foundation to design or modify Microsoft Office files using Java program. It contains classes and methods to decode the user input data or a file into MS Office documents.

HSSF (Horrible Spreadsheet Format) : It is used to read and write xls format of MS-Excel files.
XSSF (XML Spreadsheet Format) : It is used for xlsx file format of MS-Excel.

Step 1- Download the apache poi jar file as below

Go to Official website of Apache POI  and Click on the download section

http://poi.apache.org/download.html











Now Click on the below mention link


All jar files will come in zip files, Extract it and you will get final jar folder looks like this. Add the highlighted jars to the build path.