PHP 导出Excel 备忘

<?php
header("Content-type:application/vnd.ms-excel;charset=UTF-8");
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:attachment;filename=order_name.xls");
echo "订单号"."\t";
echo "商品名称"."\t";
echo "商品属性"."\t";
echo "订单状态"."\t";
echo "支付方式"."\t";
echo "订单金额"."\t";
echo "收货人"."\t";
echo "收货电话"."\t";
echo "收货地址"."\t";
echo "\n";

foreach($data as $v){
    echo $v['id'].'\''."\t";
    echo $v['title']."\t";
    echo $v['specification']."\t";
    echo $v['status']."\t";
    echo $v['type']."\t";
    echo $v['money']."\t";
    echo $v['name']."\t";
    echo $v['phone']."\t";
    echo $v['address']."\t";
    echo "\n";
}

主要是输出头信息

\n 软回车:
      在Windows 中表示换行且回到下一行的最开始位置。相当于Mac OS 里的 \r 的效果。
      在Linux、unix 中只表示换行,但不会回到下一行的开始位置。
\r 软空格:
  在Linux、unix 中表示返回到当行的最开始位置。
  在Mac OS 中表示换行且返回到下一行的最开始位置,相当于Windows 里的 \n 的效果。

\t 跳格(移至下一列)。
  它们在双引号或定界符表示的字符串中有效,在单引号表示的字符串中无效。
  \r\n 一般一起用,用来表示键盘上的回车键,也可只用 \n。
  \t表示键盘上的“TAB”键。

 

方法2:

 

<?php 
header("Content-type:application/vnd.ms-excel;charset=UTF-8");
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:attachment;filename=order_name.xls");
?>
<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=1 cellpadding=0 cellspacing=0 width=100% style="border-collapse: collapse">
                  <tr>
                    <td class=xl2216681 nowrap width="150">订单号</td>
                    <td class=xl2216681 nowrap width="150">商品名称</td>
                    <td class=xl2216681 nowrap width="120">商品属性</td>
                    <td class=xl2216681 nowrap width="150">订单状态</td>
                    <td class=xl2216681 nowrap width="50">支付方式</td>
                    <td class=xl2216681 nowrap width="50">订单金额</td>
                    <td class=xl2216681 nowrap width="150">收货人</td>
                    <td class=xl2216681 nowrap width="150">收货电话</td>
                    <td class=xl2216681 nowrap width="350">收货地址</td>
                 </tr> 
                <?php 
                $data = is_array($data)?$data:array();
                foreach($data as $k=>$v){
                ?>
                 <tr>
                    <td class=xl2216681 nowrap><?=$v['id']?></td>
                    <td class=xl2216681 nowrap><?=$v['title']?></td>
                    <td class=xl2216681 nowrap><?=$v['specification']?></td>
                    <td class=xl2216681 nowrap><?=$v['status']?></td>
                    <td class=xl2216681 nowrap><?=$v['type']?></td>
                    <td class=xl2216681 nowrap><?=$v['money']?></td>
                    <td class=xl2216681 nowrap><?=$v['name']?></td>
                    <td class=xl2216681 nowrap><?=$v['phone']?></td>
                    <td class=xl2216681 nowrap><?=$v['address']?></td>
                 </tr> 
                 <?php }?> 
             </table> 
         </div> 
     </body> 
 </html>

 

posted @ 2015-09-19 11:12  一盏油灯-俊轩  阅读(221)  评论(0编辑  收藏  举报