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
- <? Php
- Header ("Content-type: application/vnd. ms-excel ");
- Header ("Content-Disposition: attachment?filename=export_data.xls ");
- Echo "name". "\ t ";
- Echo "traditional". "\ t ";
- Echo "blog". "\ t ";
- Echo "\ n ";
- Echo "jason". "\ t ";
- Echo "@". "\ t ";
- Echo "javaeye". "\ t ";
- ?>
Some students think of adding headers to character sets.
Php code
- 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
- 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
- <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
- Header ("Content-type: application/vnd. ms-excel ");
- Header ("Content-Disposition: attachment?filename=export_data.xls ");
I would like to share with you a little bit of experience ..