EXCEL的导出
具体实现代码:
1 protected void EXCEL_Click(object sender, EventArgs e) 2 { 3 ToExcel(GridView1); 4 } 5 6 public void ToExcel(System.Web.UI.Control ctl) 7 { 8 HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=grid.xls"); 9 // HttpContext.Current.Response.Write("<metahttp-equiv=Content-Type content=text/html; charset=UTF-8>"); 10 HttpContext.Current.Response.Charset = "UTF-8"; //"GB2312";// 11 HttpContext.Current.Response.ContentEncoding = Encoding.UTF8; 12 HttpContext.Current.Response.ContentType = "application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword 13 ctl.Page.EnableViewState = false; 14 System.IO.StringWriter tw = new System.IO.StringWriter(); 15 System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw); 16 ctl.RenderControl(hw); 17 HttpContext.Current.Response.Write(tw.ToString()); 18 HttpContext.Current.Response.End(); 19 }
前台只需要添加一个button按钮就可。