read exceldemo

Download Read exceldemo

If you can't read please download the document

Upload: radha-krishna

Post on 06-Aug-2015

23 views

Category:

Internet


1 download

TRANSCRIPT

  1. 1. import java.io.File; import java.io.FileInputStream; import java.util.HashMap; 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 ReadExcelDemo { public static void main(String[] args) { try { FileInputStream file = new FileInputStream(new File("ValidateCVDeviceByObligationDomain.xlsx")); //Create Workbook instance holding reference to .xlsx file XSSFWorkbook workbook = new XSSFWorkbook(file); //Get first/desired sheet from the workbook XSSFSheet sheet = workbook.getSheetAt(0); //Iterate through each rows one by one Iterator rowIterator = sheet.iterator(); int count =0; HashMap hn = new HashMap(); while (rowIterator.hasNext()) { Row row = rowIterator.next(); count++; int cell_count=0; //For each row, iterate through all the columns Iterator cellIterator = row.cellIterator(); while (cellIterator.hasNext()) { Cell cell = cellIterator.next(); String content = null; //Check the cell type and format accordingly cell_count++; if(count==1) { switch (cell.getCellType()) { case Cell.CELL_TYPE_NUMERIC: content=String.valueOf(cell.getNumericCellValue()); //System.out.print(cell.getNumeri cCellValue() + "t"); break; case Cell.CELL_TYPE_STRING: content=cell.getStringCellValue(); //System.out.print(cell.getString CellValue() + "t"); break; } /*System.out.println("cell_count
  2. 2. "+cell_count); * System.out.println("content "+content);*/ hn.put(String.valueOf(cell_count), content); } else { //ystem.out.println("in else cell_count "+cell_count); switch (cell.getCellType()) { case Cell.CELL_TYPE_NUMERIC: //content=String.valueOf(cell.get NumericCellValue()); System.out.print(hn.get(String.valueOf(cell_count)) +cell.getNumericCellValue() + "t"); break; case Cell.CELL_TYPE_STRING: content=cell.getStringCellValue(); System.out.print(hn.get(String.valueOf(cell_count)) +cell.getStringCellValue() + "t"); break; } } } System.out.println(""); } file.close(); } catch (Exception e) { e.printStackTrace(); } } }