Browse Source

导出优化

wufeng 2 years ago
parent
commit
d5c0982e51
2 changed files with 15 additions and 13 deletions
  1. 2 0
      app/admin/controller/Report.php
  2. 13 13
      app/common.php

+ 2 - 0
app/admin/controller/Report.php

@@ -47,6 +47,7 @@ class Report extends Base
             ->where($where_ladder)
             ->leftJoin('good_platform gp', 'gp.skuCode=gl.skuCode AND gp.is_del=0')
             ->leftJoin('good g', 'g.spuCode=gp.spuCode AND g.is_del=0')
+            ->order('gl.id')
             ->select()
             ->toArray();
 
@@ -64,6 +65,7 @@ class Report extends Base
             ->field('"" 一级分类,"" 二级分类, g.cat_id 三级分类,g.good_name 商品名称,g.good_type 商品类型,g.brand_id 商品品牌,\'\' 型号,g.origin_place 产地,g.good_unit 计量单位,g.weight 重量g,\'\' 响应时间,g.lead_time 供货周期,g.good_size 商品尺寸,g.packing_size 装箱尺寸,g.packing_way 包装方式,g.packing_spec 装箱规格,g.packing_list 包装清单,g.delivery_place 发货地,g.delivery_day 物流时间,gp.skuCode 商品编码,g.spuCode')
             ->where($where_good)
             ->leftJoin('good_platform gp', 'gp.spuCode=g.spuCode AND gp.is_del=0')
+            ->order('g.id')
             ->select()
             ->toArray();
 

+ 13 - 13
app/common.php

@@ -811,6 +811,10 @@ if (!function_exists('excelSaveBatch')) {
     {
 
         $urls = [];
+        $dir = root_path() . 'public/storage/report/' . date("YmdHis") . "/";
+        if (!is_dir($dir)) {
+            mkdir($dir, 0777, true);
+        }
         foreach ($datas as $item) {
 
             $objPHPExcel = new PHPExcel();
@@ -842,10 +846,7 @@ if (!function_exists('excelSaveBatch')) {
             $file = $item['filename'] . ".xls";
             //$fileName .= "_" . date("Y_m_d", Request()->instance()->time()) . ".xls";
             //$fileName = iconv("utf-8", "gb2312", $fileName); // 重命名表
-            $dir = root_path() . 'public/storage/report/' . date("YmdHis") . "/";
-            if (!is_dir($dir)) {
-                mkdir($dir, 0777, true);
-            }
+
             $objPHPExcel->setActiveSheetIndex(0); // 设置活动单指数到第一个表,所以Excel打开这是第一个表
             $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
             $objWriter->save($dir . $file); // 文件通过浏览器下载
@@ -863,25 +864,24 @@ if (!function_exists('excelSaveBatch')) {
             mkdir($saveDir, 0777, true);
         }
 
-        $datetime = date("Y-m-d H:i:s");
+        $datetime = date("YmdHis");
         $file_dir = $saveDir . $datetime . ".zip";
         # 5.1 文件打包,提示:使用本类,linux需开启zlib,windows需取消php_zip.dll前的注释
         $zip = new \ZipArchive ();
         # 5.2 文件不存在则生成一个新的文件 用CREATE打开文件会追加内容至zip
-        if ($zip->open($file_dir, \ZipArchive::OVERWRITE) !== true && $zip->open($file_dir, \ZipArchive::CREATE) !==
-            true) echo '无法打开文件或者文件创建失败';
+        if ($zip->open($file_dir, \ZipArchive::OVERWRITE) !== true && $zip->open($file_dir, \ZipArchive::CREATE) !== true) echo '无法打开文件或者文件创建失败';
 
         # 5.3 批量写入压缩包
+//        $zip->addEmptyDir($fileName);//往zip压缩包写入空目录
         foreach ($urls as $fileName) {
-            $zip->addEmptyDir($fileName);
+            @$zip->addFile($fileName);
         }
         // @$zip->addFile($v['file_path'], 'resume'.DIRECTORY_SEPARATOR.basename($headername));
-        @$zip->addFile($url, $fileName . DIRECTORY_SEPARATOR . basename($url));
         # 5.4 关闭压缩包写入
         $zip->close();
 
-        @deldir($dir);
-        # 6. 检查文件是否存在,并输出文件
+        @deldir($dir);//删除已生成的文件及目录
+        //6. 检查文件是否存在,并输出文件
         if (!file_exists($file_dir)) echo '压缩包文件不存在';
 
         ob_clean();
@@ -892,8 +892,8 @@ if (!function_exists('excelSaveBatch')) {
         header("Content-Type: application/octet-stream");                           # 流文件输出
         header("Content-Transfer-Encoding: binary");                                # 告诉浏览器,这是二进制文件
         header('Content-Length: ' . filesize($file_dir));                           # 告诉浏览器,文件大小
-        readfile($file_dir);                                                        # 输出文件
-        @ unlink($file_dir);
+        readfile($file_dir);//输出文件
+        @unlink($file_dir);
         exit();
     }
 }