Download CSV, MS Excel open garbled solution [Reproduced]

Source: Internet
Author: User
Download CSV, MS Excel open garbled Solution

MS Office EXCEL opens CSV files by default in ANSI, unicode format (for the old version of this, 2007 seems to have been able to identify the UTF-8 Based on BOM, but 2003 does not work)

The following describes how to use CSV compatibility for different office versions of Excel.
The idea is: Convert your code into a UTF-16LE, add a unicode BOM Header "/xFF/xfe", and finally download, so the downloaded CSV is the unicode format
PHP program example:

$ Str4file = iconv ('utf-8', 'utf-16le', $ content );
// Transcode Small Tail Order (UTF-16) into http://zh.wikipedia.org/wiki/UTF-16 ),
This is the Unicode storage text format used by Microsoft and Linux systems, the Apple system uses the large tail order storage text format, that requires the use of UTF-16BE,
While the BOM header uses Fe FF
$ Str4download = gzencode ("/xFF/xfe". $ str4file); // Add the BOM header to compress the content
Header ('content-type: text/CSV; charset = UTF-16LE ');
Header ("Content-Length:". strlen ($ str4download ));
Header ("content-Disposition: attachment; filename =/" $ filename /"");
Echo $ str4download;

Note:
Unicode-encoded. CSV files may not support ",", and must be separated by "/t" spaces.

This is another article about solving CSV Garbled text, see: http://wangblog.org/2007/12/utf-8unicode.html

Introduction: Relationship between UTF-8, UTF-16 and Unicode
The so-called "UTF-8" "UTF-16" is only a form of Unicode, only a Unicode Character Set.
For more details, refer to Wiki

Http://blog.fengruzhuo.com /? P = 295

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.