如何解决“不是合法的 OleAut 日期”。在 C# 中读取 Excel 文件时?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12605799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to fix "Not a legal OleAut date." when reading an Excel file in C#?
提问by Sophonias
I have been working with excel spreadsheets and so far I never had any problems with them.. But this error,"Not a legal OleAut date.", showed up out of the blue when I tried to read an excel file. Does anyone know how I can fix this. Here is the code I use to read the excel and put the data into a dataset. It has worked fine previously but after I made some changes (which doesn't involve dates) to the data source this error showed up.
我一直在使用 excel 电子表格,到目前为止我从来没有遇到过任何问题..但是当我试图阅读一个 excel 文件时,这个错误,“不是合法的 OleAut 日期。”,突然出现了。有谁知道我该如何解决这个问题。这是我用来读取excel并将数据放入数据集的代码。以前它运行良好,但是在我对数据源进行了一些更改(不涉及日期)后,出现了此错误。
var fileName = string.Format("C:\Drafts\Excel 97-2003 formats\All Data 09 26 2012_Edited.xls");
var connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; data source={0}; Extended Properties=Excel 8.0;", fileName);
var adapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", connectionString);
DataSet Originalds = new DataSet();
adapter.Fill(Originalds, "Employees"); // this is where the error shows up
采纳答案by Sophonias
I sort of figured out a work around to this problem I changed the connection string to the latest oleDB provider.
我找到了解决此问题的方法,我将连接字符串更改为最新的 oleDB 提供程序。
var connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0 Xml;HDR=YES;'", fileName);
I also made sure that the empty space after the last row of my excel data is not being read as a value.
我还确保我的 excel 数据的最后一行之后的空白区域没有被读取为值。
回答by Prem Kumar
Look for different date formats, in my case one of the column was in dd/mm/yyyy format and the other was in mm/dd/yyyy. Once the date formats were rectified data import worked.
寻找不同的日期格式,在我的例子中,其中一列采用 dd/mm/yyyy 格式,另一个采用 mm/dd/yyyy 格式。一旦日期格式被纠正,数据导入工作。
回答by Shamal Sabah
In my case, I had a date in my table inserted manually with the wrong format, it was 01/01/0019 instead of 01/01/2019.
就我而言,我在表格中手动插入了一个日期格式错误的日期,它是 01/01/0019 而不是 01/01/2019。

