码迷,mamicode.com
首页 > Web开发 > 详细

读取Excel文件内容在Web上显示

时间:2014-08-20 16:09:32      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   使用   os   io   

点击事件代码.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

读取Excel文件内容在Web上显示,布布扣,bubuko.com

读取Excel文件内容在Web上显示

标签:des   style   blog   http   color   使用   os   io   

原文地址:http://www.cnblogs.com/Vennet/p/3924681.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!