需要使用的扩展:
maatwebsite/excel
zanysoft/laravel-zip
jacobcyl/ali-oss-storage
自行安装配置
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]);
}
}
网友评论