PHP导出excel表格文件并下载,不需要利用插件


简单粗暴的写法。需要title对应着 字段。。用的是CI框架,写法差不多。调用exportexcel ,

//导出excle内容---------------------------
     public function getExc(){
         $table = 'jqy_pay_1';

         $field = "a.id,a.sn,a.openid,a.fac_id,a.status,a.type,a.price,a.has_time,a.set_time,a.over_time,b.facilities_code,b.facilities_name";
         $status = ['未支付','已支付','作废'];
         $type = ['1'=>'金额','2'=>'时长'];

         $query = $this->db->query('select '.$field.' from '.$table.' as a left join jqy_facilities as b on a.fac_id=b.facilities_code order by a.id desc limit 30');
         $list = $query->result_array();

         foreach ($list as $k =>$v){
             $list[$k]['status'] = $status[$v['status']];
             $list[$k]['type'] = $type[$v['type']];
             if($v['has_time'] !=0){
                 $list[$k]['has_time'] = date('Y-m-d H:i:s',$v['has_time']);
             }else{
                 $list[$k]['has_time'] = "";
             }
             if($v['set_time']!=0){
                 $list[$k]['set_time'] =date('Y-m-d H:i:s',$v['set_time']);
             }else{
                 $list[$k]['set_time']  = "";
             }
             if($v['over_time']!=0){
                 $list[$k]['over_time'] = date('Y-m-d H:i:s',$v['over_time']);
             }else{
                 $list[$k]['over_time'] = "";
             }

//             $list[$k]['fac_id'] =  $this->_getfield('jqy_facilities','facilities_name','id='.$v['fac_id']);
         }

         $title = array('序号','流水号','消费者微信号','设备名称','状态','交易类型','交易金额','交易时长','微信支付时间','交易完成时间','设备代码','设备名称');
//         $title = array('会员ID','工号','昵称','姓名','性别','电话','部门','状态','注册时间');
         $this->exportexcel($list, $title, '订单数据' . date('Y-m-d H:i:s', time()));


     }
     //导出excle结束----------------


     //导入操作这个函数
     private function exportexcel($data = array(), $title = array(), $filename = 'report')
     {
         header("Content-type:application/octet-stream");
         header("Accept-Ranges:bytes");
         header("Content-type:application/vnd.ms-excel");
         header("Content-Disposition:attachment;filename=" . $filename . ".xls");
         header("Pragma: no-cache");
         header("Expires: 0");
         //导出xls 开始
         if (!empty($title)) {
             foreach ($title as $k => $v) {
                 $title[$k] = iconv("UTF-8", "GB2312", $v);
             }
             $title = implode("\t", $title);
             echo "$title\n";
         }
         if (!empty($data)) {
             foreach ($data as $key => $val) {
                 foreach ($val as $ck => $cv) {
                     $data[$key][$ck] = iconv("UTF-8", "GB2312", $cv);
                 }
                 $data[$key] = implode("\t", $data[$key]);

             }
             echo implode("\n", $data);
         }

     }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值