如何在导出到Excel时更改gridview上的字符?

时间:2014-12-30 12:40:22

标签: c# asp.net excel gridview replace

我想导出Excel。我可以用下面的代码导出它,但我想看excel上的土耳其字符或将土耳其字符改为英文字符。

我正在尝试2循环,但它不成功。帮助我改变角色。

for (int i = 0; i < GridView1.Columns.Count; i++)
{
    for (int j = 0; j < GridView1.Rows.Count; j++)
    {
        GridView1.Rows[j].Cells[i].Text.Replace("ş", "s");
        GridView1.Rows[j].Cells[i].Text.Replace("ı", "i");
        GridView1.Rows[j].Cells[i].Text.Replace("İ", "ı");
        GridView1.Rows[j].Cells[i].Text.Replace("ğ", "g");
    }
}

Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", 
                   string.Format("attachment; filename={0}", "Görüşmeler.xls"));
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);

//Change the Header Row back to white color
GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF");
//Applying stlye to gridview header cells
for (int i = 0; i < GridView1.HeaderRow.Cells.Count; i++)
{
    GridView1.HeaderRow.Cells[i].Style.Add("background-color", "#CCCCFF");
}

GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();

1 个答案:

答案 0 :(得分:0)

您可以将Replace方法棒中的值指定给单元格文本:

GridView1.Rows[j].Cells[i].Text = GridView1.Rows[j].Cells[i].Text.Replace("ş", "s");