Most of the cases you open the blog for a fast review to get an answer and if you did not find it within seconds, you will leave :)
So here it is
Response.Clear();
string fileName= String.Format("Client_{0}.csv", DateTime.Now.ToString("yyyyMMdd_HHmmss"));
Response.ContentType = "text/csv";
Response.AddHeader("content-disposition", "filename=" + fileName);
Response.Write("Ahmed,Mohamed,Adel\n");
Response.End();
EDIT : 30/05/2010
CSV file has a problem with UTF-8 encoding.
To solve this problem, you have to know first what is BOM - If you do not care you can move down to the end of the post and get the solution directly but for those who want to learn more, you can continue reading-. BOM is Byte Order Mask which is a Unicode character used to signal the byte order of a text file or stream. Its code is U+EEFF. BOM use is optional and if used should appear at the start of the text stream. Beyond its specific use as a byte-order indicator, the BOM character may also indicate which of the several Unicode representations the text is encoded in.
The UTF-8 representation of the BOM is the byte sequence 0xEF, 0xBB, 0xBF.
So the code I wrote is :
Response.Clear();
string fileName = String.Format("Requests_{0}.csv",DateTime.Now.ToString("yyyyMMdd"));
Response.ContentType = "application/csv";
Response.AddHeader("Content-Type", "text/csv");
Response.AddHeader("content-disposition", "filename=" + fileName);
Response.Charset = Encoding.UTF8.WebName;
Response.HeaderEncoding = Encoding.UTF8;
Response.ContentEncoding = Encoding.UTF8;
byte[] bytes = new UTF8Encoding().GetBytes(requestsBuilder.ToString());
List bytesList = new List();
bytesList.Add((Byte)239);
bytesList.Add((Byte)187);
bytesList.Add((Byte)191);
bytesList.AddRange(bytes);
Response.BinaryWrite(bytesList.ToArray());
Response.Flush();
Response.End();
It works more than perfect :)
Hope you enjoy it
No comments:
Post a Comment