EXCEL的导出

时间:2023-03-09 07:06:30
EXCEL的导出

具体实现代码:

  protected void EXCEL_Click(object sender, EventArgs e)
{
ToExcel(GridView1);
} public void ToExcel(System.Web.UI.Control ctl)
{
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=grid.xls");
// HttpContext.Current.Response.Write("<metahttp-equiv=Content-Type content=text/html; charset=UTF-8>");
HttpContext.Current.Response.Charset = "UTF-8"; //"GB2312";//
HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
HttpContext.Current.Response.ContentType = "application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
ctl.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}

前台只需要添加一个button按钮就可。