I have the following method in java
/*-------------IMPORT Rutas----------*/
public String processImportRutas(MultipartFile fileDocument, long loggedUserId) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("error", "");
ArrayList<RutasImportDto> arrayList = new ArrayList<>();
try {
XSSFWorkbook workbook = new XSSFWorkbook(fileDocument.getInputStream());
XSSFSheet sheet = workbook.getSheetAt(0);
DataFormatter formater = new DataFormatter();
int rowCount = 1;
RutasImportDto dto;
for (Row row : sheet) {
dto = new RutasImportDto();
if (rowCount < 4) {
rowCount++;
continue;
}
if (rowCount == 4) {
Cell cell = row.getCell(0);
if (cell == null) {
continue;
}
if (formater.formatCellValue(cell).equalsIgnoreCase("Nº DE PLANTA")) {
// si la el encabezado interpreta que está en la fila 4
// aumentamos el rowCount para ajustar los números de filas
rowCount++;
continue;// for Row...
}
}
for (int i = 0; i < 4; i++) {
Cell cell = row.getCell(i, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
boolean isBlankCell = CellType.BLANK == cell.getCellTypeEnum();
boolean isEmptyStringCell = CellType.STRING == cell.getCellTypeEnum() && cell.getStringCellValue().trim().isEmpty();
//condition is ignored
if (cell == null || isBlankCell || isEmptyStringCell) {
continue;
}
//log.info("celda valor: {}",cell);
// if (cell == null || cell.getCellTypeEnum() == CellType.BLANK) {
// continue;
// }
switch (i) {
case 0:
dto.setPlantNumber(formater.formatCellValue(cell));
break;
case 1:
dto.setUserId(formater.formatCellValue(cell));
break;
case 2:
dto.setStartDay(formater.formatCellValue(cell));
break;
case 3:
dto.setHour(formater.formatCellValue(cell));
break;
default:
break;
}
}
dto.setRow(rowCount);
arrayList.add(dto);
rowCount++;
}
jsonObject = persistImportRutas(arrayList, loggedUserId);
// log.info("***** arrayList "+ arrayList);
} catch (Throwable th) {
jsonObject.put("error", th.getMessage()).toString();
log.error("processImportRutas : " + th.getMessage());
log.debug("processImportRutas : " + th.getMessage(), th);
}
// log.info("***** jsonObject.toString "+ jsonObject.toString());
return jsonObject.toString();
}
My code reads 4 columns
1-
plantnumber
2-
userId
3-
startDay
4-
Hour
If all 4 columns are empty the code should ignore the row and read the next one, if they have value then it should store values in an ArrayList
for further validations/instructions.
I'm trying to make it so that when I upload my excel file, it ignores rows that are empty or null and continue with the loop but it completely ignores my condition and returns a bunch of errors messages
I followed the answers from this other question but nothing in there seems to work for me
Cell cell = row.getCell(i, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
boolean isBlankCell = CellType.BLANK == cell.getCellTypeEnum();
boolean isEmptyStringCell = CellType.STRING == cell.getCellTypeEnum() && cell.getStringCellValue().trim().isEmpty();
//condition is ignored
if (cell == null || isBlankCell || isEmptyStringCell) {
continue;
}
In this method persistImportRutas
is where I make other validations with the data I got from the cells and return error msgs if data is null/empty or not valid and insert in my DB the ones that are valid but the problem is that my code is not ignoring the rows that are empty or null and it tries to validate the cell values and returns error cuz of it. My Apache POI version is 3.17