php开发技巧

laravel 数据导出excel压缩处理后上传OS

2022-03-01  本文已影响0人  顶尖少爷

需要使用的扩展:

maatwebsite/excel
zanysoft/laravel-zip
jacobcyl/ali-oss-storage

自行安装配置

https://packagist.org/

controller

public function test(){
 //数据导出吃内存
  ini_set('memory_limit',-1);
 //生成excel
 $date_name=date("YmdHis");
 $file_name = $date_name.".xlsx";
  if(Excel::store(new PaperQuestionOrder(),"public/excel/".$file_name)){
      /压缩文件
            $zip_file_name="storage/excel/".$date_name.".zip";
            $tmpfname= "storage/excel/".$file_name;
            $object = date('Ymd').'/'.$zip_file_name;
            $zip = Zip::create($zip_file_name);
            $zip->add($tmpfname);
            $zip->close();
//文件上传
            $ossClient = new OssClient(env("ALIYUN_ACCESS_ID"), env("ALIYUN_ACCESS_KEY"), env("ALIYUN_ENDPOINT"));
            $res = $ossClient->uploadFile(env("ALIYUN_BUCKET"), $object, $zip_file_name);
            $zip_url_file=env("ALIYUN_CDN_DOMAIN")."/".$object;
            return json_encode(["status"=>"1","msg"=>"ok","excel_url"=>$zip_url_file]);
  }
}
上一篇下一篇

猜你喜欢

热点阅读