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
-
- /*
- * Created on 2012-2-26
- *
- * To change the template for this generated file go
- * Window-Preferences-PHPeclipse-PHP-Code Templates
- */
- Header ("Content-type: application/vnd. ms-excel ");
- Header ("Content-Disposition: filenameappsphp100.xls ");
- ?>
-
-
- "Red" align = "center"> class infomation
-
-
-
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.
-
- Xmlns: x = "urn: schemas-microsoft-com: office: excel"
- Xmlns = "http://www.w3.org/TR/REC-html40">
-
-
-
-
-
-
-
- "Classeurrent16681" align = center x: publishsource = "Excel">
-
- 1234 Robbin will spit
- 5678 Javaeye website
-
-
-
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.