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

    --------------------------------
    From: Elisabeth Svensson

    -----------------------
    Posted by a user from .NET 247 (http://www.dotnet247.com/)

    <Id>P8BOk1tkZkGIWIV23paKCg==</Id>
    Elisabeth Svensson via .NET 247, Aug 4, 2004
    #1
    1. Advertising

  2. Elisabeth Svensson via .NET 247

    Pete Fearn Guest

    Elisabeth Svensson via .NET 247 wrote:
    > 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?
    >


    Elisabeth Svensson via .NET 247 wrote:
    > 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
    Pete Fearn, Dec 21, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Goran Djuranovic
    Replies:
    0
    Views:
    357
    Goran Djuranovic
    Dec 10, 2004
  2. Luis Esteban Valencia
    Replies:
    1
    Views:
    1,380
    Carl Prothman [MVP]
    Jan 12, 2005
  3. Replies:
    2
    Views:
    351
  4. ChrisN
    Replies:
    0
    Views:
    842
    ChrisN
    Sep 19, 2006
  5. Exporting to Excel with Chinese Characters

    , Oct 15, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    294
Loading...

Share This Page