2015-04-06 155 views
0

我想從DataTable使用Response.Write將數據導出到excel文件。 但是,一些數據從excel文件中丟失。可以幫我解決這個問題。 我已經嘗試使用StringBuilder創建html並且循環沒有錯誤,可以獲取所有數據,只有當導出爲ex​​cel時纔會丟失。請幫我解決一下這個。Response.Write丟失元素

  Response.Buffer = true; 
      Response.ClearContent(); 
      Response.Write(HEADER); 
      Response.Write(STYLE); 
      Response.Write("</head><body><table border=\"0\" style=\"font-size:11pt\"><tr>"); 
      Response.AddHeader("content-disposition", attachment); 
      Response.Charset = ""; 
      Response.ContentType = "application/vnd.ms-excel"; 

      Response.Write(strTitle); 
      Response.Write("</tr>"); 

      Response.Write("<tr><td colspan=3>From Date: " + lblFromDate.Text + " To: " + lblToDate.Text + "</td></tr>"); 
      Response.Write("<tr><td colspan=9>Protocol: " + lblProtocol.Text + "</td></tr>"); 
      Response.Write("<tr><td colspan=9>Production Line: " + lblPL.Text + "</td></tr>"); 
      Response.Write("<tr></tr></table>"); 

      Response.Write("<table border=\"1\" style=\"font-size:11pt\">"); 
      Response.Write("<tr>"); 
      for (int i = 0; i < summaryreporttable.Columns.Count - 2; i++) 
      { 
       Response.Write(" <th>" + summaryreporttable.Columns[i].ToString() + "</th>"); 
      } 

      Response.Write("</tr>"); 

      for (int r = 0; r < summaryreporttable.Rows.Count; r++) 
      { 
       Response.Write("<tr>"); 
       Response.Write("<td>"+r+"</td>"); 
       for (int i = 0; i < summaryreporttable.Columns.Count - 2; i++) 
       { 
        if (summaryreporttable.Columns[i].ToString() == "Issue Date") 
        { 

         //Response.Write("<td class=\"x167\" valign=\"top\">" + summaryreporttable.Rows[r][i] + "</td>"); 
         Response.Write(String.Format("<td class=\"x167\" valign=\"top\"> {0} </td>", summaryreporttable.Rows[r][i])); 
        } 
        else 
        { 
         //Response.Write("<td class=\"x169\" valign=\"top\">" + summaryreporttable.Rows[r][i] + "</td>"); 
         Response.Write(String.Format("<td class=\"x169\" valign=\"top\"> {0} </td>", summaryreporttable.Rows[r][i])); 
        } 

       } 

       Response.Write("</tr>"); 
       Response.Flush(); 
      } 

      Response.Write("</table></body></html>"); 

     } 
     catch (Exception ex) 
     { 
     } 
     finally 
     { 
      Response.End(); 
     } 

正如您所看到的,兩者之間的數據缺失。 enter image description here

回答

0

我想解釋我們的方式。這是有用的,然後你的。

1,我們在網頁中文字這樣

<asp:Literal ID="lblData" runat="server" /> 

2,在代碼中我們有一些字符串變量。而且我們像HTML一樣在屏幕上繪製html。

// our html datas 
// you can use foreach for repating 
sTEXT = @"<table class='table'><thead>"; 
sTEXT += @"<th>"; 
sTEXT += @"Caption A"; 
sTEXT += @"</th>"; 
sTEXT += @"</thead>"; 
sTEXT += @"<tr>"; 
sTEXT += @"<td>abcdef</td>"; 
sTEXT += @"<td>ghjklm</td>"; 
sTEXT += @"</tr>"; 
sTEXT += @"</table>"; 

// our screen literaldata sets 
lblData.Text = sTEXT; 

// excel output code 
// carefull end of the "Responce.Write"" sentence 
Response.Clear(); 
Response.ClearHeaders(); 
Response.ClearContent(); 
Response.Cache.SetCacheability(HttpCacheability.NoCache); 
Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1254"); 
Response.Charset = "windows-1254"; 
Response.AppendHeader("content-disposition", "attachment;filename=ExportedHtml.xls"); 
Response.Buffer = true; 
this.EnableViewState = false; 
Response.ContentType = "application/vnd.ms-excel"; 
Response.Write("<style> TABLE { border: 1px solid gray; } TD { border: 1px solid gray; } </style> " + lblData.Text); 
Response.End();