您的位置:首页 > 编程语言 > PHP开发

PHP生成excel,方法一:header生成

2018-02-09 16:22 302 查看
    public function export_order()
    {
    //搜索条件
$where = 'where 1=1 ';
$consignee = I('consignee');
if($consignee){
$where .= " AND consignee like '%$consignee%' ";
}
$order_sn =  I('order_sn');
if($order_sn){
$where .= " AND order_sn = '$order_sn' ";
}
if(I('order_status')){
$where .= " AND order_status = ".I('order_status');
}

$timegap = I('timegap');
if($timegap){
$gap = explode('-', $timegap);
$begin = strtotime($gap[0]);
$end = strtotime($gap[1]);
$where .= " AND add_time>$begin and add_time<$end ";
}
    
$sql = "select *,FROM_UNIXTIME(add_time,'%Y-%m-%d') as create_time from __PREFIX__order $where order by order_id";
    $orderList = D()->query($sql);
    $strTable ='<table width="500" border="1">';
    $strTable .= '<tr>';
    $strTable .= '<td style="text-align:center;font-size:12px;width:120px;">订单编号</td>';
    $strTable .= '<td style="text-align:center;font-size:12px;" width="100">日期</td>';
    $strTable .= '<td style="text-align:center;font-size:12px;" width="*">收货人</td>';
    $strTable .= '<td style="text-align:center;font-size:12px;" width="*">收货地址</td>';
    $strTable .= '<td style="text-align:center;font-size:12px;" width="*">电话</td>';
    $strTable .= '<td style="text-align:center;font-size:12px;" width="*">订单金额</td>';
    $strTable .= '<td style="text-align:center;font-size:12px;" width="*">实际支付</td>';
    $strTable .= '<td style="text-align:center;font-size:12px;" width="*">支付方式</td>';
    $strTable .= '<td style="text-align:center;font-size:12px;" width="*">支付状态</td>';
    $strTable .= '<td style="text-align:center;font-size:12px;" width="*">发货状态</td>';
    $strTable .= '<td style="text-align:center;font-size:12px;" width="*">商品信息</td>';
    $strTable .= '</tr>';
    if(is_array($orderList)){
    $region = M('region')->getField('id,name');
    foreach($orderList as $k=>$val){
    $strTable .= '<tr>';
    $strTable .= '<td style="text-align:center;font-size:12px;"> '.$val['order_sn'].'</td>';
    $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['create_time'].' </td>';    
    $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['consignee'].'</td>';
                        $strTable .= '<td style="text-align:left;font-size:12px;">'."{$region[$val['province']]},{$region[$val['city']]},{$region[$val['district']]},{$regi
e67a
on[$val['twon']]}{$val['address']}".' </td>';                        
    $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['mobile'].'</td>';
    $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['goods_price'].'</td>';
    $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['order_amount'].'</td>';
    $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['pay_name'].'</td>';
    $strTable .= '<td style="text-align:left;font-size:12px;">'.$this->pay_status[$val['pay_status']].'</td>';
    $strTable .= '<td style="text-align:left;font-size:12px;">'.$this->shipping_status[$val['shipping_status']].'</td>';
    $orderGoods = D('order_goods')->where('order_id='.$val['order_id'])->select();
    $strGoods="";
    foreach($orderGoods as $goods){
    $strGoods .= "商品编号:".$goods['goods_sn']." 商品名称:".$goods['goods_name'];
    if ($goods['spec_key_name'] != '') $strGoods .= " 规格:".$goods['spec_key_name'];
    $strGoods .= "<br />";
    }
    unset($orderGoods);
    $strTable .= '<td style="text-align:left;font-size:12px;">'.$strGoods.' </td>';
    $strTable .= '</tr>';
    }
    }
    $strTable .='</table>';
    unset($orderList);
    downloadExcel($strTable,'order');
    exit();
    }


/**
 * 导出excel
 * @param $strTable 表格内容
 * @param $filename 文件名
 */
function downloadExcel($strTable,$filename)
{
header("Content-type: application/vnd.ms-excel");
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=".$filename."_".date('Y-m-d').".xls");
header('Expires:0');
header('Pragma:public');
echo '<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'.$strTable.'</html>';

}
摘取了常用的部分,其实还有其他一些
$mimetypes = array(
    'doc'        => 'application/msword',
    'bin'        => 'application/octet-stream',
    'exe'        => 'application/octet-stream',
    'so'        => 'application/octet-stream',
    'dll'        => 'application/octet-stream',
    'pdf'        => 'application/pdf',
    'ai'        => 'application/postscript',
    'xls'        => 'application/vnd.ms-excel',
    'ppt'        => 'application/vnd.ms-powerpoint',
    'dir'        => 'application/x-director',
    'js'        => 'application/x-javascript',
    'swf'        => 'application/x-shockwave-flash',
    'xhtml'        => 'application/xhtml+xml',
    'xht'        => 'application/xhtml+xml',
    'zip'        => 'application/zip',
    'mid'        => 'audio/midi',
    'midi'        => 'audio/midi',
    'mp3'        => 'audio/mpeg',
    'rm'        => 'audio/x-pn-realaudio',
    'rpm'        => 'audio/x-pn-realaudio-plugin',
    'wav'        => 'audio/x-wav',
    'bmp'        => 'image/bmp',
    'gif'        => 'image/gif',
    'jpeg'        => 'image/jpeg',
    'jpg'        => 'image/jpeg',
    'png'        => 'image/png',
    'css'        => 'text/css',
    'html'        => 'text/html',
    'htm'        => 'text/html',
    'txt'        => 'text/plain',
    'xsl'        => 'text/xml',
    'xml'        => 'text/xml',
    'mpeg'        => 'video/mpeg',
    'mpg'        => 'video/mpeg',
    'avi'        => 'video/x-msvideo',
    'movie'        => 'video/x-sgi-movie',  

);

相关文章 http://blog.csdn.net/load_life/article/details/7992675
              https://zhidao.baidu.com/question/352112931.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: