Phpexcelreader2.21 how to export Chinese garbled characters in excel

Source: Internet
Author: User
Tags php excel xsl file
In my PHP Tutorial, a blog user asked me how to solve Chinese garbled characters when I used phpexcelreader2.21 to export excel. now I want to explain how phpexcelreader can export Chinese garbled characters in excel, I hope it will be helpful for anyone who uses phpexcelreader to export excel files. In my PHP Tutorial, a blog user asked me how to solve the garbled characters in Chinese when I used php excel reader2.21 to export excel. now I want to explain how to solve the garbled Chinese characters exported by php excel reader, I hope it will be helpful for anyone who uses php excel reader to export excel files.

  Introduction to php excel reader

Php excel reader is a php Excel class that reads the content of the excel xsl file. The latest version is php excel reader2.21. you can search for it on the internet. php excel reader on sourceforge is an initial version, the php excel reader in this tutorial exports Chinese characters in excel. the solution is based on php excel reader2.21. Therefore, the solution for exporting excel garbled characters in php versions is different, be sure to download the correct php excel reader version.

  How to solve php excel reader2.21 export excel Chinese garbled characters?

After downloading php excel reader2.21, decompress it to the running directory of your PHP environment configuration and open example. php.

1
$ Data = new Spreadsheet_Excel_Reader ("example.xls ");

This statement is used to create an php excel export instance. in the excel_reader2.php file, we can find the php excel reader class constructor prototype.

1
Spreadsheet_Excel_Reader ($ file = '', $ store_extended_info = true, $ outputEncoding = '')

As the name suggests, the encoding type of the excel file exported by php excel reader is specified by the $ outputEncoding parameter. the default encoding type of the excel file exported by php excel reader is set by the variable _ defaultEncoding, the default is UTF-8, so there are two methods to solve php excel reader export excel Chinese garbled.

  Php excel reader exporting excel Chinese garbled characters solution 1:

1
$ Data = new Spreadsheet_Excel_Reader ("example.xls ");

Change

1
$ Data = new Spreadsheet_Excel_Reader ("example.xls", true, "GB2312 ");

  Php excel reader exporting excel Chinese garbled characters solution 2: Open excel_reader2.php and find

1
Var $ _ defaultEncoding = "UTF-8 ";

Change

1
Var $ _ defaultEncoding = "GB2312 ";

You can solve the problem of excel garbled characters exported by php excel reader.

After the tutorial is modified, if you add Chinese characters to example.xls, the example. php still outputs garbled characters? This is because it calls the dump function in the php excel reader class, which is used to output the exported excel file content in HTML format, the problem is precisely because the htmlentities function in this function is strange. the htmlentities function is used to convert characters into HTML entities. the prototype is as follows:

1
Htmlentities (string, quotestyle, character-set)

Its default character set is ISO-8859-1, so when the use of php excel reader dump function to export excel appear Chinese garbled,

  Solution 1:

1
$ Val = htmlentities ($ val );

Change

1
$ Val = htmlentities ($ val, ENT_COMPAT, "GB2312 ");

  Solution 2

1
$ Val = htmlentities ($ val );

Change

1
$ Val = htmlspecialchars ($ val );

  Php excel reader export excel Chinese garbled solution summary

If you do not use the dump function to export an excel file, you can modify the _ defaultEncoding variable orNew Spreadsheet_Excel_Reader (excel file name, true, "GB2312 ″);Solve the problem of excel export garbled characters. if you use the dump function to export excel in HTML format, you need to modify the htmlentities function to solve the problem of excel export garbled characters.

Now, how to solve the problem of exporting excel Chinese garbled characters using php excel reader2.21 is complete.

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.