Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 导入Excel时出现错误“;System.InvalidOperationException:Microsoft.Jet.OLEDB.4.0';“供应商未注册”;_C#_.net_Excel_Oledb - Fatal编程技术网

C# 导入Excel时出现错误“;System.InvalidOperationException:Microsoft.Jet.OLEDB.4.0';“供应商未注册”;

C# 导入Excel时出现错误“;System.InvalidOperationException:Microsoft.Jet.OLEDB.4.0';“供应商未注册”;,c#,.net,excel,oledb,C#,.net,Excel,Oledb,我有一个运行良好的老项目。现在,我在尝试从Excel导入时遇到错误: System.Invalidoperationexception未注册“microsoft.jet.oledb.4.0”提供程序 我可以登录。我尝试将项目的平台更改为x86/x64/任何CPU,但没有任何用处。 我也改变了: Provider=Microsoft.Jet.OLEDB.4.0;数据源 到 Provider=Microsoft.Jet.OLEDB.12.0;数据源 但是没有用。以下是我导入Excel的代码: p

我有一个运行良好的老项目。现在,我在尝试从Excel导入时遇到错误:

System.Invalidoperationexception未注册“microsoft.jet.oledb.4.0”提供程序

我可以登录。我尝试将项目的平台更改为x86/x64/任何CPU,但没有任何用处。 我也改变了:

Provider=Microsoft.Jet.OLEDB.4.0;数据源

Provider=Microsoft.Jet.OLEDB.12.0;数据源

但是没有用。以下是我导入Excel的代码:

 private void button1_Click_1(object sender, EventArgs e)
    {
      //  string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\BILL REGISTER 97.xls;Extended Properties=\"Excel 8.0;HDR=Yes;\";";
        //string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + txtfilepath.Text + ";Extended Properties=\"Excel 8.0;HDR=Yes;\";";
        string connStr = "Provider=Microsoft.Jet.OLEDB.12.0;Data Source=" + txtfilepath.Text + ";Extended Properties=\"Excel 8.0;HDR=Yes;\";";


        OleDbConnection con = new OleDbConnection(connStr);
       // string strCmd = "select * from [sheet1$A8:P10]";
       // string strCmd = "select * from [sheet1$A8:IV65536]";
        string strCmd = "select * from [sheet1$A6:IV65536]";
        OleDbCommand cmd = new OleDbCommand(strCmd, con);
        try
        {
            con.Open();
            ds.Clear();
            da.SelectCommand = cmd;
            da.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        finally
        {
            con.Close();
        }
    }
我已安装Office2010。

请尝试使用

Provider=Microsoft.ACE.OLEDB.12.0;Data Source
而不是

Provider=Microsoft.Jet.OLEDB.12.0;Data Source

哪个版本的Office?64位还是32位?如何检查?如果Office是32位,那么您应该使用ACE.12.0并为x86编译应用程序。(或任何32位CPU)看看这个答案,它可以帮助理解情况