Handling special characters when exporting DataGrid to Excel

  • Thread starter Elisabeth Svensson via .NET 247
  • Start date
E

Elisabeth Svensson via .NET 247

I am trying to export a DataGrid to Excel using a HtmlTextWriter and the DataGrid.RenderControl method. Everything is working fine except that certain swedish characters in the DataGrid (?, ?, ?) gets displayed as ?? ?? ?? in the Excel file.

Can anyone tell me how to solve this?

This is the code I am using:
//create a string writer
System.IO.StringWriter stringWrite = new StringWriter();

//create an htmltextwriter which uses the stringwriter
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

//first let's clean up the response.object
Response.Clear();
Response.Charset = "";

//set the response mime type for excel
Response.ContentType = "application/vnd.ms-excel";

//Tell the datagrid to render itself to our htmltextwriter
dgIssues.RenderControl(htmlWrite);

//all that's left is to output the html
Response.Write(stringWrite.ToString());
Response.End();
 
P

Pete Fearn

Elisabeth said:
I am trying to export a DataGrid to Excel using a HtmlTextWriter and
the DataGrid.RenderControl method. Everything is working fine except
that certain swedish characters in the DataGrid (?, ?, ?) gets
displayed as ?? ?? ?? in the Excel file.
Can anyone tell me how to solve this?
I am trying to export a DataGrid to Excel using a HtmlTextWriter and
the DataGrid.RenderControl method. Everything is working fine except
that certain swedish characters in the DataGrid (?, ?, ?) gets
displayed as ?? ?? ?? in the Excel file.
Can anyone tell me how to solve this?
<snip code>

Yes,

Finally figured this one out myself too! Add the following line before
rendering the control

Response.ContentEncoding = Encoding.Default;

I know this may seem like it would do nothing, but it seems to have
solved my strange character problems (well, the excel export related
ones anyhoo :p )

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top