【问题标题】:External table is not in the expected format.外部表不是预期的格式。
【发布时间】:2015-03-08 04:39:36
【问题描述】:

我正在创建一个小应用程序来将 excel 数据导入我的数据库,当我单击按钮时它会因错误而崩溃

外部表不是预期的格式。

我尝试用谷歌搜索并在这里和那里更改代码,但问题仍然存在。我尝试将文件保存为 .xls,当我运行代码时,页面因 google chrome 的此网页不可用(甚至无法进入调试)而脱机

这是我的代码:

    string strConnection = ConfigurationManager.ConnectionStrings["---"].ConnectionString;

    String excelConnString = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0\"", filePath);
    //Create Connection to Excel work book 
    using (OleDbConnection excelConnection = new OleDbConnection(excelConnString))
    {
        //Create OleDbCommand to fetch data from Excel 
        using (OleDbCommand cmd = new OleDbCommand("Select * from [Sheet1$]", excelConnection))
        {
            excelConnection.Open();
            using (OleDbDataReader dReader = cmd.ExecuteReader())
            {
                using (SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection))
                {
                    //Give your Destination table name 
                    sqlBulk.DestinationTableName = "TableName";
                    sqlBulk.WriteToServer(dReader);
                }
            }
        }
    }

【问题讨论】:

标签: c# asp.net excel oledb


【解决方案1】:

假设您已完成所有准备工作,包括安装 Microsoft Access 2010 Runtime 以获取相关驱动程序,请尝试以下操作,因为我过去成功过。

如果您的输入文件是 *.xlsx 类型,则尝试替换

Extended Properties="Excel 12.0"

在您的连接字符串中使用

Extended Properties="Excel 12.0 Xml"

此问题通常发生在有问题或格式不匹配的 Excel 文件中,因此根据经验,您首先要尝试打开 Excel 并创建一个新的 .xlsx 文件,其中包含几个示例数据您自己输入(而不是复制/粘贴)。你不需要太多的输入来测试你的代码 sn-p。几个具有您期望的正确信息的单元格就可以了。这种方法消除了实际 Excel 文件而不是 C# 代码的任何问题。

让我们知道您的进展情况。

【讨论】:

    猜你喜欢
    • 2015-04-02
    • 2010-11-11
    • 2016-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多