首页 文章

使用c#将数据更新到Excel工作表

提问于
浏览
1

我正在尝试使用OLEDB连接更新格式为“xlsx”的Excel工作表中的某些数据,但我无法确定连接 Build .

这是我的代码:

String sConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source='" + "D:\abc1.xlsx" + "';Extended Properties='Excel 8.0;HDR=Yes'";
        OleDbConnection con = new OleDbConnection(sConnectionString);

        con.Open();
        OleDbCommand com = new OleDbCommand("select * from Sheet1",con);
        OleDbDataReader reader = null;

        reader = com.ExecuteReader();
        while (reader.Read())
        {
            Console.WriteLine(reader[0]);
        }
        con.Open();

        Console.ReadLine();
    }

当我运行代码时,我面临以下异常:

“Microsoft.ACE.OLEDB.12.0”提供程序未在本地计算机上注册 .

任何想法如何从此异常或任何其他建议中恢复,我可以在Excel中更新我的数据是可取的 .

4 回答

  • 0

    这可能是您声明的提供商尝试将其更改为与您的计算机上的Excel版本匹配的提供商

    尝试

    Provider=Microsoft.ACE.OLEDB.12.0;Data Source='D:\abc1.xlsx';Extended Properties="Excel 12.0 Xml;HDR=YES";
    

    代替

    也可能是没有安装excel

    还要检查是否已为项目引用了OLEDB库

  • -2

    PlatformTarget 类型从 AnyCPU 更改为 X86 .

    脚步:

    转到项目属性 .
    选择 Build 选项卡 .
    从PlatformTarget选项中选择 X86 .

  • 0

    此异常可能有多种原因 .

    1)您可以使用OleDbEnumerator类来查找可用的提供程序 . 因此,您设置您的连接字符串 .

    2)在此之前,尝试下面的连接字符串 . String sConnectionString =“Provider = Microsoft.Jet.OLEDB.4.0; Data Source ='”“D:\ abc1.xlsx”“'; Extended Properties ='Excel 8.0; HDR = Yes'”;

    3)如果您有64位操作系统,则没有可用的64位版本的JET提供程序,并且没有其他选择 . 只要您想支持JET,就需要将构建目标设置为x86 .

  • 0

    首先保存为excel工作簿作为Excel 97-2003工作簿它将在我的项目中工作...

    string filepath = Server.MapPath("~/ImportData/") + fileUpload.FileName;
     OleDbConnection oconn = new OleDbConnection
     (@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";  
            Extended Properties=Excel 8.0");`
    
    
        oconn.Open();
        OleDbDataAdapter adp = new OleDbDataAdapter("select * from Sheet1", oconn);
        DataSet ds = new DataSet();
    
        adp.Fill(ds);
        oconn.Close();`
    

相关问题