Reading xlsx using XSSF(XML SpreadhSheet Format)..

9:54 PM 0 Comments

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






Unknown

This site creates a platform to become an expert by gaining knowledge of unknown real-time issues as well.

0 comments: