现在的位置: 首页 > 综合 > 正文

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

2013年07月24日 ⁄ 综合 ⁄ 共 1220字 ⁄ 字号 评论关闭

 

        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);
            }
        }

 

抱歉!评论已关闭.