42

I'm doing some Excel Exports on the ASP.NET Site. Everything works except of the Encoding. When I open it in Excel, it looks like this:

Eingabe Kosten je Gerät Gerät: Gerätebezeichnung: Betriebsmittel Heizöl in €: 4 Dieselverbrauch in €: 4

This is my code:

Response.Clear();
Response.ContentType = "application/ms-excel";
Response.AddHeader("Content-Disposition", "inline;filename=NachkalkGeraete.xls;");
var writer = new HtmlTextWriter(Response.Output);

SomeControl.RenderControl(writer); /* FormView, Table, DataGrid... */

Response.End();

I've already tried explicitly set the Encoding.. but no change occured:

Response.Clear();
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment; filename=NachkalkGeraete.xls");

Response.BufferOutput = true;
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.Charset = "UTF-8";
EnableViewState = false;

System.IO.StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);

SomeControl.RenderControl(hw);

Response.Write(tw.ToString());
Response.End();

What is wrong, please?

6 Answers 6

134

Well I found out that the problem could be in the header of the excel file, that it does not contain the BOM byte sequence (at the beginning of the file representing the encoding used).

So I made it this way and it works for me:

Response.Clear();
Response.AddHeader("content-disposition","attachment;filename=Test.xls");   
Response.ContentType = "application/ms-excel";
Response.ContentEncoding = System.Text.Encoding.Unicode;
Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());

System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(sw);

FormView1.RenderControl(hw);

Response.Write(sw.ToString());
Response.End(); 
3
  • As a minimalist I tried eliminating lines 4 (ContentEncoding) & 5 (BinaryWrite) and was surprised to find it didn't work. Excellent job on finding this solution. Respect! :)
    – Chiramisu
    Aug 9, 2012 at 22:38
  • I spent a few hours trying to get this to work and finally gave up and concatenated " " in front of my strings. A total hack. Thanks so much for figuring this out.
    – user441058
    May 29, 2015 at 19:32
  • My last comment doesn't sound right. I was trying to figure out a solution on my own and wasn't able to get it fixed until I came across your answer. :-)
    – user441058
    May 29, 2015 at 19:40
12

Have you tried setting the encoding in a meta tag in the HTML?

<meta http-equiv="content-type" content="application/xhtml+xml; charset=UTF-8" />

Excel won't see the response headers, so it won't know what the Response.Encoding is. The meta tag allows it to find out.

4
  • I'm exporting a Control that is rendered which means that the file exported is html with a table only. no body, no headers. nothing..
    – theSpyCry
    Nov 5, 2009 at 18:42
  • Adding at least <html><head><meta... /></head><body>...</body> is probably worth while then. Nov 6, 2009 at 9:17
  • Yeah but i think that's the same solution as to add a byte header with encoding.. anyways it's a file to be opened in office.. what do you think ?
    – theSpyCry
    Nov 6, 2009 at 11:29
  • YOU SAVE MY JOB! THANKS, THIS WORK PERFECTLY WITH WebApi (Asp.net WebApi return a IHttpAction result). Very thanks!!!!!!!
    – luke cross
    Aug 4, 2022 at 14:33
11

For instances where UTF8 is needed...

FileInfo dataExportFile = new FileInfo(dsExport.Tables[0].Rows[0]["DataExportFile"].ToString());

Response.Clear();
Response.ContentType = "application/ms-excel";                        
Response.AddHeader("Content-Disposition", "attachment;filename=" + dataExportFile.Name);
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.BinaryWrite(System.Text.Encoding.UTF8.GetPreamble());
Response.TransmitFile(dataExportFile.FullName);
4

I got same problem with spanish characters and solved it with this line of code.

        response.ContentEncoding = System.Text.Encoding.Default ;

hope this helps

2

add Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());

1
  • You should add a bit more than just code, such as where to add your one liner.
    – EdChum
    Dec 31, 2012 at 1:12
1

You can try to use "Server.HtmlDecode" to decode these words like "João"

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e){
String wrong = "Jo&#227;o";
String corrected = Server.HtmlDecode(wrong);}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.