Autofit column after setting the date format in Excel using Aspose.Cells for .NET in C#

Hello,

In my excel, i selected a cell and that cell i set a format like date,“Wednesday, March 14, 2012”, in Format Cells.

In my code:

selectedCell.PutValue(datetimeValue);
worksheet.AutoFitColumn(selectedCell.Column);

where datetimeValue is System.Datetime.

The value of cell.StringValue is not the format that i set in the excel but is the same date of datetimeValue . And the autoFitColumn function calculated the width seems using cell.StringValue.
As a result when i open the excel, the value is correct but the width is too narrow to shown the whole format and become ######.
My Question is how could i handle it?

@mshinmina,

Thanks for the details.

I tested your scenario/ case a bit using the following sample code,it works fine and as expected. Please see the sample code and find attached the output Excel file for your reference:
e.g
Sample code:

Workbook wb = new Workbook();
            Worksheet ws = wb.Worksheets[0];
            
            DateTime dt = DateTime.Now.Date;
            ws.Cells["A1"].PutValue(dt);

            Style style = wb.CreateStyle();
            style.Custom = "[$-F800]dddd, mmmm dd, yyyy";

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

        ws.AutoFitColumn(ws.Cells["A1"].Column);

        wb.Save("e:\\test2\\out1.xlsx");

If you still find any issue, kindly do provide your sample code (runnable) and template file to show the issue, we will check it soon.
files1.zip (6.4 KB)

Thank you for your reply. I fixed it because i used an old version

@mshinmina,

Good to know that your issue is sorted out by newer versions. Feel free to contact us any time if you need further help or have some other issue or queries, we will be happy to assist you soon.