错误:“未在本地计算机上注册“microsoft.ACE.oledb.12.0”

   C#通过OleDb访问Excel 错误信息:未在本地计算机上注册“microsoft.ACE.oledb.12.0”提供程序。网上查询得知需要下载安装Microsoft Access 2010 数据库引擎可再发行程序包。下载地址为:https://www.microsoft.com/zh-CN/download/details.aspx?id=13255。

   

支持的操作系统

Windows 7, Windows 8, Windows Server 2003, Windows Server 2003 Service Pack 1, Windows Server 2003 Service Pack 2, Windows Server 2008 R2, Windows Server 2008 Service Pack 2, Windows Vista Service Pack 1, Windows XP Service Pack 2

    • 只有 32 位 Access 数据库引擎可在 Windows XP Service Pack 3 上使用

使用场景:

  1. 如果使用 OLEDB 的应用程序,将 ConnectionString 属性的 Provider 参数设置为“Microsoft.ACE.OLEDB.12.0”。 

    如果连接到 Microsoft Office Excel 数据,根据 Excel 文件类型添加相应的 OLEDB 连接字符串扩展属性: 

    文件类型(扩展名)                                             扩展属性
    ---------------------------------------------------------------------------------------------
    Excel 97-2003 工作簿 (.xls)                                  “Excel 8.0”
    Excel 2007-2010 工作簿 (.xlsx)                             “Excel 12.0 Xml”
    启用宏的 Excel 2007-2010 工作簿 (.xlsm)     “Excel 12.0 宏”
    Excel 2007-2010 非 XML 二进制工作簿 (.xlsb)     “Excel 12.0”

  2. 如果您是使用 ODBC 连接到 Microsoft Office Access 数据的应用程序开发人员,请将连接字符串设置为“Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=path to mdb/accdb file”
  3. 如果您是使用 ODBC 连接到 Microsoft Office Excel 数据的应用程序开发人员,请将连接字符串设置为“Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=path to xls/xlsx/xlsm/xlsb file”

一个测例子:

 1  public static DataTable GetData(string fileName)
 2         {
 3             if (!File.Exists(fileName)) return null;
 4             //string connStr = string.Format(@"Provider=MMicrosoft.ACE.OLEDB.12.0;data source='{0}';Extended Properties='Excel 8.0;HDR=YES;IMEX=1';", fileName);
 5             string connStr = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;data source='{0}';Extended Properties='Excel 12.0;HDR=YES;IMEX=1';", fileName);
 6             using (OleDbConnection conn = new OleDbConnection(connStr))
 7             {
 8 
 9                 try
10                 {
11                     conn.Open();
12                     DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
13                     var tableName = dt.Rows[0][2].ToString().Trim();
14                     string cmdTxt = string.Format("select * from [{0}]", tableName);
15                 
16                 //    Trace.Write("查询表:" + cmdTxt);
17                     OleDbDataAdapter da = new OleDbDataAdapter(cmdTxt, conn);
18                     DataSet ds = new DataSet();
19                     da.Fill(ds);
20                     return ds.Tables[0];
21                 }
22                 catch (Exception e)
23                 {
24 
25                   //  Trace.WriteLine("GetData错误: " + e.Message);
26                     return null;
27                 }
28                 finally
29                 {
30                     conn.Close();
31                 }
32             }
33         }

 

转载于:https://www.cnblogs.com/tonginyLiu/p/5576737.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值