Wednesday, April 24, 2019

Retrieve data from Excel file by using Apache POI

 package fileio;  
 import java.io.File;  
 import java.io.FileInputStream;  
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
 import org.apache.poi.ss.usermodel.Sheet;  
 import org.apache.poi.ss.usermodel.Workbook;  
 import org.apache.poi.ss.usermodel.WorkbookFactory;  
 public class POI123 {  
      //This method id used to get data from the xls OR xlsx file by using browser factory class  
      public static String getdata1() throws Exception {  
           Workbook workbook = WorkbookFactory.create(new File("C:\\Users\\Mitturaj.h\\Desktop\\data.xls"));  
           Sheet sheet = workbook.getSheetAt(0);  
           int rows = sheet.getLastRowNum();  
           System.out.println(rows);  
           String value = sheet.getRow(0).getCell(0).getStringCellValue();  
           System.out.println(value);  
           return value;  
      }  
      // this is method is used to get data from the .xls file by using HSSF class  
      public static String getdata2() throws Exception {  
           HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream("C:\\Users\\Mitturaj.h\\Desktop\\data.xls"));  
           Sheet sheet = workbook.getSheetAt(0);  
           int rows = sheet.getLastRowNum();  
           String data = sheet.getRow(0).getCell(0).getStringCellValue();  
           System.out.println(rows);  
           System.out.println(data);  
           return data;  
      }  
      public static void getdata3() throws Exception{  
           HSSFWorkbook workbook=new HSSFWorkbook(new FileInputStream("C:\\Users\\Mitturaj.h\\Desktop\\data.xlsx"));  
           Sheet sheet=workbook.getSheetAt(0);  
           String value=sheet.getRow(0).getCell(0).getStringCellValue();  
           System.out.println(value);  
      }  
      public static void main(String[] args) throws Exception {  
           getdata2();  
           getdata1();  
           getdata3();  
      }  
 }  

No comments:

Post a Comment