Reputation: 41
I'm working with a legacy system that has components made several years ago (Excel macros) which I'm now trying to integrate into a more user friendly .net application. The files I am accessing are tab-delimited datasheets saved as __.xls so they're opened automatically by Excel when double-clicked.
Inside the sheet there are various dates in the format "dd/MM/yyyy", and when the file is opened normally (double clicked, right-click open, etc) via Excel 2003 (required version), the dates are interpreted as such. When I attempt to open the file using the Microsoft.Office.Interop.Excel (14.0.4756.1000) library however, (i.e. invoke via c#) using the following syntax:
eit_book = eit_books.Open(targetFile.FullName, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing);
the dates have their day and month switched, and all date math done in the aforementioned macros is done incorrectly. (As well as any dates where the day is greater than 12 are not even recognized as dates)
I.E. 04/01/2012 becomes 01/04/2012, this is presumably because the system has its regional settings set to "English (United States)", but I've made sure that the system's Regional Settings Short Date format is set to a custom value that matches the data in the spreadsheet ("dd/MM/yyyy"), and the problem only seems to occurs when the sheet is opened using the code above.
Any help would be very much appreciated. Thanks.
edit: The Operating System of the computer opening the files is Windows XP.
Upvotes: 2
Views: 203
Reputation: 9461
You need to use the OpenText
method to open the file to be able to specify the date formats. Additionally, Excel remembers your previous preferences (when opening a text file, or converting text to columns), so you must explicitly specify most of the parameters for reliable outcomes.
My sample file is tab delimited with 3 columns, but I'm only specifying the date format for the first column.
The first line of text in my file is:
11/01/2001 12/01/2001 13/01/2001
The OpenText method doesn't return a Workbook, so I have to retrieve it by name afterwards:
int[] dateColInfo = new int[] {1, (int)XlColumnDataType.xlDMYFormat};
object[] fieldInfo = new object[] {dateColInfo};
eit_books.OpenText(path, Type.Missing, 1, XlTextParsingType.xlDelimited, XlTextQualifier.xlTextQualifierDoubleQuote, false, true, false, false, false, false, Type.Missing, fieldInfo, Type.Missing, Type.Missing, Type.Missing, Type.Missing, false);
eit_book = eit_books.get_Item(name);
Range rng = eit_book.Worksheets.get_Item(1).range("A1");
Console.WriteLine("{0} ISO Date: {1}", rng.Value, ((DateTime)rng.Value).ToString("yyyyMMdd"));
The output when dateColInfo[1] = XlColumnDataType.xlDMYFormat
is:
11/01/2001 12:00:00 AM ISO Date: 20010111
The output when dateColInfo[1] = XlColumnDataType.xlMDYFormat
is:
1/11/2001 12:00:00 AM - ISO Date: 20011101
Upvotes: 1