Tuesday, 15 July 2014

How to read Excel sheet by java code

package com.ashish;

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.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ReadExcelDemo
{
    public static void main(String[] args)
    {
        try
        {
            FileInputStream file = new FileInputStream(new File("D:/ashu.xlsx"));
            //System.out.println("0 place");
            //Create Workbook instance holding reference to .xlsx file
            //XSSFWorkbook workbook = new XSSFWorkbook(file);
            org.apache.poi.ss.usermodel.Workbook workbook = WorkbookFactory.create(file);
           // System.out.println("-1 place");
            //Get first/desired sheet from the workbook
          //  XSSFSheet sheet = workbook.getSheetAt(0);
            org.apache.poi.ss.usermodel.Sheet sheet = workbook.getSheetAt(0);
          //  System.out.println("-2 place");
            //Iterate through each rows one by one
            Iterator<Row> rowIterator = sheet.iterator();
           // System.out.println("1 place");
            while (rowIterator.hasNext())
            {//System.out.println("2 place");
                Row row = rowIterator.next();
                //For each row, iterate through all the columns
                Iterator<Cell> cellIterator = row.cellIterator();
               
                while (cellIterator.hasNext())
                {
                    Cell cell = cellIterator.next();
                //    System.out.println("3 place");
                    //Check the cell type and format accordingly
                    switch (cell.getCellType())
                    {
                        case Cell.CELL_TYPE_NUMERIC:
                            System.out.print(cell.getNumericCellValue() + "\t\t");
                            break;
                        case Cell.CELL_TYPE_STRING:
                            System.out.print(cell.getStringCellValue() + "\t\t");
                            break;
                    }
                }
                System.out.println("");
            }
            file.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
you need follwoing jar file  POI download page
  • dom4j-1.6.1.jar
  • poi-3.9-20121203.jar
  • poi-ooxml-3.9-20121203.jar
  • poi-ooxml-schemas-3.9-20121203.jar
  • xmlbeans-2.3.0.jar
You can track the error if arise ...to below link...

3 comments:

  1. Croma campus is best it training insitute in noida and best class Java Training in Noida with job placement support croma campus best company job placement support. amd more it course like (SAP, SAS, JAVA, ANDROID APPS, DOT NET, INFORMATICA, PLC SCADA, EMBEDDED SYSTEMS) and more it course visit at croma campus

    ReplyDelete
  2. It is amazing and wonderful to visit your site. Thanks for sharing information; this is useful to us....
    VBA MACRO COURSE IN DELHI
    FOR MORE INFO:

    ReplyDelete
  3. It is amazing and wonderful to visit your site. Thanks for sharing information; this is useful to us....
    R PROGRAMING TRAINING IN DELHI
    FOR MORE INFO:

    ReplyDelete