文件内容在Web上显示

王朝学院·作者佚名  2016-05-21
窄屏简体版  字體: 小  |  中  |  大  |  超大  

读取Excel文件内容在Web上显示点击事件代码.cs

PRotected void Button1_Click(object sender, EventArgs e) { string strPath = "d:/test.xls"; string mystring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source = '" + strPath + "';Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'"; //"Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = '" + strPath + "';Extended Properties=Excel 8.0"; OleDbConnection cnnxls = new OleDbConnection(mystring); OleDbDataAdapter myDa = new OleDbDataAdapter("select * from [Sheet1$]", cnnxls); DataSet myDs = new DataSet(); myDa.Fill(myDs); DataGrid1.DataSource = myDs.Tables[0]; DataGrid1.DataBind(); }

如果使用经典的"Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = '" + strPath + "';Extended Properties=Excel 8.0"会报错:外部表不是预期的格式

这是因为:Microsoft.Jet.OLEDB.4.0是Microsoft Jet引擎,这适用于2003版本(2003之前的我没装,所以也不知道能向下适应到哪个版本),而在2007中,微软对其旗下 access 与 Excel 的主要文件格式进行修改,并且重命名为 .accdb(Access 2007 数据库文件)与 .xlsx(Excel 2007 文件),因此未被 Microsoft Jet 引擎所支持,不过微软也很快的提出了 Microsoft Office 2007 Desktop Drivers: Data Connectivity Components 来支持。

解决方法就是把连接字符串中的数据提供者改为 Microsoft.ACE.OLEDB.12.0

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航