【问题标题】:Trouble with 'External table is not in the expected format' exception [duplicate]“外部表不是预期格式”异常的问题[重复]
【发布时间】:2013-01-08 10:09:48
【问题描述】:

可能重复:
Excel “External table is not in the expected format.”

我正在做的是获取所有表名并将它们插入到列表中。这是我的代码:

public List<string> GetEditExcelSheets(string fileName, out OleDbException Error)
{
    List<string> Result = new List<string>();
    Error = null;
    if (!string.IsNullOrEmpty(fileName) && File.Exists(fileName))
    {
        string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties=\"Excel 12.0 XML;HDR=YES;IMEX=1\"";

        if (!string.IsNullOrEmpty(connectionString))
        {
            using (OleDbConnection connection = new OleDbConnection(connectionString))
            {
                try
                {
                    connection.Open();
                    DataTable tables = connection.GetSchema("Tables");
                    foreach (DataRow row in tables.Rows)
                    {
                        string TableName = Convert.ToString(row["TABLE_NAME"]);
                        Result.Add(TableName);
                    }
                }

                catch (OleDbException ex)
                {
                    Error = ex;
                }
                finally
                {
                    if (connection.State == System.Data.ConnectionState.Open)
                    {
                        connection.Close();
                    }
                }
            }
        }
    }
    return Result;
}

我收到此错误:

"'外部表不是预期的格式'"

到达此代码行时:

connection.Open();

在谷歌上搜索解决方案后,我曾尝试编辑连接字符串几次。但是没有其他连接字符串可以帮助我,并且这个连接字符串应该可以工作。我似乎可以弄清楚我做错了什么。

【问题讨论】:

  • 您是否提供了完全限定的文件名?
  • 您是否确定您尝试打开的现有 Excel 工作表是您在字符串中编码的正确版本?
  • @James(和其他人):你读过建议的副本吗?至少接受的答案没有帮助,因为他已经使用了那个连接字符串。
  • @Lahib: 你要打开哪个版本的 excel,xls 还是 xlsx?
  • 我刚刚检查了它正在使用的文件是否包含一张工作表,并且它正在使用正确的 excel 文件。并且没有该文件的副本。这是完整连接字符串的样子: Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\users\lmy\documents\visual studio 2012\Projects\CustomerImportV2\CustomerImportV2\Upload\20130108111646excel.xlsx ;扩展属性="Excel 12.0 XML;HDR=YES;IMEX=1"

标签: c# asp.net .net


【解决方案1】:

我找到了解决方案。当我使用开放 XML sdk 时,xlsx 文件以 xlsx 扩展名保存,但该文件不是 excel 文件。这是一个以 xlsx 扩展名保存的打开的 xml 文件,以便 Excel 可以打开它。这意味着我不能使用 sql 查询来读取文件中的数据。

【讨论】:

  • 您好,是不是说OleDB不能读取用OpenXML创建的excel?
  • @SebastianWidz 在我写这篇文章的时候我做不到。
  • 我问是因为我正在为完全相同的问题而苦苦挣扎。我想知道这是否可能,我知道 EPPlus 能够做到这一点,但他们编写的是纯 XML。使用纯 OpenXML,我得到了描述的错误,我不确定 OleDb 不喜欢 xls 的哪个部分
猜你喜欢
  • 1970-01-01
  • 2015-04-02
  • 1970-01-01
  • 2010-11-11
  • 2016-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多