Home  >  Article  >  Backend Development  >  What to do if php exports excel with garbled characters

What to do if php exports excel with garbled characters

藏色散人
藏色散人Original
2020-11-24 09:43:334217browse

Solution to php exporting garbled excel files: 1. Set the PHP file encoding to UTF8 and specify the header encoding to UTF8; 2. Set the database encoding to UTF8; 3. Use table for layout.

What to do if php exports excel with garbled characters

Recommended: "PHP Video Tutorial"

The operating environment of this tutorial : Windows7 system, PHP5.6 version, this method is suitable for all brands of computers.

Solve the problem of garbled characters exported to Excel using PHP using CVS

After using PHP to generate a CVS file, open it through Excel and find Chinese Everything turned into garbled characters. It used to be normal on my local win08 through WPS, but when uploaded to the server Linux and tested on the server, garbled characters appeared.

It was a problem with Linux at first, but then I tested it. I found out that it was a WPS problem (guessing that WPS may have processed the encoding, and opening it through Excel on the server was also garbled)

Because my PHP file and the database The encodings are all in UTF-8 format and the encoding opened with a text editor is also UTF-8

PHP code

The exported CVS document has no garbled characters when opened using a text editor

When opened using Excel, there are no garbled characters

Many people on the Internet say to convert the data in the database from UTF-8 to GBK or to specify the encoding in the header. Both methods have been tried but still to no avail.


$html .= iconv("UTF-8","GBK",$row[$key]).$tab;

header("Content-type: text/html; charset=utf-8")

So in this article I found that the solution is as long as I add the BOM before exporting the document: When exporting a CSV file with PHP and opening it with Excel, Chinese garbled characters appear (https:// xinpure.com/php-export-csv-file-opened-by-excel-appear-garbled/)

But after the BOM, the cells in each row are merged into one (\t) But using Table to solve the line break problem, I unexpectedly found that using Table solved the garbled code problem (no BOM required)

Solution:

  1. The PHP file is encoded as UTF-8, and specify the header encoding as


    UTF-8 header("Content-type:application/vnd.ms-excel;charset=UTF-8");
  2. The database encoding is set to UTF-8
  3. and use table for layout (before exporting the document Add BOM)

The above is the detailed content of What to do if php exports excel with garbled characters. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn