gridview data export to excel in asp.net

前端 未结 7 1055
情歌与酒
情歌与酒 2020-12-15 11:25

i tried to transfer grid view data to excel .... But the output is a blank excel sheet.How to solve this problem?Is there any code to transfer grid view value to excel sheet

7条回答
  •  死守一世寂寞
    2020-12-15 11:54

    I think it will help you

    string filename = String.Format("Results_{0}_{1}.xls", DateTime.Today.Month.ToString(), DateTime.Today.Year.ToString());
            if (!string.IsNullOrEmpty(GRIDVIEWNAME.Page.Title))
                filename = GRIDVIEWNAME.Page.Title + ".xls";
    
            HttpContext.Current.Response.Clear();
    
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + filename);
    
    
            HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
            HttpContext.Current.Response.Charset = "";
    
            System.IO.StringWriter stringWriter = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
    
    
    
            System.Web.UI.HtmlControls.HtmlForm form = new System.Web.UI.HtmlControls.HtmlForm();
            GRIDVIEWNAME.Parent.Controls.Add(form);
            form.Controls.Add(GRIDVIEWNAME);
            form.RenderControl(htmlWriter);
    
            HttpContext.Current.Response.Write("");
            HttpContext.Current.Response.Write(stringWriter.ToString());
            HttpContext.Current.Response.End();
    

提交回复
热议问题