PHP 导出excel表格
1、使用第三方库
2、
1 <?php 2 $str1 = "id\tname\tage\n"; \\\t换列;\n换行 3 $str2 = "123\tsaisai\t23"; 4 header('Content-Type: application/vnd.ms-excel'); 5 header('Content-Disposition: attachment; filename=demo.xls'); 6 echo $str1; 7 echo $str2;
结果:
3、
1 <?php 2 $str1 = "2017-03-09<br>eat<br>apple"; 3 $search = array("<br>","</br>","<br/>","<br/>"); 4 $replace = '<br style="mso-data-placement:same-cell;" />'; 5 $str1 = str_replace(array("<br>","</br>","<br/>","<br/>"),'<br style="mso-data-placement:same-cell;" />',$str1); 6 $str="<meta http-equiv='Content-type' content='text/html;charset=UTF-8' /> 7 <table border='1'> 8 <tr> 9 <td align='center'>id</td> 10 <td align='center'>name</td> 11 <td align='center'>age</td> 12 <td align='center'>note</td> 13 </tr> 14 <tr> 15 <td align='center'>123</td> 16 <td align='center'>saisai</td> 17 <td align='center'>23</td> 18 <td align='center'>{$str1}</td> 19 </tr> 20 </table>"; 21 header( "Content-type:application/vnd.ms-excel" ); 22 header ( "Content-Disposition:filename=demo.xls" ); 23 echo ($str); exit;
<br>将会换行,需要给<br>加样式 “mso-data-placement:same-cell” 让其换行后在一个单元格内显示。
同样可以给表格样式中添加背景色。