Based on PHP derived Excel's little experience to solve the problem of messy code perfectly

  • 2020-06-12 08:43:03
  • OfStack

In the PHP project, I required the data to be exported as Excel, and the data included Chinese.
I know about using PHPExcel on the Internet, but this framework is too complicated for my needs. So I still want to find a simple way to do it.
It is found on the Internet that it is the easiest way to write in this way, but the problem is that the code in Chinese is not reliable..

<?php 
 header("Content-type:application/vnd.ms-excel"); 
 header("Content-Disposition:attachment;filename=export_data.xls"); 
 echo   " The name "."\t";  
 echo   " Numerous � "."\t";  
 echo   " blog "."\t";  
 echo   "\n";  
 echo   "jason"."\t";  
 echo   "@"."\t";  
 echo   "javaeye"."\t";  
 ?> 

Some students would like to add header to the character set

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

Problem: This just tells the browser which character set to view, and ultimately my requirement is to generate the xls file.
Sure. Some of you might even think of using iconv for transcoding.

echo iconv(" The current code ","GB18030"," This blog comes from javaeye,by jason");

Q: So the Chinese character code in the file is GB18030, but Excel so know what code to open? You can only rely on OS default. But if you do this with traditional BIG5, you will still get confused. So it is still not reliable.
Finally, I adopted the method of phpMyAdmin. We are familiar with HTMLExcel and HTML, and the format is as follows.

<html xmlns:o="urn:schemas-microsoft-com:office: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="Classeur1_16681_Styles"></style> 
     </head> 
     <body> 
         <div id="Classeur1_16681" 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 Will spit </td></tr> 
                <tr><td class=xl2216681 nowrap>5678</td><td class=xl2216681 nowrap>javaeye Web site </td></tr> 
             </table> 
         </div> 
     </body> 
 </html> 

Now you can go directly to echo without iconv transcoding. Just set the ES32en-ES33en in HTML (UTF-8 is used here). Is it comfortable? Of course header still has to be added

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

1 little experience to share with you 1.

Related articles: