2016-04-13 52 views
-1

我正在"Server cannot set content type after HTTP headers have been sent "例外下面的代碼出口甚至很多解決方法後練成服務器無法設置內容類型之後HTTP標頭已被送往例外

private void ExportToExcel() 
    { 
     try 
     { 
      DateTime date = new DateTime(); 

      HttpContext.Current.Response.Clear(); 
      HttpContext.Current.Response.ClearContent(); 

      HttpContext.Current.Response.ClearHeaders(); 
      HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls"); 
      HttpContext.Current.Response.ContentType = "application/ms-excel"; 
      HttpContext.Current.Response.BufferOutput = true; 
      HttpContext.Current.Response.Write(@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">"); 

      HttpContext.Current.Response.Charset = "utf-8"; 

      //Check the current culture;if its polish/Czech set the windows code to 1250 else set the windows code to 1252 

      if (CultureInfo.CurrentCulture.ThreeLetterISOLanguageName.ToUpper(CultureInfo.CurrentCulture) == "CES" || CultureInfo.CurrentCulture.ThreeLetterISOLanguageName.ToUpper(CultureInfo.CurrentCulture) == "POL") 
       HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1250"); 
      else 
       HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1252"); 

      HttpContext.Current.Response.Write("<font style='font-size:10.0pt; font-family:Calibri;'>"); 
      HttpContext.Current.Response.Write("<BR><BR><BR>"); 
      HttpContext.Current.Response.Write("<Table border='1' bgColor='#ffffff' " + 
       "borderColor='#000000' cellSpacing='0' cellPadding='0' " + 
       "style='font-size:10.0pt; font-family:Calibri; background:white;'> <TR>"); 

      for (int j = 0; j < gridView.Columns.Count; j++) 
      { 
       HttpContext.Current.Response.Write("<Td>"); 
       HttpContext.Current.Response.Write("<B>"); 
       HttpContext.Current.Response.Write(gridView.Columns[j].HeaderText.ToString()); 
       HttpContext.Current.Response.Write("</B>"); 
       HttpContext.Current.Response.Write("</Td>"); 
      } 
      HttpContext.Current.Response.Write("</TR>"); 
      foreach (DataRow row in orderDetails.Rows) 
      { 
       //format DateTime to show only Date 
       if (isDeliveries && row[4].ToString() != null) 
        date = (DateTime)row[4]; 
       else if (row[5].ToString() != null) 
        date = (DateTime)row[5]; 

       HttpContext.Current.Response.Write("<TR>"); 
       for (int i = 0; i < orderDetails.Columns.Count; i++) 
       { 
        if (i < columnsOrder.Length) 
        { 
         if (i == 4) 
         { 
          HttpContext.Current.Response.Write("<Td>"); 
          HttpContext.Current.Response.Write(date.ToString("d")); 
          HttpContext.Current.Response.Write("</Td>"); 
         } 
         else 
         { 
          HttpContext.Current.Response.Write("<Td>"); 
          HttpContext.Current.Response.Write(row[columnsOrder[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.SuppressContent = true; 
      HttpContext.Current.ApplicationInstance.CompleteRequest(); 
     } 
     catch (Exception ex) 
     { 
     } 
    } 

堆棧跟蹤

at System.Web.HttpResponse.set_ContentType(String value) 
    at System.Web.HttpResponseInternalWrapper.set_ContentType(String value) 
    at System.Web.UI.PageRequestManager.RenderPageCallback(HtmlTextWriter writer, Control pageControl) 
    at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) 
    at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) 
    at System.Web.UI.Page.Render(HtmlTextWriter writer) 
    at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) 
    at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) 
    at System.Web.UI.Control.RenderControl(HtmlTextWriter writer) 
    at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) 

我我無法修復它。有人可以幫忙嗎?

+0

你想設置的內容在設置標題後鍵入一行...嘗試切換,也許? – TZHX

+2

發生異常的線是什麼? –

+0

@TZHX我試圖切換,但仍然是相同的結果 – Richa

回答

0

下面的代碼塊實際上是設置Content-Encoding HTTP標頭

if (CultureInfo.CurrentCulture.ThreeLetterISOLanguageName.ToUpper(CultureInfo.CurrentCulture) == "CES" || CultureInfo.CurrentCulture.ThreeLetterISOLanguageName.ToUpper(CultureInfo.CurrentCulture) == "POL") 
    HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1250"); 
else 
    HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1252"); 

因此,你寫的任何內容響應流之前,它必須被調用,即HttpContext.Current.Response.Write(@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">");

相關問題