c# 导出excel(将数据直接输出到响应)

 

        public void ExportExcel()
        {
            Response.Clear();
            Response.Buffer 
= true;
            
//Response.Charset="utf-8";
            
            Response.AppendHeader(
"Content-Disposition""attachment;filename=myExcel.xls");           
            Response.ContentEncoding 
= System.Text.Encoding.GetEncoding("gb2312");//设置输出流为简体中文
            Response.Write("<meta http-equiv=Content-Type content=application/vnd.ms-excel;charset=gb2312>");//设置输出文件类型为excel文件。 

            
this.EnableViewState = false;

            System.Globalization.CultureInfo myCItrad 
= new System.Globalization.CultureInfo("ZH-CN"true);
            System.IO.StringWriter oStringWriter 
= new System.IO.StringWriter(myCItrad);
            System.Web.UI.HtmlTextWriter oHtmlTextWriter 
= new System.Web.UI.HtmlTextWriter(oStringWriter);
            
this.gridXls.RenderControl(oHtmlTextWriter);  
            Response.Write(oStringWriter.ToString());
            Response.End();
        }

       //重写Page的下面方法.

        public override void VerifyRenderingInServerForm(Control control)
        {
            if (!control.GetType().Equals(gridXls.GetType()))
            {
                base.VerifyRenderingInServerForm(control);
            }
        }


 

你可能感兴趣的:(导出Excel)