PHP Export Excel Chinese garbled solution Summary _php Tutorial

Source: Internet
Author: User
My page is encoded with UTF-8, what to do to ensure that the exported Excel will not be garbled? and the different operating systems above due to the system code is different, so be sure to provide a different encoding format of the file to download it?

1. Define file name

2. Populate Excel data

These two processes may appear some PHP export Excel garbled problem, below I say the solution:


Troubleshoot PHP export of Excel data in Excel garbled:

PHP Export Excel garbled reason: page encoding and Excel encoding inconsistent.

WORKAROUND: Since the coding is inconsistent, it is OK to make it consistent. Define the character set for Excel:

The code is as follows Copy Code
Header ("Content-type:application/vnd.ms-excel; Charset=utf-8″)

, see Charset=utf-8, let it and your page encoding consistent can solve the problem of Excel data garbled, this is relatively simple!

Attach a PHP export Excel class

The code is as follows Copy Code

/*
* Created on 2012-2-26
*
* To change the template for this generated file go to
* Window-preferences-phpeclipse-php-code Templates
*/
Header ("Content-type:application/vnd.ms-excel");
Header ("Content-disposition:filename=php100.xls");
?>











Class infomation

Summarize

Need to convert to GBK before exporting

The code is as follows Copy Code

Iconv ("UTF-8", "GBK", $value);

Other methods

Finally, I adopt the phpmyadmin approach. With Htmlexcel, HTML we are more familiar with the format as follows.

The code is as follows Copy Code

xmlns:x= "Urn:schemas-microsoft-com:office:excel"
xmlns= "HTTP://WWW.W3.ORG/TR/REC-HTML40" >











1234 Robbin will spit.
5678 Javaeye website



This can be directly echo, and do not need iconv transcoding, as long as the set of HTML Content-type (here is UTF-8), is not comfortable feeling it? Of course, the header has to be added

PHP code

The code is as follows Copy Code
Header ("Content-type:application/vnd.ms-excel");
Header ("Content-disposition:attachment;filename=export_data.xls");

If the export of Chinese names may also be garbled

Resolve file name of PHP export Excel garbled:

Garbled reason: The customer uses the Chinese version of the Windows System platform, and the Windows platform file name encoding gb2312 (GBK), and our web page encoding in order to follow the current trend is generally used utf-8 (internationalization) coding, when we:

The code is as follows Copy Code
Header ("Content-disposition:inline; Filename= "". $filename. ". xls" ")

will appear garbled, if your Web page encoding is gb2312 that will not have to consider the coding problem.

Workaround:

For $filename transcoding, perform:

The code is as follows Copy Code
Iconv (' Utf-8″, ' Gb2312″, $filename)

。 If your environment does not support the ICONV function can be replaced by other functions, as long as the $filename encoding can be converted to GBK on the line.
But this problem will come again, Linux users will appear garbled file name (because the Linux platform file name is not GBK encoding).

In view of this problem I use two ways: first: Give up some customers, after all, Windows system users accounted for the majority. Second: Like Gmail, two download addresses are available. A filename GBK encoded, a filename utf-8 encoded.

http://www.bkjia.com/PHPjc/632176.html www.bkjia.com true http://www.bkjia.com/PHPjc/632176.html techarticle My page is encoded with UTF-8, what to do to ensure that the exported Excel will not be garbled? and different operating systems above due to the system code is different, so be sure to provide a different ...

  • 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.