The CSV file exported by ASP. NET is garbled.

Source: Internet
Author: User

Bom is the standard mark used for identification coding in the UTF Encoding scheme. It is FF Fe in the UTF-16 and ef bb bf in the UTF-8. This tag is optional because the UTF-8 has no order, so it can be used to detect whether a byte stream is UTF-8 encoded. Microsoft does this kind of detection, but some software does not do this kind of detection, and treats it as a normal character.
Microsoft added ef bb bf three bytes before its own text file in UTF-8 format, windows notepad and other Program is based on the three bytes to determine whether a text file is ascii or UTF-8, however, this is only a mark by Microsoft, other platforms do not make such a mark on the UTF-8 text files, Unix-like systems do not use Bom, because it will break the syntax conventions of the existing ASCII files. That is to say, a UTF-8 file may have Bom, there may be no Bom, so how to distinguish?
three methods:
1. open the file with a UltraEdit-32, switch to the hexadecimal editing mode, check whether the file header has ef bb bf;
2. use Dreamweaver to view the page attributes and check whether there is a check mark before "including Unicode signature Bom".
3. Open it in a Windows notepad, select Save As to see whether the file's default encoding is UTF-8 or ANSI, and if it is ANSI, it does not contain Bom.
the so-called Unicode file is actually a UTF-16, but it is exactly the same as the Unicode code, but in terms of concept, Unicode and UTF are two different, Unicode is a memory encoding representation scheme, UTF saves and transfers Unicode. The UTF-16 is also divided into two types: High Front (LE) and high behind (be. The official UTF Code also includes utf-32, which can be Le and be. Non-Unicode official UTF Encoding also has utf-7, mainly used for mail Transmission. The single-byte part of UTF-8 is compatible with the iso-8859-1, which is primarily intended to address the problem that some old systems and library functions cannot properly handle the UTF-16, and for English characters, it also saves storage space (at the cost of non-English characters wasting space ). In the iso-8859-1, both utf8 and iso-8859-1 are represented in one byte, and when it represents other characters, UTF-8 uses two or three bytes.

ExportCode:

         Public   Static   Void Exporttocsv (datatable DT, String  Filename) {stringbuilder sb = New  Stringbuilder ();  Int I = 0  ;  For (I = 0 ; I <= DT. Columns. Count- 1 ; I ++ ){  If (I> 0 ) {Sb. append ( "  ,  "  );} Sb. append (Dt. Columns [I]. columnname);} sb. append (  "  \ N  "  );  Foreach (Datarow Dr In DT. Rows ){  For (I = 0 ; I <= DT. Columns. Count- 1 ; I ++ ){  If (I> 0 ) {Sb. append ( "  ,  "  );} Sb. append (Dr [I]. tostring ();} sb. append (  "  \ N "  );}  Byte [] Buffer = System. Text. encoding. utf8.getbytes (sb. tostring ());  Byte [] Outbuffer = New   Byte [Buffer. Length + 3  ]; Outbuffer [  0 ] = ( Byte ) 0xef  ; Outbuffer [ 1 ] = ( Byte ) 0xbb  ; Outbuffer [  2 ] = ( Byte ) 0xbf  ; Array. Copy (buffer,  0 , Outbuffer, 3  , Buffer. Length); httpresponse rs = System. Web. httpcontext. Current. response; RS. contentencoding =System. Text. encoding. utf8; RS. appendheader (  "  Content-Disposition  " , "  Attachment; filename =  " + Filename); RS. contenttype = "  Application/MS-Excel  "  ; RS. Write (encoding. utf8.getstring (outbuffer); RS. Flush (); RS. End ();} 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.