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"); ?>
|
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 ...