export_page = $export_page; $builder_count = $builder->count(); $this->page_count = ceil($builder_count / $this->page_size); $this->builder_model = $builder->skip(($export_page - 1) * $this->page_size)->take($this->page_size)->get(); } private function swith() { if ($this->page_count > 1) { $this->bigExcel(); } else { return $this->smallExcel(); } } public function getExportBuilder() { return new FromArray($this->export_data); } private function smallExcel() { return app('excel')->download($this->getExportBuilder(),$this->file_name.'.xlsx'); } private function bigExcel() { app('excel')->store($this->getExportBuilder(),$this->file_name.'.xlsx','export'); } public function export($file_name, $export_data, $route = null, $type = 'export') { $this->file_name = $file_name; $this->export_data = $export_data; if ($this->page_count > 1) { $this->bigExcel(); } else { $response = $this->smallExcel(); return $response->send(); } if ($this->export_page == $this->page_count) { setlocale(LC_ALL,'zh_CN.GBK'); if (!file_exists(storage_path('framework/laravel-excel/'))){ mkdir(storage_path('framework/laravel-excel/')); } $filename = storage_path('framework/laravel-excel/' . time() . 'down.zip'); $time = time(); $zip = new \ZipArchive(); // 使用本类,linux需开启zlib,windows需取消php_zip.dll前的注释 if ($zip->open ( $filename, \ZipArchive::CREATE ) !== TRUE) { exit ( '无法打开文件,或者文件创建失败' ); } //$fileNameArr 就是一个存储文件路径的数组 比如 array('/a/1.jpg,/a/2.jpg....'); $fileNameArr = file_tree(storage_path('exports')); foreach ($fileNameArr as $val ) { // 当你使用addFile添加到zip包时,必须确保你添加的文件是存在的,否则close时会返回FALSE,而且使用addFile时,即使文件不存在也会返回TRUE if(file_exists(storage_path('exports/' . basename($val)))){ $zip->addFile (storage_path('exports/') . basename($val), basename($val) ); // 第二个参数是放在压缩包中的文件名称,如果文件可能会有重复,就需要注意一下 } } $zip->close (); // 关闭 foreach ($fileNameArr as $val ) { file_delete(storage_path('exports/' . basename($val))); } //下面是输出下载; if (config('app.framework') == 'platform') { $url = "https://". $_SERVER['HTTP_HOST'].'/storage/framework/laravel-excel/' . $time ."down.zip"; } else { $url = "https://". $_SERVER['HTTP_HOST'].'/addons/yun_shop/storage/framework/laravel-excel/' . $time ."down.zip"; } $backurl = "https://". $_SERVER['HTTP_HOST']. config('app.isWeb') . "?c=site&a=entry&m=yun_shop&do=4302&route=" . $route; echo '