Php export excel Chinese garbled solution summary

Source: Internet
Author: User
Tags export class php excel
My page is coded in UTF-8, how can I ensure that the exported excel will not contain garbled characters? In addition, because the system encoding is different in different operating systems, different codes must be provided.

My page is coded in UTF-8, how can I ensure that the exported excel will not contain garbled characters? In addition, is it necessary to provide files of different encoding formats for download because the system encoding is different in different operating systems?

1. define the file name2. fill in Excel data

Some PHP Excel export garbled characters may occur during these two processes. The following is a solution: Solve the PHP export Excel garbled data in Excel:

Reasons for PHP Excel export garbled characters: webpage encoding is inconsistent with Excel encoding.

Solution:Since the encoding is inconsistent, it is okay to make it consistent. define the Excel character set:

Header ("Content-Type: application/vnd. ms-excel; charset = UTF-8 ″)

See charset = UTF-8, let it and your web code consistent can solve the problem of data garbled in Excel, this is relatively simple! Comes with a php Excel export class

  1. /*
  2. * Created on 2012-2-26
  3. *
  4. * To change the template for this generated file go
  5. * Window-Preferences-PHPeclipse-PHP-Code Templates
  6. */
  7. Header ("Content-type: application/vnd. ms-excel ");
  8. Header ("Content-Disposition: filenameappsphp100.xls ");
  9. ?>
  10.  
  11. "Red" align = "center"> class infomation 
  12.  
  13.  
  14.  
  15.            
     

Summary:Convert to GBK: iconv ("UTF-8", "GBK", $ value );

Other methods:Finally, I use phpMyAdmin. we are familiar with using HTMLExcel and HTML. the format is as follows.

  1. Xmlns: x = "urn: schemas-microsoft-com: office: excel"
  2. Xmlns = "http://www.w3.org/TR/REC-html40">
  3.  
  4.  
  5.   
  6.   
  7.   
  8. "Classeurrent16681" align = center x: publishsource = "Excel">
  9. 1234 Robbin will spit 
  10. 5678 Javaeye website 
  11.  
  12.   
  13.  

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.

Header ("Content-type: application/vnd. ms-excel ");

Header ("Content-Disposition: attachment?filename=export_data.xls ");

If you export a Chinese name, garbled characters may also occur, resolving the file name's PHP export Excel garbled characters.

Cause of garbled characters: the Chinese version of the Windows system platform used by the customer, while the file name encoding of the Windows platform is gb2312 (gbk). in order to follow up the existing trend, we generally use UTF-8 (international) encoding, at this time, when we:

Header ("Content-Disposition: inline; filename =" ". $ filename. pai.xls "")

If your webpage code is gb2312, you do not need to consider encoding.

Solution:For $ filename transcoding, run: iconv ('utf-8', "gb2312", $ filename ). If your environment does not support iconv functions, you only need to convert $ filename encoding to gbk.

However, this problem may occur again, and linux users may encounter garbled file names (because the file names on the linux platform are not gbk encoded ).

To solve this problem, I adopt two methods: first, give up some customers. after all, windows users account for the majority. Second: like gmail, it provides two. A file name must be gbk encoded, and a file name must be UTF-8 encoded.

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.