为了账号安全,请及时绑定邮箱和手机立即绑定

为什么我的导出到 Excel 功能会导出整个页面?

为什么我的导出到 Excel 功能会导出整个页面?

C#
呼唤远方 2023-09-24 16:30:56
我正在尝试导出显示在窗口(模式)中的网格视图,但它导出整个页面。public void ExportToXLS(GridView gv){    gv.AllowPaging = false;    HttpContext.Current.Response.Buffer = true;    HttpContext.Current.Response.ClearContent();    HttpContext.Current.Response.ClearHeaders();    HttpContext.Current.Response.Clear();    HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=GridView.xls");    HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";    System.IO.StringWriter stringWrite = new System.IO.StringWriter();    System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);    GridView gvExp = new GridView();    gvExp = gv;    gvExp.RenderControl(htmlWrite);    HttpContext.Current.Response.Write(stringWrite.ToString());    HttpContext.Current.Response.Flush();    HttpContext.Current.Response.Close();    HttpContext.Current.Response.End();}
查看完整描述

1 回答

?
慕容森

TA贡献1853条经验 获得超18个赞

问题是我在 gridview 中有一个链接按钮,它导致页面无法正确呈现。


解决方案很简单,我只是删除了链接按钮认为我确实不需要它们的列。


public void ExportToXLS(GridView gv)

{


    GV.Columns[4].Visible = false;

    GV.Columns[5].Visible = false;


    gv.AllowPaging = false;

    HttpContext.Current.Response.Buffer = true;

    HttpContext.Current.Response.ClearContent();

    HttpContext.Current.Response.ClearHeaders();

    HttpContext.Current.Response.Clear();

    HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=GridView.xls");

    HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";

    System.IO.StringWriter stringWrite = new System.IO.StringWriter();

    System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

    GridView gvExp = new GridView();

    gvExp = gv;

    gvExp.RenderControl(htmlWrite);

    HttpContext.Current.Response.Write(stringWrite.ToString());

    HttpContext.Current.Response.Flush();

    HttpContext.Current.Response.Close();

    HttpContext.Current.Response.End();

}

GV.Columns[ ].Visible = false;一开始的代码行刚刚解决了我的整个问题。


查看完整回答
反对 回复 2023-09-24
  • 1 回答
  • 0 关注
  • 149 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信