I am generating an Excel file using Apache POI in Java, and I am formatting some cells to display dates. However, when I open the Excel file and inspect the cells, I notice that the formatted date cells are categorized under Custom instead of Date in Excel
private void createCell(Workbook wb, Sheet sheet, Row row, int columnCount, Object value) {
Cell cell = row.createCell(columnCount);
if (value instanceof Date) {
// Create a CellStyle for the custom date format
CellStyle dateCellStyle = wb.createCellStyle();
CreationHelper createHelper = wb.getCreationHelper();
// Set the custom date format "dd-MM-yyyy"
dateCellStyle.setDataFormat(createHelper.createDataFormat().getFormat("dd-MM-yyyy"));
// Set the cell value and apply the style
cell.setCellValue((Date) value);
cell.setCellStyle(dateCellStyle);
} else if (value instanceof Integer) {
cell.setCellValue((Integer) value);
} else if (value instanceof Boolean) {
cell.setCellValue((Boolean) value);
} else if (value instanceof String) {
cell.setCellValue((String) value);
} else {
cell.setCellValue(value != null ? value.toString() : "");
}
}
I need these cells to be categorized under the Date format in Excel.
What I Have Tried:
-
Using different date formats, such as dd-MM-yyyy or MM/dd/yy.
-
Disabling any additional styling to test the behavior with just the date value.
-
Setting locale-specific formats like Locale.US or Locale.UK.
-
Manually inspecting the generated file’s regional settings in Excel.
My Goal:
I want the cells to:
- Be categorized under Date format instead of Custom in Excel.
Is there a way to ensure that the cells default to the Date format category in Excel? If not, what would be the best way to achieve this?
3