Exporting html to excel 2003 and 2007 using utf-8

Using html in Excel is very usefull, because it allows to do easy reporting with nice features like colors, tabular formatting, etc.

First we need to output some headers so the browser will open the ouput with Excel. This works in both 2003 and 2007 :

(pseudo-code)
headers.Add(“Content-Type”, “application/vnd.excel”)
headers.Add(“Content-Disposition”, “attachment;filename=something.xls”)

The tricky thing was the output encoding. In utf-8, it worked in 2007, but not in 2003. In utf-7 it worked in 2003, but not in 2007. In unicode, it did not work at all.

After alot of tries, I finally discovered, that the output must be in utf-8, but we need to output the UTF-8 BOM wich identify the utf-8 byte order. Then it worked perfectly. You need to output this at the beginning of your data :

(pseudo-code)
OutputStream.WriteByte(0xEF);
OutputStream.WriteByte(0xBB);
OutputStream.WriteByte(0xBF);

2 Replies to “Exporting html to excel 2003 and 2007 using utf-8”

  1. also try this
    HttpContext.Current.Response.BinaryWrite(new Byte[] { 0xEF, 0xBB, 0xBF });

Leave a Reply