症状
DataGrid导出到excel,如果不保存导出的Xls文件,会自动关闭当前页面。
代码
public void DGToExcel(DataGrid dataGrid)
{
HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=Excel.xls");
HttpContext.Current.Response.Charset ="UTF-8";
HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.Default;
HttpContext.Current.Response.ContentType ="application/ms-excel";
dataGrid.Page.EnableViewState =false;
System.IO.StringWriter tw = new System.IO.StringWriter() ;
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter (tw);
dataGrid.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
解决:
方法一:
HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=Excel.xls") 中去掉“attachment;”即可
方法二:
打开新的页面,执行输出流导出方法
其他:
框架页貌似不存在这样的情况