tags:

views:

78

answers:

1

As per question how to i create a a file that also includes the Byte Order Mark?

Having some trouble displaying some chars because the csv viewer cannot display the chars correctly without the BOM.

Appreciate any help.

EDIT To Include code:

string attachment = string.Format("attachment; filename=results_{0}_to _{1}.csv", startDate.ToString("MM_yy_yyyy"), endDate.ToString("MM_yy_yyyy"));
HttpContext.Current.Response.AddHeader("content-disposition", attachment);
HttpContext.Current.Response.ContentType = "text/csv";
HttpContext.Current.Response.AddHeader("Pragma", "public");

string content = GetTextFromDB();

HttpContext.Current.Response.Write(content);
HttpContext.Current.Response.End();
+3  A: 

This should be done automatically for any of the inbuilt code using an Encoding, such as StreamWriter - just make sure that the encoder you pass in is configured to include a bom (the default Encoding.UTF8 includes the bom).

If you are handling your own Encoding -> Stream logic, then you need to call .GetPreamble() and write those bytes at the start of the stream.

The default Encoding.UTF8 includes the preamble; but you can do it explicitly in the ctor: new UTF8Encoding(true)

Marc Gravell
.GetPreamble() did the trick :).
GMan