PHP export Excel A little bit of experience to solve garbled problems.
The PHP project requires that the data be exported to Excel, and that the data contains Chinese.
I probably know about it on the internet but using Phpexcel, but the framework is too complex for my needs. So I still want to find a simple way.
Online discovery is actually the simplest to write, but the problem is that the Chinese encoding is unreliable.
PHP code
Some students think of the header to join the character set
PHP code
Header ("Content-type:application/vnd.ms-excel;charset=utf-8");
question: This is just to tell the browser what character set to choose to view, and ultimately I need to generate XLS files.
Of course, some students will also think of using Iconv transcoding.
PHP code
Echo iconv ("Current Code", "GB18030", "This blog is from Javaeye,by Jason");
question: so the Chinese character coding in the file is GB18030, but how does Excel know what code to open it? You can only rely entirely on the OS default. But if the traditional BIG5 to do so, it will be garbled. So it's still not reliable.
Finally, we adopt the phpMyAdmin approach. With Htmlexcel, HTML we are more familiar with the format as follows.
HTML code
1234 |
Robbin will spit. |
5678 |
Javaeye website |
This can be directly echo, and do not need iconv transcoding, as long as the set of HTML Content-type (here is UTF-8), is not comfortable feeling it? Of course, the header has to be added
PHP code
Header ("Content-type:application/vnd.ms-excel"); Header ("Content-disposition:attachment;filename=export_data.xls");