【问题标题】:Updating data in to a Excel sheet using c#使用 c# 将数据更新到 Excel 工作表中
【发布时间】:2014-01-13 09:29:00
【问题描述】:

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

这是我的代码:

        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 中的数据的建议都是可取的。

【问题讨论】:

  • "当我运行代码时,我面临以下异常.." 那么这个异常是什么?
  • 什么样的异常?异常的信息是什么?
  • “Microsoft.ACE.OLEDB.12.0”提供程序未在本地计算机上注册。
  • 问题很可能与您的应用程序的位数有关。尝试为 x86 构建并查看应用程序是否有效。
  • 你需要安装,看这里 [Microsft.ACE.OLEDB.12.0 provider not registered on local machine][1] [1]: stackoverflow.com/questions/6649363/…

标签: c# excel oledb


【解决方案1】:

这可能是您所说的提供者,尝试将其更改为与您机器上的 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

    【讨论】:

      【解决方案3】:

      此异常可能有多种原因。

      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。

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-01-22
          • 2013-03-25
          相关资源
          最近更新 更多