将数据导出到制表符分隔文件的循环添加了额外的列

问题描述

我有一个程序可以将 gridview 的内容导出到制表符分隔的文本文件。它工作正常,但生成文件一个额外的列。我确定问题一定是我如何构建我的行(在最后一个条目中添加一个额外的选项卡),但我不知道该怎么做。任何帮助表示赞赏。

我要导出的代码

public void ExportGrid(GridView gv,string gvName)
{

//  Build the text file data
string txt = string.Empty;

txt += "<table id='tblCTSWContracts' border='1'>";

//  Add the header row for text file
txt += "<tr>";
foreach (TableCell cell in gv.HeaderRow.Cells)
{
    txt += "<th>";        
    txt += cell.Text + "\t";
    txt += "</th>";
}
txt += "</tr>";

//  Add header line
txt += "\r\n";

foreach (GridViewRow row in gv.Rows)
{
    //  Add the Data rows            
    txt += "<tr>";
    foreach (TableCell cell in row.Cells)
    {
        txt += "<td>";
        txt += cell.Text + "\t";
        txt += "</td>";
    }
    txt += "</tr>";
    //  Add new line    
    txt += "\r\n";    
}

txt += "<table>";

//  Export the file
Response.Clear();
Response.Buffer = true;
response.addheader("content-disposition","attachment;filename=" + gvName + ".html");
Response.Charset = "";
Response.ContentType = "application/text";
Response.Output.Write(txt);
Response.Flush();
Response.End();

}

解决方法

我是暴力破解的。跟踪列并为除最后一行之外的所有行添加“/t”。不优雅,但如果它有效,它就有效。