PHP Export Excel garbled problem solved

Source: Internet
Author: User
PHP export Excel A little bit of experience to solve garbled problems.

The PHP project requires that the data be exported to Excel, and that the data contains Chinese.

I probably know about it on the internet but using Phpexcel, but the framework is too complex for my needs. So I still want to find a simple way.

Online discovery is actually the simplest to write, but the problem is that the Chinese encoding is unreliable.

PHP code

 
    

Some students think of the header to join the character set

PHP code

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

question: This is just to tell the browser what character set to choose to view, and ultimately I need to generate XLS files.

Of course, some students will also think of using Iconv transcoding.

PHP code

Echo iconv ("Current Code", "GB18030", "This blog is from Javaeye,by Jason");

question: so the Chinese character coding in the file is GB18030, but how does Excel know what code to open it? You can only rely entirely on the OS default. But if the traditional BIG5 to do so, it will be garbled. So it's still not reliable.

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

HTML code

                    
 
                                                
 
  
  
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

Header ("Content-type:application/vnd.ms-excel");  Header ("Content-disposition:attachment;filename=export_data.xls");  
  • 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.