GridView 导出Excle

       
方法一:

//    Response.ContentEncoding = System.Text.Encoding.UTF7;
        //    Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("NPRB.xls", System.Text.Encoding.UTF7).ToString());
        //    Response.ContentType = "application/ms-excel";
        //    this.EnableViewState = false;
        //    StringWriter tw = new StringWriter();
        //    HtmlTextWriter hw = new HtmlTextWriter(tw);
        //    gdvExcel.Visible = true;
        //    gdvExcel.RenderControl(hw);
        //    Response.Write(tw.ToString());
        //    gdvExcel.Visible = false;
        //    Response.End();

方法二:

 

Response.Clear();
            //string sText = "<meta http-equiv='Content-Type'; content='text/html';charset='{0}'>";
            //sText = String.Format(sText, Encoding.Default.WebName);
            Response.Write("<html xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\">");

            //Response.Write("<head>");
            Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=big5\">");
            Response.Write("<meta name=ProgId content=Excel.Sheet>");
            Response.Write("<meta name=Generator content=\"Microsoft Excel 11\">");
            Response.AddHeader("content-disposition", "attachment;filename=NPRB.xls");

            Response.ContentEncoding = System.Text.Encoding.UTF7;
            Response.Charset = Encoding.Default.WebName;
            Response.ContentType = "application/ms-excel";

            System.IO.StringWriter sw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htw = new HtmlTextWriter(sw);

            gdvNprb.AllowPaging = false;
            gdvNprb.AllowSorting = false;
            gdvNprb.EnableViewState = false;
            gdvNprb.Columns[0].Visible = false;
            gdvNprb.Columns[1].Visible = true;
            gdvNprb.Columns[8].Visible = false;
            gdvNprb.DataBind();

            //dgvPNList.RenderControl(htw);

            HtmlForm hf = new HtmlForm();
            Controls.Add(hf);
            hf.Controls.Add(gdvNprb);
            hf.RenderControl(htw);

            string output = sw.ToString();

            Response.Write(output);
            Response.Write("</html>");
            Response.End();

 

posted on 2009-07-24 19:49  AndyCai  阅读(205)  评论(0编辑  收藏  举报

导航