I have a text file in below format
|COL1|COL2|COL3|COL4|
|VAL1|VAL2|VAL3|VAL4|
||VAL12|VAL13||
|VAL21|VAL22||VAL24|
I am then converting and saving this text data to xlsx file (using separator | to do this) using below code:
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
String line;
Row row;
Cell cell;
int rowIndex = 0;
while ((line = br.readLine()) != null) {
row = sheet.createRow(rowIndex);
String[] tokens = line.split("[|]");
for (int iToken = 0; iToken < tokens.length; iToken++) {
cell = row.createCell(iToken);
cell.setCellValue(tokens[iToken]);
}
rowIndex++;
}
} catch (Exception e) {
e.printStackTrace();
}
Now, I am reading this excel file columnwise and if I find any blank cell in that particular column, then i have to stop the execution
Code to do the above thing:
XSSFWorkbook workbook;
Cell cell = null;
int i = 0;
String msg = null;
InputStream myxls = new FileInputStream(csvFileName);
workbook = new XSSFWorkbook(myxls);
Sheet sheet = workbook.getSheetAt(0);
int rowTotal = sheet.getLastRowNum();
for (Row row : sheet) {
cell = row.getCell(col1);
if (cell == null || cell.getCellType() == Cell.CELL_TYPE_BLANK) {
System.out.println("Blank cell found and do not process the file");
break;
} else {
System.out.println(cell.toString());
i++;
}
}
But this is not working when I am converting the text in xlsx. But if I manually create one excel file with blank cells then above code works properly and stops execution for COL1 when it finds blank cell after VAL1. COL2 process properly, COL3 stops exection after VAL13 and COL4 after VAL4 itself
Can someone please help me with conversion of text into excel and then read excel for blank cell and stop execution when blank cell is found