关于报错:The Microsoft.ACE. Oledb.12.0 provider was not registered on the local computer

时间:2023-03-09 09:24:50
关于报错:The Microsoft.ACE. Oledb.12.0 provider was not registered on the local computer

错误描述:The Microsoft.ACE. Oledb.12.0 provider was not registered on the local computer

最近在Web项目中做一个自动生成Excel→下载→修改→上传→生成json格式文件的功能。

本地一切都顺利,在部署到IIS服务器后,运行发现挂了。

本人代码:

  #region 读取Excel中的数据
/// <summary> 
/// 读取Excel中的数据 支持表头(.xlsx)   不支持表头(.xls)  
/// </summary> 
/// <param name="fileName">Excel文件路径</param> 
/// <returns>Excel中的数据</returns> 
public DataTable GetTable(string fileName)
{
OleDbConnection Conn = null;
DataTable dt = null;
string connString = string.Empty;
OleDbDataAdapter da = new OleDbDataAdapter();
DataTable dataTable = new DataTable();
try
{
string FileType = fileName.Substring(fileName.LastIndexOf("."));
if (FileType == ".xls")
connString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + fileName + ";Extended Properties=Excel 8.0;";
else//.xlsx
connString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + fileName + ";" + ";Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\"";
// 创建连接对象
Conn = new OleDbConnection(connString);
// 打开数据库连接 
Conn.Open();
//获取Excel工作薄中Sheet页(工作表)名集合
DataTable ss = Conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" });
string sql_F = "Select * FROM [{0}]";
for (int i = ; i < ss.Rows.Count; i++)
{
da.SelectCommand = new OleDbCommand(String.Format(sql_F, ss.Rows[i][].ToString()), Conn);
da.Fill(dataTable);
}
return dataTable;
}
catch (Exception ex)
{
string Error = ErrorUtil.GetError(ex); if (log.IsDebugEnabled)
{
log.Debug("GenerateFinfo - Error2 : [" + Error + "]");
}
throw (ex);
}
finally
{
// 释放 
if (Conn != null)
{
Conn.Close();
Conn.Dispose();
}
if (dt != null)
{
dt.Dispose();
}
}
}
#endregion

......

......

在我各种查阅,对比,实践后把解决方法记录下来,和大家分享:

报错原因是本地安装了Office客户端,但是服务器没有安装Office客户端。

我们都知道,安装一个Office客户端是需要占一些内存的,这不利于服务器的性能。

所以我就查询了资料,结合实践,有了不安装Office客户端也能读取Excel的解决办法。

解决办法:

第一步:安装数据访问组件:(AccessDatabaseEngine)

  1)适用于office2007的Access组件

  Microsoft Access Database Engine 2007 Office system 驱动程序:数据连接组件

  2)适用于office2010的Access组件

  Microsoft Access Database Engine 2010 Redistributable

下载安装后电脑里便会有一组组件,非 Microsoft Office 应用程序可以使用它们从 2007/2010 Office system 文件中读取数据,

例如:

1)从 Microsoft Office Access 2007/2010(mdb 和 accdb)文件中读取数据;

2)从Microsoft Office Excel 2007/2010(xls、xlsx 和 xlsb)文件中读取数据。

这些组件还支持与 Microsoft Windows SharePoint Services 和文本文件建立连接。

         此外,还会安装 ODBC 和 OLEDB 驱动程序,供应用程序开发人员在开发与 Office 文件格式连接的应用程序时使用。

第二步:打开你所部署项目的电脑的IIS管理器,把你用的那个程序池修改“启用兼容32位应用程序” 属性值为True。

如下图所示:

关于报错:The Microsoft.ACE. Oledb.12.0 provider was not registered on the local computer

以上方法,亲测有效,如果你遇到类似的问题依旧无法解决,可以查看一下Excel。有的读取Excel的方法分xlxs和xls两种文件。

              关于报错:The Microsoft.ACE. Oledb.12.0 provider was not registered on the local computer

              

             如有更多问题可查看: https://www.cnblogs.com/yifeixue/   或者:http://www.cnblogs.com/willingtolove/