asp.net 读取excel文件

来源:互联网 发布:a star算法 编辑:程序博客网 时间:2024/05/17 06:02
 

 //需要连接的Excel文件地址
            string excelUrl = this.Server.MapPath(@"App_data\demo.xls");
           
            //组装连接Excel文件的连接字符串并实例化连接
            OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+excelUrl+";Extended Properties=\"Excel 8.0;HDR=NO;IMEX=0\"");
            //创建命令文件
            OleDbCommand com = con.CreateCommand();
            //查询工作簿Sheet1
            com.CommandText = "SELECT * FROM [Sheet1$]";

            //实例化数据适配器
            OleDbDataAdapter adpt = new OleDbDataAdapter();
            adpt.SelectCommand = com;

          
            DataSet ds = new DataSet();
            //填充DataSet
            adpt.Fill(ds);

            //数据绑定
            this.GridView1.DataSource = ds;
            this.GridView1.DataBind();