首页 新闻 会员 周边

.net上传xls格式文件报错: 未在本地计算机上注册“microsoft.ACE.oledb.12.0"提供程序

0
[已解决问题] 解决于 2018-01-18 08:04

.net上传xls格式文件报错: 未在本地计算机上注册“microsoft.ACE.oledb.12.0"提供程序

上传xlsx则正常。按理说xlsx格式是高版本的,既然服务器上高版本的可以读取数据,为什么低版本xls却读取失败了?高版本的无法读取低版本吗?

这个错误网上大多是要安装驱动或者修改项目属性

问:能不能通过代码规避这个错误?

 1         #region 获取链接字符串
 2         /// <summary>
 3         /// 获取链接字符串
 4         /// </summary>
 5         /// <param name="filepath">相对路径</param>
 6         /// <returns>链接字符串</returns>
 7         private static string getconstring(string filepath)
 8         {
 9             string constring;
10             if (filepath.EndsWith(".xls"))
11             {
12                 // Excel 97-2003
13                 constring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=YES;IMEX=1'";
14             }
15             else
16             {
17                 // Excel 2007
18                 constring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0 Xml;HDR=YES;IMEX=1'";
19             }
20             return string.Format(constring, System.Web.HttpContext.Current.Server.MapPath(filepath));
21         }
22         #endregion
23 
24         #region 读取excel表格sheet的名称
25         /// <summary>
26         /// 读取excel表格sheet的名称
27         /// </summary>
28         /// <param name="filepath">相对路径</param>
29         /// <returns>excel表格sheet的名称数组</returns>
30         public static string[] getexcelsheetnames(string filepath)
31         {
32             using (OleDbConnection con = new OleDbConnection(getconstring(filepath)))
33             {
34                 con.Open();
35                 DataTable dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
36                 con.Close();
37                 if (dt == null)
38                 {
39                     return null;
40                 }
41                 string[] excelsheetnames = new string[dt.Rows.Count];
42                 int i = 0;
43                 foreach (DataRow dr in dt.Rows)
44                 {
45                     excelsheetnames[i++] = dr["table_name"].ToString();
46                 }
47                 return excelsheetnames;
48             }
49         }
50         #endregion
51 
52         #region 读取excel文件
53         /// <summary>
54         /// 读取excel文件
55         /// </summary>
56         /// <param name="filepath">相对路径</param>
57         /// <param name="sheetname">查询Excel表格sheet的名称</param>
58         /// <returns>DataTable</returns>
59         public static DataTable readexcel(string filepath, string sheetname)
60         {
61             using (OleDbConnection con = new OleDbConnection(getconstring(filepath)))
62             {
63                 if (string.IsNullOrEmpty(sheetname))
64                 {
65                     sheetname = getexcelsheetnames(filepath)[0];//如果Excel表格sheet的名称为空,给个默认值
66                 }
67                 using (OleDbDataAdapter oda = new OleDbDataAdapter(string.Format("select * from [{0}]", sheetname), con))
68                 {
69                     DataTable dt = new DataTable();
70                     
71                     con.Open();
72                     oda.Fill(dt);
73                     con.Close();
74                     return dt;
75                 }
76             }
77         }
大da脸的主页 大da脸 | 初学一级 | 园豆:73
提问于:2018-01-12 10:10
< >
分享
最佳答案
0

用第三方组件不需要office,也就没有版本问题。Free Spire.XLS读取Excel很简单

获取Sheet Name:

Workbook workbook = new Workbook();
workbook.LoadFromFile("FileName");
String name = workbook.Worksheets[0].Name;

导出DataTable:

Workbook workbook = new Workbook();
workbook.LoadFromFile("FileName");
DataTable dt = workbook.Worksheets[0].ExportDataTable();

 

奖励园豆:5
ms_doudou | 小虾三级 |园豆:1166 | 2018-01-17 15:21
其他回答(3)
0

貌似不能.

顾星河 | 园豆:7173 (大侠五级) | 2018-01-12 10:39
0

npoi ,第三方的,特别好使,不需要office组件就可以哦

百鸟朝凤 | 园豆:260 (菜鸟二级) | 2018-01-12 11:56
0

 office自带的dll有版本兼容问题,低版本和高版本不能通用的,当前坑的不轻,用NPOI读写轻松解决

jqw2009 | 园豆:2439 (老鸟四级) | 2018-01-12 14:40
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册