Small Excel export experience based on PHP perfectly solves the problem of garbled text _ PHP Tutorial

Source: Internet
Author: User
Based on the small experience of exporting Excel in PHP, the garbled text problem is perfectly solved. In my PHP project, I want to export data to Excel and contain Chinese characters. I have a rough idea about PHPExcel on the Internet, but this framework is too complicated for me. so I still want to export the data to Excel in the PHP project, and the data contains Chinese characters.
I 'd like to know about PHPExcel on the Internet, but this framework is too complicated for my needs. so I still want to find a simple method.
It is actually the easiest way to write data on the Internet, but the problem is that the Chinese encoding is not reliable ..

The code is as follows:


Header ("Content-type: application/vnd. ms-excel ");
Header ("Content-Disposition: attachment?filename=export_data.xls ");
Echo "name". "\ t ";
Echo "traditional". "\ t ";
Echo "blog". "\ t ";
Echo "\ n ";
Echo "jason". "\ t ";
Echo "@". "\ t ";
Echo "javaeye". "\ t ";
?>


Some students think of adding headers to character sets.

The code is as follows:


Header ("Content-type: application/vnd. ms-excel; charsets = UTF-8 ");


Question: I only want to tell the browser which character set to choose to view. In the end, I still need to generate the xls file.
Of course. some people will think of iconv transcoding.

The code is as follows:


Echo iconv ("current code", "GB18030", "This blog is from javaeye, by jason ");


Q: In this case, the Chinese character encoding in the file is GB18030. but what encoding does Excel know to use to open the file? You can only rely entirely on the default OS. However, if you encounter BIG5 in traditional Chinese style, it will still be garbled, so it is still unreliable.
Finally, I use phpMyAdmin. we are familiar with using HTMLExcel and HTML. the format is as follows.

The code is as follows:


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 directly echo, without iconv transcoding, as long as you set the Content-type in HTML (here is the use of UTF-8), is there a comfortable feeling? Of course, the header must be added.

The code is as follows:


Header ("Content-type: application/vnd. ms-excel ");
Header ("Content-Disposition: attachment?filename=export_data.xls ");


I would like to share with you a little bit of experience ..

Sorry. I 'd like to know about PHPExcel on the Internet, but this framework is too complicated to meet my needs...

Related Article

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.