File reading

package Excelfile;

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

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;



public class ExcelReader {

public static void main(String[] args) {

try {
//Read the file in input mode 
 FileInputStream file = new FileInputStream(new File("D:\\TestScript1.xls"));
 HSSFWorkbook workbook = new HSSFWorkbook(file);
// read first sheet of the document/ Excel file
 HSSFSheet sheet = workbook.getSheetAt(0);

  //code to read first columns cells
 String heading = sheet.getRow(0).getCell(0).getStringCellValue();
 String searchText1 = sheet.getRow(1).getCell(0).getStringCellValue();
 String searchText2 = sheet.getRow(2).getCell(0).getStringCellValue();
 System.out.println("Heading is:" + heading);
 System.out.println("Search Text 1 is:" + searchText1);
 System.out.println("Search Text 2 is:" + searchText2);
 //Code to read second column cells.
 String userheading=sheet.getRow(0).getCell(1).getStringCellValue();
 System.out.println("Second column Heading is:" + userheading);
 String searchText11 = sheet.getRow(1).getCell(1).getStringCellValue();
 String searchText21 = sheet.getRow(2).getCell(1).getStringCellValue();
 System.out.println("Heading is:" + heading);
 System.out.println("Search Text 1 is:" + searchText11);
 System.out.println("Search Text 2 is:" + searchText21);

 //After read or write inside the file it is good practice to close the file
 file.close();

}
//This exception help us when file does not found
            catch (FileNotFoundException fnfe) {
 fnfe.printStackTrace();
}
//This exception help us , when something wrong in INput and output error occur
catch (IOException ioe) {
 ioe.printStackTrace();
}
}
}

No comments:

Post a Comment