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

本文介绍了一种使用PHP从数据库中检索订单信息并将其导出为Excel文件的方法。通过构造SQL查询,根据用户提供的筛选条件获取订单详情,并将这些信息格式化成表格形式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    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;">&nbsp;'.$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']]},{$region[$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.youkuaiyun.com/load_life/article/details/7992675

              https://zhidao.baidu.com/question/352112931.html


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值