HTTP标头发送后无法附加标头错误未解决

时间:2016-11-16 03:24:48

标签: c# .net asp.net-mvc c#-4.0 http-headers

此方法将数据表导出到excel

  public static void ExportToExcel(DataTable table)
        {
            try
            {
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.ClearContent();
                HttpContext.Current.Response.ClearHeaders();
                HttpContext.Current.Response.Buffer = true;
                HttpContext.Current.Response.ContentType = "application/ms-excel";
                HttpContext.Current.Response.Write(@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">");
                HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=Reports.xls");

                HttpContext.Current.Response.Charset = "utf-8";
                HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1250");
                //sets font
                HttpContext.Current.Response.Write("<font style='font-size:10.0pt; font-family:Calibri;'>");
                HttpContext.Current.Response.Write("<BR><BR><BR>");
                //sets the table border, cell spacing, border color, font of the text, background, foreground, font height
                HttpContext.Current.Response.Write("<Table border='1' bgColor='#ffffff' " +
                                                   "borderColor='#000000' cellSpacing='0' cellPadding='0' " +
                                                   "style='font-size:11.0pt; font-family:Calibri; background:white;'> <TR>");
                //am getting my grid's column headers
                int columnscount = table.Columns.Count;

                for (int j = 0; j < columnscount; j++)
                {
                    //write in new column
                    HttpContext.Current.Response.Write("<Td>");
                    //Get column headers  and make it as bold in excel columns
                    HttpContext.Current.Response.Write("<B>");
                    HttpContext.Current.Response.Write(table.Columns[j].ToString());
                    HttpContext.Current.Response.Write("</B>");
                    HttpContext.Current.Response.Write("</Td>");
                }
                HttpContext.Current.Response.Write("</TR>");
                foreach (DataRow row in table.Rows)
                {
//write in new row
                    HttpContext.Current.Response.Write("<TR>");
                    for (int i = 0; i < table.Columns.Count; i++)
                    {
                        HttpContext.Current.Response.Write("<Td>");
                        HttpContext.Current.Response.Write(HttpContext.Current.Server.HtmlEncode(row[i].ToString()));
                        HttpContext.Current.Response.Write("</Td>");
                    }

                    HttpContext.Current.Response.Write("</TR>");
                }
                HttpContext.Current.Response.Write("</Table>");
                HttpContext.Current.Response.Write("</font>");
                HttpContext.Current.Response.Flush();
                HttpContext.Current.Response.End();
            }
            catch (Exception ex)
            {
                string abc = ex.ToString();
            }
        }

这是我的控制器

 public ActionResult ExportData()
        {
            var industryData = _rawDataHlper.GetIndustryData();
            if (industryData != null)
            {
                ExportToExcel((industryData));
            }
            return RedirectToAction("Index");
        }

此方法从数据库中获取记录

 public DataTable GetIndustryData()
        {     
            IList<v_IndustryDetail> industryData = _insurancedb.v_IndustryDetail.OrderByDescending(x=>x.ReportYear).ToList();

            DataTable dt = new DataTable();

            return dt;

        }

我收到“无法在发送HTTP标头后添加标头”错误。我想重定向到Index操作。我试过搜索这个错误并尝试了不同的解决方案,而不是

Response.End()

使用

HttpContext.Response.SuppressContent = true;
HttpContext.ApplicationInstance.CompleteRequest();

还尝试清除标题

Response.ClearHeaders();

但是没有解决方案正在运行,我也遇到了同样的错误。

任何人都可以帮我解决这个问题。我花了将近一天的时间来解决这个问题,但仍在努力。任何帮助将不胜感激

提前致谢

1 个答案:

答案 0 :(得分:0)

不幸的是你不能这样做所以不要浪费你的时间。您需要做的是包含一个cookie,并在客户端查找cookie然后重定向。 [这里](http://gruffcode.com/2010/10/28/detecting-the-file-download-dialog-in-the-browser/)是如何使用cookie进行操作的示例。

添加标题以供下载后,您无法设置重定向标题。