Skip to content

Commit

Permalink
Merge pull request #1039 from PhenoApps/xlsx_import_fix
Browse files Browse the repository at this point in the history
Update xlsx import to include all cells even when empty
  • Loading branch information
trife authored Oct 2, 2024
2 parents db563b2 + e9a545b commit b09908a
Showing 1 changed file with 25 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -486,37 +486,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 b09908a

Please sign in to comment.