Home >Backend Development >PHP Tutorial >Summary of solutions to Chinese garbled Chinese characters exported by PHP_PHP Tutorial
My page is encoded in UTF-8. What should I do to ensure that the exported excel will not be garbled? And because different operating systems have different system encodings, do they have to provide files in different encoding formats for download?
1. Define file name
2. Fill in Excel data
There may be some garbled problems when exporting Excel from PHP during these two processes. Let me talk about the solution below:
Solve the garbled Excel exported by PHP of data in Excel:
The reason why PHP exports Excel with garbled characters: The web page encoding is inconsistent with the Excel encoding.
Solution: Since the encoding is inconsistent, it will be OK to make it consistent. Define Excel’s character set:
代码如下 | 复制代码 |
header(“Content-Type: application/vnd.ms-excel; charset=UTF-8″) |
, you see charset=UTF-8, make it consistent with your web page encoding to solve the problem of garbled data in Excel, this is relatively simple!
Attached is a php export Excel class
The code is as follows | Copy code | ||||||||||||||||||||||||
* * To change the template for this generated file go to
|
The code is as follows | Copy code |
iconv("UTF-8", "GBK", $value); |
Other methods
Finally, I adopted the method of phpMyAdmin. I used HTMLExcel. We are familiar with HTML and the format is as follows.
The code is as follows | Copy code | ||||||||||||||||||||
|
The code is as follows | Copy code | ||||
header("Content-type:application/vnd .ms-excel");
|
. If your environment does not support the iconv function, you can use another function, as long as the encoding of $filename can be converted to gbk.
But this problem will arise again, and Linux users will have garbled file names (because the Linux platform file names are not gbk encoded).
Considering this problem, I adopted two methods: First: give up some customers, after all, Windows system users account for the majority. Second: Like gmail, two download addresses are provided. One file name is gbk encoded, and one file name is utf-8 encoded.