Export Excel based on PHP small experience Perfect solution garbled problem _php skill

Source: Internet
Author: User
I asked to export the data to excel in the PHP project, and the data contained Chinese.
I probably know about it on the internet but use Phpexcel, but the framework is too complicated for my needs. So I still want to find a simple way.
Online discovery is actually the easiest way to write, but the problem is that the Chinese code is unreliable.
Copy Code code as follows:

Header ("Content-type:application/vnd.ms-excel");
Header ("Content-disposition:attachment;filename=export_data.xls");
echo "Name". " \ t ";
echo "Multiply". \ t ";
echo "blog". \ t ";
echo "\ n";
echo "Jason". " \ t ";
echo "@". \ t ";
echo "Javaeye". \ t ";

Some alumni think header join character set
Copy Code code as follows:

Header ("Content-type:application/vnd.ms-excel;charset=utf-8");

question: here just tells the browser to choose what character set to view, finally my demand or to generate XLS file.
Of course, some students will also think of using Iconv transcoding.
Copy Code code as follows:

Echo iconv ("Current Code", "GB18030", "this blog originates from Javaeye,by Jason");

question: so the encoding in the file is GB18030, but does Excel know what code to open? You can only rely entirely on OS defaults. But if you encounter the traditional BIG5 so, it will be garbled.
Finally, I adopted the phpmyadmin approach. With Htmlexcel, HTML we are familiar with the format below.
Copy Code code as follows:

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 ">
<meta http-equiv= "Content-type" content= "Text/html;charset=utf-8"/>
<style id= "Classeur1_16681_styles" ></style>
<div id= "classeur1_16681" Align=center x:publishsource= "Excel" >
<table x:str border=0 cellpadding=0 cellspacing=0 width=100% style= "Border-collapse:collapse" >
&LT;TR&GT;&LT;TD class=xl2216681 nowrap>1234</td><td class=xl2216681 Nowrap>robbin will spit </td> </tr>
&LT;TR&GT;&LT;TD class=xl2216681 nowrap>5678</td><td class=xl2216681 nowrap>javaeye website </td> </tr>

This can be directly echo, and do not need to iconv transcoding, as long as the set of good HTML Content-type (here with the UTF-8), is not comfortable feeling it? Of course the header is still to be added
Copy Code code as follows:

Header ("Content-type:application/vnd.ms-excel");
Header ("Content-disposition:attachment;filename=export_data.xls");

a little experience to share with fellow students.
Related Article

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.