Browse Source

月检导出excel

lizhen_gitee 6 months ago
parent
commit
e5c3481b06

+ 13 - 0
application/common.php

@@ -5,6 +5,7 @@
 use think\exception\HttpResponseException;
 use think\Response;
 use fast\Random;
+use Hyperf\Utils\Collection;
 
 if (!function_exists('__')) {
 
@@ -981,4 +982,16 @@ if(!function_exists('birthtime_to_age')) {
 
         return $age;
     }
+}
+if (! function_exists('collect')) {
+    /**
+     * Create a collection from the given value.
+     *
+     * @param null|mixed $value
+     * @return Collection
+     */
+    function collect($value = null)
+    {
+        return new Collection($value);
+    }
 }

+ 79 - 0
application/common/library/ExcelCsv.php

@@ -0,0 +1,79 @@
+<?php
+namespace app\common\library;
+
+class ExcelCsv{
+    private $row;
+    private $data;
+
+    public function __construct($row,$data){
+        $this->row[] = $row;
+        $this->data = $data;
+    }
+
+    public function collection()
+    {
+        $row = $this->row;
+        $data = $this->data;
+        //设置表头
+        foreach ($row[0] as $key => $value) {
+            $key_arr[] = $key;
+        }
+
+        //输入数据
+        foreach ($data as $key => &$value) {
+            $js = [];
+            for ($i=0; $i < count($key_arr); $i++) {
+                $js = array_merge($js,[ $key_arr[$i] => $value[ $key_arr[$i] ] ]);
+            }
+            array_push($row, $js);
+            unset($val);
+        }
+
+        $collect =  collect($row);
+
+        $row = [];
+        $list = [];
+        foreach ($collect as $key=>$item){
+            $val = [];
+            foreach ($item as $v){
+                $val[] = $v;
+            }
+            if ($key == 0){
+                $row[] = $val;
+            }else{
+                $list[] = $val;
+            }
+        }
+
+        return $list;
+    }
+
+    public function download($fileName,$row,$list)
+    {
+        $fileName = $fileName.'.csv';
+        //设置文件头
+        header('Content-Description: File Transfer');
+        header('Content-Type: application/vnd.ms-excel');
+        header('Content-Disposition: attachment; filename="' . $fileName . '"');
+        header('Expires: 0');
+        header('Cache-Control: must-revalidate');
+        header('Pragma: public');
+
+        $fp = fopen('php://output', 'a');//打开output流
+        mb_convert_variables('GBK', 'UTF-8', $row);
+        fputcsv($fp, $row);
+
+        foreach ($list as $val) {
+            $val1 = [];
+            foreach ($val as $export_obj) {
+                $val1[] = iconv('utf-8', 'GB18030', $export_obj);
+            }
+            fputcsv($fp, $val1);
+            //刷新缓冲
+            ob_flush();
+            flush();
+        }
+        fclose($fp);
+        exit();
+    }
+}

+ 64 - 1
application/company/controller/Yuejian.php

@@ -4,6 +4,7 @@ namespace app\company\controller;
 
 use app\common\controller\Apic;
 use think\Db;
+use app\common\library\ExcelCsv;
 /**
  * 月检进度表
  */
@@ -165,6 +166,8 @@ class Yuejian extends Apic
             'month_12'     => $month_data['month_12']['total'],
         ];
 
+        $bottom_list_export = $bottom_list;
+
         //重整结果集,替换,对号,错号,空白
         foreach($bottom_list as $bk => $bottom){
             foreach($bottom as $key => $val){
@@ -183,6 +186,24 @@ class Yuejian extends Apic
             $bottom_list[$bk] = $bottom;
         }
 
+        //重整结果集,替换,对号,错号,空白
+        foreach($bottom_list_export as $bk => $bottom){
+            foreach($bottom as $key => $val){
+
+                if(strpos($key,'month_') !== false){
+                    //这里严格用===是因为,php7.4里: '一切字符串'==0 为true
+                    if($val === 'moren'){$val = '';}
+                    elseif($val === 1){$val = 'V';}
+                    elseif($val === 0){$val = 'X';}
+                }
+
+                $bottom[$key] = $val;
+            }
+
+            $bottom['endtime'] = date('Y-m-d',$bottom['endtime']);
+            $bottom_list_export[$bk] = $bottom;
+        }
+
 
         //组合结果
         $result[] = $top1;
@@ -190,7 +211,49 @@ class Yuejian extends Apic
         $result[] = $top3;
         $result   = array_merge($result,$bottom_list);
 
-        $this->success(1,$result);
+
+        //导出excel
+        $action = input('action','');
+        if($action == 'export'){
+
+            //表头
+            $excel_header = [
+                'num'         => '序号',
+                'companyname' => '合同单位',
+                'projectname' => '项目名称',
+                'endtime'     => '终止日期',
+                'month_01'     => '1月',
+                'month_02'     => '2月',
+                'month_03'     => '3月',
+                'month_04'     => '4月',
+                'month_05'     => '5月',
+                'month_06'     => '6月',
+                'month_07'     => '7月',
+                'month_08'     => '8月',
+                'month_09'     => '9月',
+                'month_10'     => '10月',
+                'month_11'     => '11月',
+                'month_12'     => '12月',
+            ];
+
+            //内容
+            $excel_result[] = $top1;
+            $excel_result[] = $top2;
+            $excel_result[] = $top3;
+            $excel_result   = array_merge($excel_result,$bottom_list_export);
+
+            //文件输出
+            $worker_info = Db::name('worker')->where('id',$worker_id)->value('truename');
+
+            $fileName = $year.'年'.$worker_info.'月检-' . date('Y-m-d-H-i-s');
+            $ExcelCsv = new ExcelCsv($excel_header, $excel_result);
+            $data = $ExcelCsv->collection();
+            $ExcelCsv->download($fileName, $excel_header, $data);
+
+        }else{
+            $this->success(1,$result);
+        }
+
 
     }