PHP exports excel with a little experience to solve the garbled problem.

Source: Internet
Author: User
Tags php excel
PHP exports Excel with a little experience to solve the garbled problem.

08:57:53 | category: php excel export | font size subscription

In my PHP project, I want to export data to Excel and contain Chinese characters.

I 'd like to know about PHPExcel on the Internet, but this framework is too complicated for my needs. So I still want to find a simple method.

It is actually the easiest way to write data on the Internet, but the problem is that the Chinese encoding is not reliable ..

Php code
  1. <? Php
  2. Header ("Content-type: application/vnd. ms-excel ");
  3. Header ("Content-Disposition: attachment?filename=export_data.xls ");
  4. Echo "name". "\ t ";
  5. Echo "traditional". "\ t ";
  6. Echo "blog". "\ t ";
  7. Echo "\ n ";
  8. Echo "jason". "\ t ";
  9. Echo "@". "\ t ";
  10. Echo "javaeye". "\ t ";
  11. ?>

Some students think of adding headers to character sets.

Php code
  1. Header ("Content-type: application/vnd. ms-excel; charsets = UTF-8 ");

Problem:Here, I only want to tell the browser which character set to choose to view. In the end, I still need to generate xls files.

 

Of course. Some people will think of iconv transcoding.

Php code
  1. Echo iconv ("current code", "GB18030", "this blog is from javaeye, by jason ");

Problem:In this way, the Chinese character encoding in the file is GB18030, But what encoding does Excel use to open it? You can only rely entirely on the default OS. However, if you encounter BIG5 in Traditional Chinese style, it will still be garbled, so it is still unreliable.

 

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

Html code
  1. <Html xmlns: o = "urn: schemas-microsoft-com: office"
  2. Xmlns: x = "urn: schemas-microsoft-com: office: excel"
  3. Xmlns = "http://www.w3.org/TR/REC-html40">
  4. <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  5. <Html>
  6. <Head>
  7. <Meta http-equiv = "Content-type" content = "text/html; charset = UTF-8"/>
  8. <Style id = "classeurrent16681_styles"> </style>
  9. </Head>
  10. <Body>
  11. <Div id = "classeurrent16681" align = center x: publishsource = "Excel">
  12. <Table x: str border = 0 cellpadding = 0 cellspacing = 0 width = 100% style = "border-collapse: collapse">
  13. <Tr> <td class = xl2216681 nowrap> 1234 </td> <td class = xl2216681 nowrap> Robbin spitting </td> </tr>
  14. <Tr> <td class = xl2216681 nowrap> 5678 </td> <td class = xl2216681 nowrap> javaeye website </td> </tr>
  15. </Table>
  16. </Div>
  17. </Body>
  18. </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
  1. Header ("Content-type: application/vnd. ms-excel ");
  2. Header ("Content-Disposition: attachment?filename=export_data.xls ");

 

I would like to share with you a little bit of experience ..

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.