请问:如何使用gridView导出excel

解决方案 »

  1.   

    添加一个button 在click里面写入Response.ClearContent();Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");Response.ContentType = "application/excel";StringWriter sw = new StringWriter();HtmlTextWriter htw = new HtmlTextWriter(sw);gvUsers.RenderControl(htw);Response.Write(sw.ToString());Response.End();记得还要重载VerifyRenderingInServerForm方法。public override void VerifyRenderingInServerForm(Control control)
    {}
      

  2.   

    try
            {
                this.DataGrid2.Visible = true;
                Response.Clear();
                Response.Buffer = true;
                Response.Charset = "GB2312";
                Response.AppendHeader("Content-Disposition", "attachment;filename=" +
                    System.Web.HttpUtility.UrlEncode("日志信息", System.Text.Encoding.UTF8) + DateTime.Now.ToShortDateString() + ".xls");
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                Response.ContentType = "application/ms-excel";
                this.EnableViewState = false;
                System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
                this.DataGrid2.RenderControl(oHtmlTextWriter);
                Response.Write(oStringWriter.ToString());
                this.Response.End();
                this.EnableViewState = true;
                this.DataGrid2.Visible = false;
    }
    可以参考下