Skip to content

[Bug] CsvCell.setCellValue(Calendar) silently writes an empty field #973

Description

@Aias00

Search before asking

  • I searched in the issues and found nothing similar.

Fesod version

current main (50a6b5b)

JDK version

Temurin 25 (code path is version-independent, affects 8+)

Operating system

Linux (not OS-specific)

Steps To Reproduce

Writing a Calendar value to a CsvCell via the POI Cell API silently produces an empty field instead of the date.

Calendar cal = Calendar.getInstance();
cal.set(2024, Calendar.JANUARY, 15, 12, 30, 45);
cal.set(Calendar.MILLISECOND, 0);

try (Writer writer = Files.newBufferedWriter(csvFile.toPath(), StandardCharsets.UTF_8)) {
    CsvWorkbook workbook = new CsvWorkbook(writer, null, false, false, StandardCharsets.UTF_8, false);
    CsvSheet sheet = (CsvSheet) workbook.createSheet();
    CsvRow row = (CsvRow) sheet.createRow(0);

    Cell cell = row.createCell(0, CellType.NUMERIC);
    cell.setCellValue(cal);

    sheet.close();
}

// csvFile content is an empty field instead of "2024-01-15 12:30:45"

Current Behavior

The CSV cell is written as an empty field. The Calendar value is silently lost — no exception is thrown.

Expected Behavior

The cell should be written as a date (2024-01-15 12:30:45 by default), the same way setCellValue(java.util.Date) and setCellValue(java.time.LocalDateTime) behave on a CsvCell.

Anything else?

Root cause: CsvCell.setCellValueImpl(Calendar) sets dateValue and cellType = NUMERIC but, unlike its sibling setters, does not set numericCellType = NumericCellTypeEnum.DATE:

https://github.com/apache/fesod/blob/main/fesod-sheet/src/main/java/org/apache/fesod/sheet/metadata/csv/CsvCell.java#L186-L193

@Override
protected void setCellValueImpl(Calendar value) {
    if (value == null) {
        return;
    }
    this.dateValue = LocalDateTime.ofInstant(value.toInstant(), ZoneId.systemDefault());
    this.cellType = CellType.NUMERIC;
    // missing: this.numericCellType = NumericCellTypeEnum.DATE;
}

The sibling Date setter (a few lines above) and LocalDateTime setter both set it:

this.cellType = CellType.NUMERIC;
this.numericCellType = NumericCellTypeEnum.DATE;

In CsvSheet.buildCellValue the NUMERIC branch dispatches on csvCell.getNumericCellType() == NumericCellTypeEnum.DATE; when it is not set, the cell takes the number branch, finds numberValue is null, and returns null, producing an empty field.

Adding the missing line fixes it:

this.numericCellType = NumericCellTypeEnum.DATE;

Are you willing to submit a PR?

  • I'm willing to submit a PR!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions