简体   繁体   English

用C#调用的Excel中打开的制表符分隔文件无法将文本正确解释为日期

[英]Tab-delimited file opened in Excel invoked by c# not interpreting text to dates correctly

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. 我正在使用具有几年前制造的组件(Excel宏)的旧系统,现在我正尝试将其集成到更用户友好的.net应用程序中。 The files I am accessing are tab-delimited datasheets saved as _ _.xls so they're opened automatically by Excel when double-clicked. 我正在访问的文件是制表符分隔的数据表,另存为_ _.xls,因此双击时它们会由Excel自动打开。

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. 工作表内有各种日期,格式为“ dd / MM / yyyy”,当通过Excel 2003(必需版本)正常打开文件(双击,右键单击打开等)时,日期将被解释为。 When I attempt to open the file using the Microsoft.Office.Interop.Excel (14.0.4756.1000) library however, (ie invoke via c#) using the following syntax: 但是,当我尝试使用Microsoft.Office.Interop.Excel(14.0.4756.1000)库打开文件时,(即通过c#调用)使用以下语法:

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) (以及日期大于12的任何日期都不会被识别为日期)

IE 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. IE 04/01/2012变为01/04/2012,这大概是因为系统将其区域设置设置为“英语(美国)”,但是我已经确保将系统的“区域设置短日期”格式设置为一个与电子表格中的数据相匹配的自定义值(“ dd / MM / yyyy”),并且仅在使用上述代码打开工作表时才出现问题。

Any help would be very much appreciated. 任何帮助将不胜感激。 Thanks. 谢谢。

edit: The Operating System of the computer opening the files is Windows XP. 编辑:打开文件的计算机的操作系统是Windows XP。

You need to use the OpenText method to open the file to be able to specify the date formats. 您需要使用OpenText方法打开文件才能指定日期格式。 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. 此外,Excel会记住您以前的首选项(打开文本文件或将文本转换为列时),因此您必须明确指定大多数参数才能获得可靠的结果。

My sample file is tab delimited with 3 columns, but I'm only specifying the date format for the first column. 我的示例文件是由3列分隔的制表符,但是我仅指定第一列的日期格式。

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: OpenText方法不会返回工作簿,因此之后我必须按名称检索它:

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: dateColInfo[1] = XlColumnDataType.xlDMYFormat时,输出为:

11/01/2001 12:00:00 AM ISO Date: 20010111

The output when dateColInfo[1] = XlColumnDataType.xlMDYFormat is: dateColInfo[1] = XlColumnDataType.xlMDYFormat时,输出为:

1/11/2001 12:00:00 AM - ISO Date: 20011101

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM