ASP.NET EXCEL2010数据读入到GridView以及64位系统补丁

将excel2010数据读入到GirdView中。

//pageLoad中绑定
protected void Page_Load(object sender, EventArgs e)
        {
            UserMessageGridView.DataSource = ExceltoDataSet(excelPath);//excel路径
            UserMessageGridView.DataBind();
        }


//从EXCEL读取数据并绑定,注意要用Microsoft.ACE.OLEDB.12.0,如图是在64为系统请下载AccessDatabaseEngine.exe

        private DataSet ExceltoDataSet(String ExcelPath)//Server.MapPath(ExcelPath)
        {
            string strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + ExcelPath + ";Extended Properties='Excel 12.0;HDR=YES;IMEX=1'";
            OleDbConnection oleCon = new OleDbConnection(strCon);
            OleDbDataAdapter oleDA = new OleDbDataAdapter("select * from [Sheet1$]", oleCon);
            DataSet ds = new DataSet();
            oleDA.Fill(ds);
            return ds;
            
        }

 //换页
        protected void MessageGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

            UserMessageGridView.PageIndex = e.NewPageIndex;

            UserMessageGridView.DataBind();
        }

你可能感兴趣的:(GridView,64位系统,excel2010)