Spire.XLS is a professional Excel API that enables developers to create, manage, manipulate, convert and print Excel worksheets. Get free and professional technical support for Spire.XLS for .NET, Java, Android, C++, Python.

Mon Oct 09, 2023 4:41 pm

Hi-

How do you add a Date/Time to a cell and format it as a date/time?

Thanks
-Thomas

trozzi1957
 
Posts: 5
Joined: Sun Oct 14, 2018 10:58 pm

Tue Oct 10, 2023 1:59 am

Hello,

Thanks for your inquiry.
For your requirement, please refer to the following code:
Code: Select all
 // Create a workbook object
            Workbook workbook = new Workbook();     

            // Get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];

           // Get the "A1" cell and set value for it
            CellRange range = sheet.Range["A1"];
            range.Value = "2023-10-10 09:45";

            // Set the format of cell A1 as date
            range.NumberFormat = "yyyy";
            //range.NumberFormat = "yyyy-mm-dd";

            // Set AutoFitColumns for "A1" cell
            range.AutoFitColumns();

            // Save the modified Excel file
            workbook.SaveToFile(@"../../output/output.xlsx", ExcelVersion.Version2013);

            // Close the workbook
            workbook.Dispose();


If you have any issues, just feel free to contact us.

Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Return to Spire.XLS

cron