0

Need help on,

I have an Excel file

TN  FN      MN  LN      Type
MR  Test1   t1  Test1   abc
MR  Test2   t2          abc
MR          t3  Test3   abc
MR  Test4   t4  Test4   abc

My requirements are:

  1. read rows from java program
  2. check for invalid rows(if either 2nd or 4th column is null/empty then it will be invalid rows and store this row in list1 )
  3. check for valid rows(if neither 2nd or 4th column is not null/empty then it will be valid rows and store this rows in list2)

1)

FileInputStream file = new FileInputStream("c:\\test.xls");

//Get the workbook instance for XLS file 
XSSFWorkbook workbook = new XSSFWorkbook(file);

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

//Iterate through each rows from first sheet
Iterator<Row> rowIterator = sheet.iterator();
while(rowIterator.hasNext()) {
    Row row = rowIterator.next();

    //For each row, iterate through each columns
    Iterator<Cell> cellIterator = row.cellIterator();
    while(cellIterator.hasNext()) {

        Cell cell = cellIterator.next();

        switch(cell.getCellType()) {
            case Cell.CELL_TYPE_BOOLEAN:
                System.out.print(cell.getBooleanCellValue() + "\t\t");
                break;
            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();
FileOutputStream out = 
    new FileOutputStream(new File("C:\\Users\\IBM_ADMIN\\Desktop\\test.xls"));
workbook.write(out);
out.close();

} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();

}

2) I am stuck here, how can I get the valid and invalid rows?

Appreciate your help

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Try to frame this as a (technical) and specific question. What is your exact problem here? Where are you stuck? – Martin Apr 09 '14 at 14:14

1 Answers1

0

POI takes a little getting used to. You have to explicitly check to see if the cell is blank or null.

There's a good example here on SO.

Community
  • 1
  • 1
Mike
  • 3,186
  • 3
  • 26
  • 32