sunhao 5 years ago
parent
commit
77af299e39
1 changed files with 50 additions and 61 deletions
  1. 50 61
      app/Http/Controllers/Admin/OrderController.php

+ 50 - 61
app/Http/Controllers/Admin/OrderController.php

@@ -15,6 +15,8 @@ use Illuminate\Support\Facades\DB;
15 15
 
16 16
 use PHPExcel_Reader_Excel2007;
17 17
 use PHPExcel_Reader_Excel5;
18
+use PHPExcel;
19
+use PHPExcel_Writer_Excel2007;
18 20
 
19 21
 class OrderController extends Controller
20 22
 {
@@ -681,68 +683,15 @@ class OrderController extends Controller
681 683
             if($delivery_type==0) $query->where('delivery_date', '<=', $today_date)->where('logistics_id', '=', '');
682 684
         })->where('is_del',0)->orderBy('id', 'desc')->get();
683 685
         $result = json_decode(json_encode($result),true);
686
+        foreach ($result as $k => &$v)
687
+        {
688
+            $v['address'] = $v['receiverState'].$v['receiverCity'].$v['receiverDistrict'].$v['receiverAddress'];                
689
+        }
684 690
 
685
-
686
-        $filename="订单数据.xls";
687
-        header("Content-type:application/vnd.ms-excel");
688
-        Header("Accept-Ranges:bytes");
689
-        Header("Content-Disposition:attachment;filename=".$filename); //$filename导出的文件名
690
-        header("Pragma: no-cache");
691
-        header("Expires: 0");
692
-
693
-        $data_str = '<html xmlns:o="urn:schemas-microsoft-com:office:office"
694
-            xmlns:x="urn:schemas-microsoft-com:office:excel"
695
-            xmlns="http://www.w3.org/TR/REC-html40">
696
-            <head>
697
-            <meta http-equiv="expires" content="Mon, 06 Jan 1999 00:00:01 GMT">
698
-            <meta http-equiv=Content-Type content="text/html; charset=gb2312">
699
-            <!--[if gte mso 9]><xml>
700
-            <x:ExcelWorkbook>
701
-            <x:ExcelWorksheets>
702
-            <x:ExcelWorksheet>
703
-            <x:Name></x:Name>
704
-            <x:WorksheetOptions>
705
-            <x:DisplayGridlines/>
706
-            </x:WorksheetOptions>
707
-            </x:ExcelWorksheet>
708
-            </x:ExcelWorksheets>
709
-            </x:ExcelWorkbook>
710
-            </xml><![endif]-->
711
-            </head>';
712
-
713
-        $data_str .= "<table><tr>";
714
-
715
-        $data_str .="<th>".iconv("UTF-8", "GB2312//IGNORE","订单编号")."</th>";
716
-        $data_str .="<th>".iconv("UTF-8", "GB2312//IGNORE","商品信息")."</th>";
717
-        $data_str .="<th>".iconv("UTF-8", "GB2312//IGNORE","收货人")."</th>";
718
-        $data_str .="<th>".iconv("UTF-8", "GB2312//IGNORE","收货人手机号")."</th>";
719
-        $data_str .="<th>".iconv("UTF-8", "GB2312//IGNORE","收货地址")."</th>";
720
-        $data_str .="<th>".iconv("UTF-8", "GB2312//IGNORE","下单时间")."</th>";
721
-        $data_str .="<th>".iconv("UTF-8", "GB2312//IGNORE","卖家备注")."</th>";
722
-        $data_str .="<th>".iconv("UTF-8", "GB2312//IGNORE","买家备注")."</th>";
723
-        $data_str .="<th>".iconv("UTF-8", "GB2312//IGNORE","供应商成本")."</th>";
724
-        $data_str .="<th>".iconv("UTF-8", "GB2312//IGNORE","顺丰单号")."</th></tr>";
725
-
726
-            foreach ($result as $k => $v)
727
-            {
728
-                $address = $v['receiverState'].$v['receiverCity'].$v['receiverDistrict'].$v['receiverAddress'];
729
-                $data_str .= "<tr>";
730
-                $data_str .= "<td>".$v['id']."</td>";
731
-                $data_str .= "<td>".iconv("UTF-8", "GB2312//IGNORE", $v["goods_note"])."</td>";
732
-                $data_str .= "<td>".iconv("UTF-8", "GB2312//IGNORE", $v["receiverName"])."</td>";
733
-                $data_str .= "<td>".iconv("UTF-8", "GB2312//IGNORE", $v["receiverMobile"])."</td>";
734
-                $data_str .= "<td>".iconv("UTF-8", "GB2312//IGNORE", $address)."</td>";
735
-                $data_str .= "<td>".$v['createTime']."</td>";
736
-                $data_str .= "<td>".iconv("UTF-8", "GB2312//IGNORE", $v["sellerMemo"])."</td>";
737
-                $data_str .= "<td>".iconv("UTF-8", "GB2312//IGNORE", $v["buyerMemo"])."</td>";
738
-                $data_str .= "<td>".$v['cost']."</td>";
739
-                $data_str .= "<td>".$v['logistics_id']."</td>";
740
-                $data_str .= "</tr>";
741
-            }
742
-
743
-        $data_str .= "</table>";
744
-        echo $data_str;
745
-        exit;       
691
+        $indexKey = ['id','goods_note','receiverName','receiverMobile','address','createTime','sellerMemo','buyerMemo','cost','logistics_id'];
692
+        $title = ['订单编号', '商品信息', '收货人', '收货人手机号', '收货地址', '下单时间', '卖家备注', '买家备注', '供应商成本', '顺丰单号'];
693
+        return $this->export_excel($result, '订单数据.xlsx', $indexKey, $title);
694
+ 
746 695
     }
747 696
 
748 697
     /**
@@ -810,6 +759,46 @@ class OrderController extends Controller
810 759
 
811 760
     }
812 761
 
762
+    /**
763
+     * 导出excel
764
+     * @param $data
765
+     * @param string 
766
+     */
767
+    public function export_excel($data, $filename = '未命名.xlsx', $indexKey, $title) {
768
+
769
+        if( !is_array($indexKey)) return false;            
770
+        $header_arr = array('A','B','C','D','E','F','G','H','I','J','K','L','M', 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z');  
771
+        //初始化PHPExcel()  
772
+        $objPHPExcel = new PHPExcel();            
773
+        $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);    
774
+            
775
+        //接下来就是写数据到表格里面去  
776
+        $objActSheet = $objPHPExcel->getActiveSheet();  
777
+        foreach($title as $k=>$item){
778
+            $objActSheet->setCellValue($header_arr[$k].'1',$item);
779
+        }
780
+        $startRow = 2;  
781
+        foreach ($data as $row) {  
782
+            foreach ($indexKey as $key => $value){  
783
+                //这里是设置单元格的内容  
784
+                $objActSheet->setCellValue($header_arr[$key].$startRow,$row[$value]);  
785
+            }  
786
+            $startRow++;  
787
+        } 
788
+
789
+        header("Pragma: public");  
790
+        header("Expires: 0");  
791
+        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");  
792
+        header("Content-Type:application/force-download");  
793
+        header("Content-Type:application/vnd.ms-execl");  
794
+        header("Content-Type:application/octet-stream");  
795
+        header("Content-Type:application/download");;  
796
+        header('Content-Disposition:attachment;filename='.$filename.'');  
797
+        header("Content-Transfer-Encoding:binary");  
798
+        $objWriter->save('php://output');  
799
+        exit();
800
+    }
801
+
813 802
 
814 803
 }
815 804