Formatting DateTime in Excel XML worksheet - c#

When generating an Excel XML workbook with ASP.NET i'm appending 3 cells in a row like:
sb.Append(String.Format(
"<Cell><Data ss:Type=\"DateTime\">{1}</Data></Cell>{0}",
Environment.NewLine,
item.DateAvailable.ToString("yyyy-MM-ddTHH:mm:ss.fffffffK")
)); // newlines added for readability;
item.DateAvailable is a C# DateTime that has to end in Excel as An cell-DateTime.
so
value from debug in VS2010 in asssembly:
2011-08-15T01:16:06.8470000
value in excel:
40761,2299142361
after manual conversion of the cell formatting:
6-08-11 5:31
However, when converting it outputs in excel as a number, and the cell formatting is set to custom. When I manually change the cell formatting back to DateTime the correct??? date is shown in the correct format. But of course, the business would not accept such a raw data they have to convert themselves ;)
Perhaps I am using the wrong StringFormat?
What is the correct format to write a DateTime in this case and output it in Excel in the correct DateTime Format?

Yes, you are using a "wrong" string format, i.e. a format that Excel does not automatically recognize (or parse appropriately) as a date. Drop the T. Also, the date-time format in Excel rounds at least to milliseconds, if not to integer seconds, so you might as well drop some of those decimal places.
This should work:
item.DateAvailable.ToString("yyyy-MM-dd HH:mm:ss.ffff")

Related

Local Time Format in Excel

I'm using c# to import same data in Excel and on of the cell in each row has date time
var dataToImportToExcel= new object[1048576, 10] //Array of data
foreach(...)
{
...
dataToImportToExcel[row, columnIndex++] = UnixTime.LocalTimeToDateTime(time)
...
}
Here UnixTime is defined as Epoch.AddMilliseconds(unixTimeStamp)
After creating the above variable it's passed to current worksheet
var writeRange = currentWorkSheet.Range[i, j];
writeRange.Value2 = dataToImportToExcel;
Excel is showing date time format as 02/05/2021 06:04:37.000 pm instead of 5/2/2021 6:04:35 pm, here later on is the local date time format. Even if I change local date time format in machine it always uses first format only.
While debugging I can see in the IDE that date format is correctly showing in variable dataToImportToExcel
It looks your Excel understood that you wrote a DateTime value into the cell.
It's format is not defined by the value but by the formatting information set on Cell or Range. Looking into How to make correct date format when writing data to Excel topic you should set it like this:
Range rg = (Excel.Range)worksheetobject.Cells[1,1];
// Instead of "MM/DD/YYYY" you may use a format specifier appropriate for your needs.
rg.EntireColumn.NumberFormat = "MM/DD/YYYY";
This will format the entire column in the same way. Following the topic above you will find other examples like setting only one cell's format.

C# How to copy the content of excel as you see (for example: date time as you see is 12/31/2018, excel content is a number like 43465)

I am having a problem with parsing excel content to datetime type.
In my country, there are just two cultureinfo to use: "vi-VN" (dd/MM/yyyy) and "us-US" (MM/dd/yyyy).
For some cases I test, both DateTime.Parse with cultureinfo above throwing error "String is not recognized as a valid datetime".
I want to use DateTime.ParseExact instead of above approach. In order to do this, I must get a string like "12/31/2018". But when I use Epplus, it reads the content of the cell as a number 43465. This way, I cannot parse use DateTime.ParseExact.
Any here know how to read the excel as you see on the screen ?
Thank you.
DateTime.FromOADate(Double) Method
var dateTime = DateTime.FromOADate(43465);

Convert number of days to date in excel using ReoGridControl for WPF

I'm having an excel sheet (Excel 2007). I have used ReoGridControl in it. When I'm opening the sheet, the default date format is getting changed and instead of date, the number of days is being displayed.
I tried using
worksheet.SetRangeDataFormat(RangePosition.EntireRange,CellDataFormatFlag.DateTime);
but it is converting other fields which are not dates into date format.
Is there any other way to convert the specific column to the date format or the way to get the data format of the particular column?
This is the original file.
After loading the file, I'm getting
After applying SetRangeDataFormat I'm getting

Formatting '[$-F400]h:mm:ss\\ AM/PM' Excel 2007 date time in c#

I'm trying to read Excel 2007+ files in c# but all the libraries I have tried so far (OpenXML, ClosedXML and NPOI) seem unable to parse a cell with the time format correctly.
In Excel the data is formatted as Number > Time and uses '*hh:mm:ss' as it's type.
When I look at the raw value in the libraries it is appearing as 0.0416666666666667. I've followed advice from other posts which suggest using DateTime.FromOADate which (correctly) results in '30/12/1899 01:00:00'.
What I'm really stuck on is how to display the datetime object {30/12/1899 01:00:00} as it is displayed in Excel: '01:00:00'. I can see the Style.DateFormat is set to '[$-F400]h:mm:ss\ AM/PM' but how can I use this to format the DateTime object in C# as a string? The ToString() method doesn't recognise it as a valid format.
A DateTime by definition always has a date and a time. To only have the time you would have to use a TimeSpan. Here is a quick way you can get that.
DateTime originalDateTime = DateTime.Now;
TimeSpan hoursMinutesSeconds = originalDateTime.TimeOfDay;

Issue while reading an Excel through adapter which was written through npoi

Steps I am doing:
First I exported some rows into excel(97-2003 format) using NPOI.
Columns actually are string fields, date fields and time fields but when exported into excel they were converted into string(General in terms of Excel format).
Now if suppose I open the above formed excel and update few cells(specially date/time columns), excel converts that cells to date/time format.
Now when I read this excel file using OLEDB Adapter, it reads the cells that were converted to date/time fields but other cells in same column in string format throws error.
The NPOI examples have an example of setting date values in cells (SetDateCellInXls). This has two methods of writing dates, the first setting the cell style to a date format (the example is in Chinese, but should be easy to use your locale). The second is to use a formula to enter the date.
Hopefully one of these will solve your problem

Categories