/** * Get the value of the EXCEL cell, all converted to String and returned * @param cell * @return */ private String getCellStrValue(Cell cell) { String value = ""; if (cell != null) { switch (cell.getCellType()) { case Cell.CELL_TYPE_NUMERIC: if(DateUtil.isCellDateFormatted(cell)){ // Process date format, time format SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = cell.getDateCellValue(); value = sdf.format(date); }else{ DataFormatter data = new DataFormatter(); value = data.formatCellValue(cell); } break; case Cell.CELL_TYPE_STRING: value = cell.getStringCellValue().trim(); break; case Cell.CELL_TYPE_FORMULA: break; case Cell.CELL_TYPE_BOOLEAN: value += cell.getBooleanCellValue() + ""; break; default: break; } } return value; }
/** * Function: determine whether it is a blank line * */ private boolean isBlankRow(org.apache.poi.ss.usermodel.Row columnRow, int excelLastcell) { String value = ""; for (int i = 0; i < excelLastcell; i++) { Cell cell = columnRow.getCell(i); if (cell != null) { switch (cell.getCellType()) { case Cell.CELL_TYPE_NUMERIC: value += cell.getNumericCellValue() + ""; break; case Cell.CELL_TYPE_STRING: value += cell.getStringCellValue(); break; case Cell.CELL_TYPE_FORMULA: break; case Cell.CELL_TYPE_BOOLEAN: value += cell.getBooleanCellValue() + ""; break; default: break; } } } if (value == null || "".equals(value)) { return true; } else { return false; } }
Row dataRow = null;
org.apache.poi.ss.usermodel.Row excelheadRow = sheet.getRow(0);
int excelLastcell = excelheadRow.getLastCellNum(); //Number of columns
// perform the operation
for(int n=1;n<=lastNum;n++){
dataRow = sheet.getRow(n);
if (isBlankRow(dataRow, excelLastcell)) { //Skip empty row
continue;
}
.....
}
--Excerpted from the Import Orientation Data of the Billing System, in System Administration-Student Administration