Hello,
Hi,
Thanks for your posting and using Aspose.Cells.
You need to set the StyleFlag.NumberFormat property true. Please change your following code
into
Changes are highlighted in red color.
I have tested this issue with the following sample code with the latest version: Aspose.Cells for .NET 8.5.1 and it generated the correct results. I have attached the output excel file generated by the code for your reference.
C#
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
Style style = workbook.CreateStyle();
style.Custom = “yyyy/mm/dd”;
StyleFlag flag = new StyleFlag();
flag.NumberFormat = true; //set NumberFormat true
worksheet.Cells.ApplyColumnStyle(0, style, flag);
worksheet.Cells[“A1”].PutValue(DateTime.Now);
worksheet.Cells[“A2”].PutValue(DateTime.Now);
worksheet.Cells[“A3”].PutValue(DateTime.Now);
worksheet.Cells[“A4”].PutValue(DateTime.Now);
worksheet.Cells[“A5”].PutValue(DateTime.Now);
worksheet.AutoFitColumns();
workbook.Save(“output.xlsx”);
Hi Shakeel,
Thank you for your help! It works after I made the change you suggested. Now I just want to understand the StyleFlag a little for future implementation.
I originally set the StyleFlag.NumberFormat to false because I thought date datatype isn’t a “number”. I usually set the StyleFlag to true only if it’s an integer or decimal.
To help me better work with aspose object in the future, can you shed some lights on this?
Thanks!
Hi,
Thanks for your feedback and using Aspose.Cells.
It is good to know that your original issue is resolved. Actually, date formats or other formats like currency, numbers etc are set using Style.Number and Style.Custom properties. Since it deals with Style.Number property, therefore it is named as StyleFlag.NumberFormat.
If you have any other question, please feel free to ask, we will look into it and help you asap.