Php export excel Chinese garbled solution summary

Source: Internet
Author: User
Tags character set export class php code php excel xmlns

1. Define the file name

2. Fill in Excel data

Some PHP Excel export garbled characters may occur during these two processes. The solution is as follows:


Solve the Excel data export garbled by PHP 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:

The code is as follows: Copy code
Header ("Content-Type: application/vnd. ms-excel; charset = UTF-8 & Prime ;)

, 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

The code is as follows: Copy code

<? Php
/*
* 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 ");
?>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Table>
<Tr>
<Td colspan = "4"> <font color = "red" align = "center"> class infomation </font> </td>
</Tr>
<Tr>
<Td> <? Php echo iconv ("UTF-8", "GBK", "number");?> </Td>
<Td> <? Php echo iconv ("UTF-8", "GBK", "name");?> </Td>
<Td> <? Php echo iconv ("UTF-8", "GBK", "student ID");?> </Td>
<Td> <? Php echo iconv ("UTF-8", "GBK", "gender");?> </Td>
</Tr>
</Table>

Summary

Convert to GBK before export

The code is as follows: Copy code

Iconv ("UTF-8", "GBK", $ value );

Other methods

Finally, I use phpMyAdmin. We are familiar with using HTMLExcel and HTML. The format is as follows.

The code is as follows: Copy code

<Html xmlns: o = "urn: schemas-microsoft-com: office"
Xmlns: x = "urn: schemas-microsoft-com: office: excel"
Xmlns = "http://www.w3.org/TR/REC-html40">
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html>
<Head>
<Meta http-equiv = "Content-type" content = "text/html; charset = UTF-8"/>
<Style id = "classeurrent16681_styles"> </style>
</Head>
<Body>
<Div id = "classeurrent16681" align = center x: publishsource = "Excel">
<Table x: str border = 0 cellpadding = 0 cellspacing = 0 width = 100% style = "border-collapse: collapse">
<Tr> <td class = xl2216681 nowrap> 1234 </td> <td class = xl2216681 nowrap> Robbin spitting </td> </tr>
<Tr> <td class = xl2216681 nowrap> 5678 </td> <td class = xl2216681 nowrap> javaeye website </td> </tr>
</Table>
</Div>
</Body>
</Html>

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.

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 you export a Chinese name, garbled characters may also occur.

Solve the Excel file export garbled by PHP:

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:

The code is as follows: Copy code
Header ("Content-Disposition: inline; filename =" ". $ filename. Pai.xls "")

If your webpage code is gb2312, you do not need to consider encoding.

Solution:

To transcode $ filename, run:

The code is as follows: Copy code
Iconv ('utf-8 & Prime;, "gb2312 & Prime;, $ filename)

. If your environment does not support iconv functions, you only need to convert $ filename encoding to gbk.
However, this problem occurs again, and linux users may encounter garbled file names (because the file name on the linux platform is 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, provide two download addresses like gmail. A file name must be gbk encoded, and a file name must be UTF-8 encoded.

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.