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();
 
Ad

Advertisements

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

Top