tags:

views:

1866

answers:

4

What is the best way of representing a DateTime in Excel? We use Syncfusions Essential XlsIO to output values to an Excel document which works great. But I can't figure out how to display a DateTime in a column. Not when doing it myself directly in Excel either. Is it impossible? Do I have to use a separate date and a time column? I really wish I didn't, cause it kind of breaks sorting etc... unless Excel have something clever going on to fix that...

A: 

Excel can display a Date type in a similar manner to a DateTime. Right click on the affected cell, select Format Cells, then under Category select Date and under Type select the type that looks something like this:

3/14/01 1:30 PM

That should do what you requested. I tested sorting on some sample data with this format and it seemed to work fine.

Matthew Jones
That is actually true... do you know if it is possible to set this in a culture spesific way from C#? Does the Standard DateTime Format Strings from .NET work in excel?
Svish
If the data is exported from .NET to Excel as a string, and it looks something similar to my example, it should work when you format the cells. As for culture-specificity (?), there is a ton of custom formats (i.e. Category is Custom in Excel) that might provide what you are looking for.
Matthew Jones
+2  A: 

The underlying datatype of a datetime in Excel is a 64-bit floating point number where the length of a day equals 1 and 1st Jan 1900 00:00 equals zero. So 11th June 17:30 is about 39975.72978.

If a cell contains a numeric value such as this, it can be converted to a datetime simply by applying a datetime format to the cell.

So, if you can convert your datetimes to numbers using the above formula, output them to the relevant cells and then set the cell formats to the appropriate datetime format, e.g. yyyy-mm-dd hh:mm:ss, then it should be possible to acheive what you want.

AdamRalph
A: 

Excel expects dates and times to be stored as a floating point number whose value depends on the Date1904 setting of the workbook, plus a number format such as "mm/dd/yyyy" or "hh:mm:ss" or "mm/dd/yyyy hh:mm:ss" so that the number is displayed to the user as a date / time.

Using SpreadsheetGear for .NET you can do this: worksheet.Cells["A1"].Value = DateTime.Now;

This will convert the DateTime to a double which is the underlying type which Excel uses for a Date / Time, and then format the cell with a default date and / or time number format automatically depending on the value.

SpreadsheetGear also has IWorkbook.DateTimeToNumber(DateTime) and NumberToDateTime(double) methods which convert from .NET DateTime objects to a double which Excel can use.

I would expect XlsIO to have something similar.

Disclaimer: I own SpreadsheetGear LLC

Joe Erickson
A: 

You can set date time values to a cell in XlsIO using one of these options

            sheet.Range["A1"].Value2 = DateTime.Now;
            sheet.Range["A1"].NumberFormat = "dd/mm/yyyy";

            sheet.Range["A2"].DateTime = DateTime.Now;
            sheet.Range["A2"].NumberFormat = "[$-409]d-mmm-yy;@";

You can find more information here

http://help.syncfusion.com/ug_73/xlsio/NumberFormatting.html