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
Related
I'm using the Microsoft Office C# library (Microsoft.Office.Interop.Excel) to write some values to an existing excel file.
In Excel I've already formatted some cells in the file to be interpreted as a date, as shown in the screenshot:
As you can see, the cell is set to a dd/MM/yyyy format.
However, when I then wrote the value using the interop library, like this:
cell.Value2 = "20/06/2019"
I noticed that it wasn't working: when I opened the final file in Excel the cell was being interpreted as plain text, instead of a date.
So I did some debugging, and it turns out that the format I set for the cell is completely ignored, and that the interop library is using the American format, as you can see by inspecting the NumberFormat property of the cell with the visual studio debugger:
What am I doing wrong here? Why is the American date format being forced?
Some additional info:
My OS locale (italian) uses the dd/MM/yyyy format by default
I did not change the locale of my c# app in any way
My copy of Excel is set to italian, which should use the dd/MM/yyyy format by default as well. I checked under Options -> Language, and I can confirm this.
EDIT: and interesting thing I found: if I double-click on a cell and then click away from it, it "magically" fixes the formatting.... why? And can I simulate a double click on the cell using Excel Interop?
Dates in Excel are stored as double representing OLE Automation date.
MSDN
In your case, you are setting a string value.
A proper way to set a date in Excel is following:
cell.NumberFormat = "dd/MM/yyyy";
cell.Value = new DateTime(2019, 6, 20).ToOADate();
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
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;
I am creating a .CSV file in which one of the fields is a Date. One of the rows of my .CSV looks like this
Ryan,Adams,Ryan Adams,"""reddish"" brown,yellow,""bluish"" green",May 12, 1995
Excel does not recognize that the last field is a date and splits into two fields May 12 and 1995
I tried adding quotes "May 12, 1995" but that means it is just copied as a string and that is not what I want. I want the Excel cell to recognize that it is a date.
Other than changing your CSV file to have a recognizable date format, e.g. 1995-05-12, you could import the data as it stands and then convert the two parts to a valid date using the following formula.
=DATE(F1,MONTH(E1),DAY(E1))
This assumes that the "MMM-DD" part is in cell E1 and the YYYY part is in cell F1.
Then you can copy the new column, "paste values" over the top of itself to turn it into data, and delete columns E & F.
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")