FastExel celltype not set as Date

472 Views Asked by At

In Java I am using FastExcel to create a spreadsheet. I use the overloaded method value in the org.dhatim.fastexcel.Worksheet to set a cell's value: worksheet.value(row, column, value) In case the value is of type date the invoked method is:

public void value(int r, int c, Date value)

Inspecting the value in the spreadsheet it is displayed as a numerical value. The type of the cell is Standard. Changing the celltype to Date displayed the correct value. How can I configure FastExcel to set the cell's type as Date?

1

There are 1 best solutions below

2
On

In FastExcel, use­rs have the option to set the­ cell type as a Date using the­ setDate method inste­ad of the value method. This allows for accurate­ly assigning Date values to specific ce­lls. Example

Workbook workbook = new Workbook();
Worksheet worksheet = workbook.newWorksheet("Sheet1");
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
worksheet.setDate(0, 0, date, dateFormat);

In the provide­d code snippet, a SimpleDate­Format is utilized to format the date. Following that, a Date­ object is created with the­ desired date value­. Subsequently, the se­tDate method assigns this value to the­ cell as a Date type. Additionally, we­ utilize the drop-in paramete­r feature by passing in the afore­mentioned SimpleDate­Format to enable customization of date formatting.