Free Support Forum - aspose.com

Excel Custom formatting for Cell is not updated

When the run the below code:


Workbook book = new Workbook(FileFormatType.Xlsx);
Worksheet sheet = book.Worksheets[0];

Style style1 = book.CreateStyle();
StyleFlag flag = new StyleFlag();
style1.Custom = “m/d/yyyy h:mm”;
flag.NumberFormat = true;
sheet.Cells.ApplyColumnStyle(0, style1, flag);

sheet.Cells[“A1”].PutValue(“November 07, 2011”);
Style style = sheet.Cells[“A1”].GetStyle();
style.Number = 15;//d-mmm-yy
sheet.Cells[“A1”].SetStyle(style);
sheet.Cells[“A2”].PutValue(DateTime.Now);
book.Save(“sample.xlsx”);

I can find A2 getting updated with appropriate formats but A1 is not. When you manually select the column values, it gets reset & then formatting is applied.

Steps:
1) Open the attachment sample.xslx
2) Manually select values in A1
3) Now randomly click on any cell
4) Now you can find the values in A1 formatted

Let me know for further reference

Hi,


Thanks for the template file and sample code.

Well, the reason (of your issue) is simple, your so called DateTime value “November 07, 2011” for A1 cell is actually inserted as string value. So, you got to first insert the value as DateTime. I think you may try to change the line of code:
i.e.,
sheet.Cells[“A1”].PutValue(“November 07, 2011”);

to:
sheet.Cells[“A1”].PutValue(“November 07, 2011”, true);

it would work fine as Aspose.Cells would automatically convert the underlying value to DateTime type.

Thank you.