ASP.NET2.0上传EXCEL文件到gridview中显示

protected void Button1_Click(object sender, EventArgs e)
    {
        string name = FileUpload1.FileName;
        string size = FileUpload1.PostedFile.ContentLength.ToString();
        string type = FileUpload1.PostedFile.ContentType;
        string type2 = name.Substring(name.LastIndexOf(".") + 1);
        string fpath = Server.MapPath("upfile") + "\\" + name;
        if (type2 == "xls")
        {
            FileUpload1.SaveAs(fpath);
            Response.Write("文件类型:" + type + "<br>文件名:" + name + "<br>文件类型2:" + type2 + "<br>");
            Response.Write("文件路径:" + fpath);
            string strConn = "Provider=Microsoft.Jet.Oledb.4.0;Data Source=" + fpath + ";Extended Properties=Excel 8.0";
            OleDbConnection conn = new OleDbConnection(strConn);
            OleDbDataAdapter adp = new OleDbDataAdapter("Select * from [sheet1$]", strConn);
            DataSet ds = new DataSet();
            adp.Fill(ds);
            GridView1.DataSource = ds.Tables[0].DefaultView;
            GridView1.DataBind();
        }
        else
        {
            Response.Write("只能上传excel文件!");
        }
    }

你可能感兴趣的:(GridView)