日期:2014-05-20  浏览次数:20442 次

asp.net导出的excel文件 显示下载页面时只闪了一下 怎么办啊啊啊啊
asp.net导出的excel文件 显示下载页面时只闪了一下 代码

  Response.Charset = "utf-8";
  Response.ContentEncoding = System.Text.Encoding.UTF8;
  Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("帐户往来汇总表.xls", System.Text.Encoding.UTF8).ToString());
  Response.ContentType = "application/ms-excel";

  //将表格内容作为Excel输出  
  this.EnableViewState = false;
  StringWriter tw = new StringWriter();
  HtmlTextWriter hw = new HtmlTextWriter(tw);
  GetTable(Table1);
  this.Table1.RenderControl(hw);
  System.Text.StringBuilder sbContent = new System.Text.StringBuilder();
  sbContent.Append("<html>\r\n<head>\r\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\r\n</head>\r\n<body>\r\n");
  sbContent.Append(tw.ToString());
  sbContent.Append("</body></html>");
  Response.Write(sbContent.ToString());
  Response.End();

------解决方案--------------------
IE拦截了
------解决方案--------------------

利用Html方式导出Excel很通用,但是你的方法太简单了,导出的时候页面不好看,更要注意的是导出图片的时候就不行了,我建议你最好用Com组件导出Excel,当然如果只是文字数据就无所谓了。
再就是你说下载页面时只闪了一下,意思是弹出了文件下载对话框,很快就自动关闭了?要是那样,肯定是浏览器设置的原因,可能像楼上说的被拦截了,或者其它的。
给你一个我写的Html导出Excel的方法,你可以修改下:
/// <summary>
/// 将控件中内容导出到Excel中
/// </summary>
/// <param name="ctr">控件名称</param>
/// <param name="stylestring">样式列表</param>
public static void ExportToExcel(System.Web.UI.Control ctr,string stylestring)
{
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + "" + System.DateTime.Now.ToString("yyyyMMddhhmmss") + ".xls");
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
HttpContext.Current.Response.ContentType = "application/ms-excel/ms-word";
ctr.Page.EnableViewState = true;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
ctr.RenderControl(hw);
HttpContext.Current.Response.Write(string.Format(@"<html xmlns:x='urn:schemas-microsoft-com:office:excel'><head>
<!--[if gte mso 9]>
<xml>
<x:ExcelWorkbook>
<x:ExcelWorksheets>
<x:ExcelWorksheet>
<x:Name>工作表1</x:Name>
<x:WorksheetOptions>
<x:Print>
<x:ValidPrinterInfo />
<x:PaperSizeIndex>9</x:PaperSizeIndex>
</x:Print>
</x:WorksheetOptions>
</x:ExcelWorksheet>
</x:ExcelWorksheets>
</x:ExcelWorkbook>
</xml>
<![endif]-->
<meta http-equiv='Content-Type' content='text/html; charset=gb2312'>{0}</head><body>", stylestring));
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.Write("</body></html>");