使用C#将Gridview导出到CSV文件

时间:2016-12-21 15:11:41

标签: c# asp.net csv gridview

我正在尝试将gridview数据导出到csv文件中但是当我导出文件时,我总是在第二列中得到这个

<span style = 'background-color:#00ff00'></span>P<span style = 'background-color:#00ff00'></span>l<span style = 'background-color:#00ff00'></span>a<span style = 'background-color:#00ff00'></span>t<span style = 'background-color:#00ff00'></span>e<span style = 'background-color:#00ff00'></span>l<span style = 'background-color:#00ff00'></span>e<span style = 'background-color:#00ff00'></span>t<span style = 'background-color:#00ff00'></span> <span style = 'background-color:#00ff00'></span>F<span style = 'background-color:#00ff00'></span>u<span style = 'background-color:#00ff00'></span>n<span style = 'background-color:#00ff00'></span>c<span style = 'background-color:#00ff00'></span>t<span style = 'background-color:#00ff00'></span>i<span style = 'background-color:#00ff00'></span>o<span style = 'background-color:#00ff00'></span>n<span style = 'background-color:#00ff00'></span> <span style = 'background-color:#00ff00'></span>A<span style = 'background-color:#00ff00'></span>n<span style = 'background-color:#00ff00'></span>a<span style = 'background-color:#00ff00'></span>l<span style = 'background-color:#00ff00'></span>y<span style = 'background-color:#00ff00'></span>z<span style = 'background-color:#00ff00'></span>e<span style = 'background-color:#00ff00'></span>r<span style = 'background-color:#00ff00'></span>s<span style = 'background-color:#00ff00'></span>

其余列都很好,但问题始终在第二列。 这是我的代码:

protected void btnExportCSV_Click(object sender, EventArgs e)
{
    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", "attachment;filename=gvtocsv.csv");
    Response.Charset = "";
    Response.ContentType = "application/text";
    StringBuilder sBuilder = new System.Text.StringBuilder();
    for (int index = 0; index < gvSearch.Columns.Count; index++)
    {
        sBuilder.Append(gvSearch.Columns[index].HeaderText + ',');
    }
    sBuilder.Append("\r\n");
    for (int i = 0; i < gvSearch.Rows.Count; i++)
    {
        for (int k = 0; k < gvSearch.HeaderRow.Cells.Count; k++)
        {
            sBuilder.Append(gvSearch.Rows[i].Cells[k].Text.Replace(",", "") + ",");
        }
        sBuilder.Append("\r\n");
    }
    Response.Output.Write(sBuilder.ToString());
    Response.Flush();
    Response.End();
}

1 个答案:

答案 0 :(得分:0)

你对该列有一些格式吗?

<span style = 'background-color:#00ff00'/>
P<span style = 'background-color:#00ff00'/>
l<span style = 'background-color:#00ff00'/>
a<span style = 'background-color:#00ff00'/>
t<span style = 'background-color:#00ff00'/>
e<span style = 'background-color:#00ff00'/>
l<span style = 'background-color:#00ff00'/>
e<span style = 'background-color:#00ff00'/>
t<span style = 'background-color:#00ff00'/>
 <span style = 'background-color:#00ff00'/>
F<span style = 'background-color:#00ff00'/>
u<span style = 'background-color:#00ff00'/>
n<span style = 'background-color:#00ff00'/>
c<span style = 'background-color:#00ff00'/>
t<span style = 'background-color:#00ff00'/>
i<span style = 'background-color:#00ff00'/>
o<span style = 'background-color:#00ff00'/>
n<span style = 'background-color:#00ff00'/>
 <span style = 'background-color:#00ff00'/>
A<span style = 'background-color:#00ff00'/>
n<span style = 'background-color:#00ff00'/>
a<span style = 'background-color:#00ff00'/>
l<span style = 'background-color:#00ff00'/>
y<span style = 'background-color:#00ff00'/>
z<span style = 'background-color:#00ff00'/>
e<span style = 'background-color:#00ff00'/>
r<span style = 'background-color:#00ff00'/>
s<span style = 'background-color:#00ff00'/>

我看到血小板功能分析仪