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文件!");
        }
    }
因为爱上你,我才懂得珍惜,每一天日记,都写满了甜蜜
因为想念你,我每天都可以,对着镜子说我多爱你,有多想见到你。
原文地址:https://www.cnblogs.com/jackzhang/p/569184.html