Read from Excel file using POI library



import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class TestClass {

 public static void main(String[] args) {
  FileInputStream fileInputStream = null;
  try {
   fileInputStream = new FileInputStream(new File(
     "C:\\ExcelFolder\\ExcelFile.xls"));
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  }

  HSSFWorkbook workbook = null;
  try {
   workbook = new HSSFWorkbook(fileInputStream);
  } catch (IOException e) {

   e.printStackTrace();
  }

  // Get first sheet from the workbook
  HSSFSheet sheet = workbook.getSheetAt(0);

  int rows; // No of rows
  rows = sheet.getPhysicalNumberOfRows();
  if (rows >= 0) {
   for (int n = 1; n < rows; n++) {
    HSSFRow row1 = sheet.getRow(n);
    // Get value from first cell (Numeric value)
    HSSFCell cellA1 = row1.getCell((short) 0);
    Double phNo = cellA1.getNumericCellValue();
    long _phoneNo = phNo.longValue();
    
    // Get second cell value(String value)
    HSSFCell cellK1 = row1.getCell((short) 1);
    String name = cellK1.getStringCellValue();
    System.out.println("phone" + _phoneNo);
    System.out.println("UserName" + name);

   }
  }
 }
}



SHARE

About Unknown

    Blogger Comment
    Facebook Comment

0 comments :

Post a Comment