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:
- read rows from java program
- 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 )
- 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