【问题标题】:Error message "external table is not in the expected format" when I tried to upload file in C# program当我尝试在 C# 程序中上传文件时出现错误消息“外部表不是预期的格式”
【发布时间】:2012-06-12 02:43:05
【问题描述】:

我正在创建一个 C# 程序,我可以在其中浏览文件并将其查看到 C# 中的 datagridview 中。我已经对其进行了多次测试,并且效果很好。但是昨天,当我再次尝试执行此操作时,会显示一条错误消息,说明“Provider=Microsoft.Jet.OLEDB.4.0;”。我正在测试的文件与我之前使用的文件相同(没有错误)。我没有对文件(excel 文件)做任何事情。

这是错误消息的打印屏幕:

这是我使用的代码:

 private void buttonUpload_Click(object sender, EventArgs e)
    {
        string OleDBConnection = String.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""", textBoxFileName.Text);

        string query = String.Format("select * from [{0}$]", "Sheet1");

        OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, OleDBConnection);

        DataSet dataSet = new DataSet();

        dataAdapter.Fill(dataSet);

        dataGridView1.DataSource = dataSet.Tables[0];

    }

【问题讨论】:

  • 请阅读错误信息中的文字。 “外部表不是预期的格式”非常清楚 - 你告诉数据提供者在你的连接字符串中期望一个特定格式的文件 (Excel 8.0),而你正在打开的文件不在正确的格式,可能是因为您在textBoxFileName.Text 中拥有的文件不是受支持的文件。您的问题中绝对没有任何信息比这更有帮助 - 检查您在 textBoxFileName.Text 中传递的文件。

标签: c# excel datagridview oledb dataadapter


【解决方案1】:
 If Path.GetExtension(pth).ToLower().Equals(".xls") Then
     strcon = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & pth & ";Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;"""
 Else
     strcon = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & pth & ";Extended Properties=""Excel 12.0;HDR=YES;IMEX=1;"""
 End If

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多