【问题标题】:OleDbException while trying to read excel file in c# - External table is not in the expected format尝试在 c# 中读取 excel 文件时出现 OleDbException - 外部表不是预期的格式
【发布时间】:2014-08-19 08:25:08
【问题描述】:

我正在开发一个 MVC .Net Web 应用程序,我想从 Excel 文件中读取一些数据。 我正在使用这些连接字符串:

if (fileExtension == ".xlsx")
{
    excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\"" +
    fileLocation + "\";Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\"";
}
else if (fileExtension == ".xls")
{
    excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
    fileLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"";
}

读取 .xls 文件效果很好,但尝试读取 .xlsx 文件(在 Excel 2013 中创建)不起作用,给我一个例外:

外部表不是预期的格式。

我一直在尝试我找到的所有可能的连接字符串:

excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\"" +
fileLocation + "\";Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\"";

excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\"" +
fileLocation + "\";Extended Properties=\"Excel 12.0;HDR=YES;IMEX=2\"";

excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\"" +
fileLocation + "\";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=2\"";

...
...
...

但我无法解决这个问题。

谢谢!

【问题讨论】:

    标签: c# excel connection-string oledb


    【解决方案1】:

    嘿,你的连接看起来不错。像这样

    public static string path = @"C:\src\RedirectApplication\RedirectApplication\301s.xlsx";
    public static string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;";
    

    但请确保您的计算机上安装了 Microsoft Access Database Engine 2010 Redistributable 软件包。您可以从这里下载 Link

    对于 excel 连接字符串,您可以导航此 url Link

    【讨论】:

    • no.... 我安装了它,但它没有帮助(Microsoft Access 与它有什么关系?)
    • 你能告诉我你的fileLocation变量内容吗
    • 我也试过@...文件位置内容是:“C:\Program Files (x86)\IIS Express\Book1.xlsx”,整个连接字符串是:Provider=Microsoft.ACE .OLEDB.12.0;Data Source="C:\Program Files (x86)\IIS Express\Book1.xlsx";Extended Properties="Excel 12.0 Xml;HDR=YES;IMEX=1"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-12
    • 2010-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多