简体   繁体   English

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

[英]Updating data in to a Excel sheet using c#

I am trying to update some data in an Excel sheet of the format "xlsx" using OLEDB connection, but I am unable to make out the connection establishment. 我正在尝试使用OLEDB连接更新格式为“ xlsx”的Excel工作表中的某些数据,但是无法确定连接的建立。

Here is my code: 这是我的代码:

        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();
    }

When I run the code, I'm facing the following exception: 运行代码时,我面临以下异常:

The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine. “ Microsoft.ACE.OLEDB.12.0”提供程序未在本地计算机上注册。

Any idea how to recover from this exception or any other suggestions from which i can update my data in excel is advisable. 建议任何想法如何从此异常中恢复或我可以从Excel中更新数据的任何其他建议。

Change your PlatformTarget Type from AnyCPU to X86 . 将您的PlatformTarget类型从AnyCPU更改为X86

Steps: 脚步:

Goto Project Properties. 转到项目属性。
Select Build tab. 选择Build选项卡。
Select X86 from PlatformTarget options. 从PlatformTarget选项中选择X86

This could be the provider you have stated try changing it to the one which matches the Excel version on your machine 这可能是您说过的提供程序,尝试将其更改为与您计算机上的Excel版本匹配的提供程序

Try 尝试

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

Instead 代替

Could also be that excel isnt installed 也可能是未安装excel

Also check that you have referenced the OLEDB library for your project 还要检查您是否已为项目引用了OLEDB库

It is possible that there are multiple reasons for this exception. 造成此异常的原因可能有多种。

1) You could use the OleDbEnumerator class to find out what providers are available. 1)您可以使用OleDbEnumerator类来查找可用的提供程序。 Accordingly you set your connection string. 因此,您设置了连接字符串。

2) Before that just try out below connection string. 2)在此之前,只需尝试以下连接字符串即可。 String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + "D:\\abc1.xlsx" + "';Extended Properties='Excel 8.0;HDR=Yes'"; 字符串sConnectionString =“ Provider = Microsoft.Jet.OLEDB.4.0; Data Source ='” +“ D:\\ abc1.xlsx” +“';扩展属性='Excel 8.0; HDR =是'”;

3) If you have a 64 bit OS, there is no 64-bit version of the JET provider available and there's no alternative. 3)如果您使用的是64位操作系统,则没有可用的64位版本的JET提供程序,也别无选择。 As long as you want to support JET, you'll need to set the build target to x86. 只要要支持JET,就需要将构建目标设置为x86。

first save as your excel workbook as Excel 97-2003 Workbook It will work in my project... 首先将另存为Excel 97-2003工作簿作为excel工作簿它将在我的项目中工作...

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();`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM