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.
Related
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.
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 have an existing word document which has a formatted mail merge field({ MERGEFIELD Payment_Date \# "MMMM d,yyyy" }).
When I pass a string(say (01/01/2016)) from C# to do a mail merge the code field.Select() selects the entire merge field and replaces it with the string I am passing and I lose the formatting.
How can I prevent this?
foreach (Microsoft.Office.Interop.Word.Field field in document.Fields)
{
if (field.Code.Text.Contains("Payment_Date"))
{
DateTime pDate = new DateTime(2016, 12, 30);
field.Select();
application.Selection.TypeText(pDate.ToString());
}
}
This is the code that I am using to do the mail merge.
You either have to extract the format string from the field and transform it into something that lets you format your C# string correctly, or (simpler IMO) replace the field by one that retains the format string and let Word do the formatting for you.
For example, you might be able to replace it with
{ QUOTE "2016-12-30" \# "MMMM d,yyyy" }
(I would advise that you insert the date string in YYYY-MM-DD format, as I believe Word always interprets the day and month correctly in that case).
(NB, you can't just insert the text with { } - you have to insert a field. Then, if you just want the result, you can ensure the field has been Updated, then Unlink the field, just leaving the result).
One situation remains - what if the MERGEFIELD field has no date/time format switch? In that case you will need to impose a format (I do not think it will be possible to discover the document author's intent).
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
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")