Handling special characters when exporting DataGrid to Excel

Discussion in 'ASP .Net Datagrid Control' started by Elisabeth Svensson via .NET 247, Aug 4, 2004.

  1. 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();
     
    Elisabeth Svensson via .NET 247, Aug 4, 2004
    #1
    1. Advertisements

  2. Elisabeth Svensson via .NET 247

    Pete Fearn Guest

    the DataGrid.RenderControl method. Everything is working fine except
    that certain swedish characters in the DataGrid (?, ?, ?) gets
    displayed as ?? ?? ?? in the Excel file.
    the DataGrid.RenderControl method. Everything is working fine except
    that certain swedish characters in the DataGrid (?, ?, ?) gets
    displayed as ?? ?? ?? in the Excel file.
    <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
     
    Pete Fearn, Dec 21, 2004
    #2
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.