Click here to Skip to main content
15,898,538 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I export my GrideView from asp.net to excel but the fields which contain Persian character don’t export finely (VS2010 – C#). HOW CAN I DO IT?
My code is :
C#
HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.AddHeader("content-disposition",
        string.Format("attachment;filename={0}.xls", "Exported"));
    HttpContext.Current.Response.Charset = "";
    HttpContext.Current.Response.ContentType = "application/ms-excel";

    using (StringWriter sw = new StringWriter())
    {
        using (HtmlTextWriter htw = new HtmlTextWriter(sw))
        {
            //  Create a table to contain the grid
            Table table = new Table();

            foreach (GridViewRow row in GridView1.Rows)
            {
                table.Rows.Add(row);
            }

            //  render the table into the htmlwriter
            table.RenderControl(htw);

            //  render the htmlwriter into the response
            HttpContext.Current.Response.Write(sw.ToString());
            HttpContext.Current.Response.End();
        }
    }

Thanks very much
Posted
Updated 23-May-12 19:58pm
v2

1 solution

Try setting CodePage and Character Set of the Response object.

Details here:
MSDN: Response.CodePage[^]
MSDN: Character Set Recognition[^]

Sample:
VB
Response.CodePage = 65001
Response.CharSet = "utf-8"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900