Click here to Skip to main content
15,887,746 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 30-May-12 6:58am
v2
Comments
TRK3 31-May-12 12:55pm    
When you say "don't export finely" -- what exactly do you mean? Do you mean that it crashes or throws an exception? Or do you mean that the exported data isn't displaying correctly in excel?

If the later, then it's probably a character set / code-page mismatch of some kind. Could depend on what language version of Windows is being run and what version of excel.

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