如何使用阿拉伯字母将Gridview保存到Csv文件中?

时间:2013-12-11 08:39:46

标签: c# asp.net .net csv export-to-csv

我有一个gridView,数据包含阿拉伯字母在我的网站上当我导出到Csv文件时 在localhost它工作正常,当我在服务器上紫色时,阿拉伯字母看起来像???????? 哪里有问题  这是我的代码

  protected void btn_SaveCSV_Click(object sender, EventArgs e)
    {
       // Response.Clear();
        HttpContext.Current.Response.Clear();
        Response.Write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
        Response.Buffer = true;
        Response.AddHeader("content-disposition",
         "attachment;filename=GridViewExport.csv");
        Response.Charset = "";
        //Response.ContentType = "application/text";
        Response.ContentType =  "text/csv; charset-UTF-8";
        Response.ContentEncoding = System.Text.Encoding.Default;

        Grid_offlineMessages.AllowPaging = false;
        FillGrid();


        StringBuilder sb = new StringBuilder();
        for (int k = 0; k < Grid_offlineMessages.Columns.Count; k++)
        {
            //add separator
            sb.Append(Grid_offlineMessages.Columns[k].HeaderText + ',');
        }
        //append new line
        sb.Append("\r\n");
        for (int i = 0; i < Grid_offlineMessages.Rows.Count; i++)
        {
            for (int k = 0; k < Grid_offlineMessages.Columns.Count; k++)
            {
                //add separator
                sb.Append(Grid_offlineMessages.Rows[i].Cells[k].Text + ',');
            }
            //append new line
            sb.Append("\r\n");
        }
        Response.Output.Write(sb.ToString());
        Response.Flush();
        Response.End();
    }

1 个答案:

答案 0 :(得分:1)

尝试将编码设置为UTF-8,如下所示:

Response.ContentEncoding = System.Text.Encoding.UTF8;