Response.Write缺少元素

时间:2015-04-06 08:38:07

标签: c# asp.net .net visual-studio-2010 webforms

我希望使用Response.Write将DataTable中的导出数据导入excel文件。 但是excel文件中缺少一些数据。可以帮我解决这个问题。 我已经尝试使用StringBuilder创建html并且循环没有任何问题,可以获取所有数据,只有在导出到excel时它才会丢失。请帮帮我。

            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

1 个答案:

答案 0 :(得分:0)

我想解释一下我们的方式。这对你来说很有用。

1 - 我们在网页上有一个像这样的文字

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

2 - 在代码中我们有一些字符串变量。我们用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();