Date Formats

Hi

I am evaluating Aspose.CeIls and having some issues setting date formats as follows:

I am trying to display the date as ‘mmm-yy’, for example Dec-14.

This works fine when using a DateTime:

ws.Cells["A1"].PutValue(DateTime.Now);

Style style = ws.Cells["A1"].GetStyle();

style.Number = 17;

ws.Cells["A1"].SetStyle(style);

However I need to use strings as follows:

ws.Cells["A2"].PutValue("10/11/2014");

Style style_2 = ws.Cells["A2"].GetStyle();

style_2.Number = 17;

ws.Cells["A2"].SetStyle(style_2);

The above method produces a date in Excel as “10/11/2014” even though the properties of the cell correctly show the format of ‘mmm-yy’. If I double click the cell then the value appears in the correct format.

Any idea how to make this work with date strings?

Hi,


Well, actually the line of code i.e., ws.Cells[“A2”].PutValue(“10/11/2014”); is inserting string into the cell instead of DateTime value, so you are not getting expected results in the output Excel file. Please make sure that you paste DateTime data type value into the cell, the easiest way to do it is you should let Aspose.Cells do convert it into DateTime automatically, you may simply put “true” for isConverted parameter in the PutValue() overloaded method to convert the string into proper data type (DateTime value in your case).

Please change the line of code,
i.e.,
ws.Cells[“A2”].PutValue(“10/11/2014”);

with:
ws.Cells[“A2”].PutValue(“10/11/2014”, true);

it would work fine.

Thank you.