导出Excel的简单方法,并可以解决乱码问题

来源:互联网 发布:app store升级mac os 编辑:程序博客网 时间:2024/06/10 03:54

private void HB_ToExecl_Click(object sender, System.EventArgs e)
  {
   HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=Excel.xls");
   HttpContext.Current.Response.Charset =""; 
   HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.UTF8;
   HttpContext.Current.Response.ContentType ="application/ms-excel";
   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("<head><meta http-equiv=/"Content-Type/" content=/"text/html; charset=utf-8/"></head><body>"+tw.ToString()+"</body>");
   HttpContext.Current.Response.End();
  } 

原创粉丝点击