Skip to content

Commit

Permalink
access xlsx cell values explictly with col index instead of iterator …
Browse files Browse the repository at this point in the history
…that skips empty cells
  • Loading branch information
bellerbrock committed Sep 4, 2024
1 parent 3d36017 commit e9a545b
Showing 1 changed file with 25 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -460,37 +460,40 @@ public void open() {
public String[] readNext() {

DataFormatter fmt = new DataFormatter();
ArrayList<XSSFRow> rows = new ArrayList<>();
XSSFSheet sheet = wb.getSheetAt(0);

XSSFFormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();

for (Iterator<Row> it = sheet.rowIterator(); it.hasNext();) {
rows.add((XSSFRow) it.next());
}

if (currentRow >= rows.size()) {
if (currentRow >= sheet.getPhysicalNumberOfRows()) {
return null;
}

XSSFRow row = sheet.getRow(currentRow);
ArrayList<String> data = new ArrayList<>();
for (Iterator<Cell> it = rows.get(currentRow).cellIterator(); it.hasNext();) {
XSSFCell cell = (XSSFCell) it.next();
if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {//formula
int type = evaluator.evaluateFormulaCell(cell);
switch (type) {
case CELL_TYPE_BOOLEAN:
data.add(String.valueOf(cell.getBooleanCellValue()));
break;
case CELL_TYPE_NUMERIC:
data.add(String.valueOf(cell.getNumericCellValue()));
break;
default:
data.add(cell.getStringCellValue());
break;

int maxColumns = sheet.getRow(0).getLastCellNum(); // Get total number of columns from header

for (int colIdx = 0; colIdx < maxColumns; colIdx++) {
XSSFCell cell = (row == null) ? null : row.getCell(colIdx);

if (cell != null) {
if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {//formula
int type = evaluator.evaluateFormulaCell(cell);
switch (type) {
case CELL_TYPE_BOOLEAN:
data.add(String.valueOf(cell.getBooleanCellValue()));
break;
case CELL_TYPE_NUMERIC:
data.add(String.valueOf(cell.getNumericCellValue()));
break;
default:
data.add(cell.getStringCellValue());
break;
}
} else {
data.add(fmt.formatCellValue(cell));
}
} else {
data.add(fmt.formatCellValue(cell));
data.add(""); // Add empty string for missing/empty cells
}
}

Expand Down

0 comments on commit e9a545b

Please sign in to comment.