| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- <?php
- /**
- * Created by PhpStorm.
- * Name: 芸众商城系统
- * Author: 广州市芸众信息科技有限公司
- * Profile: 广州市芸众信息科技有限公司位于国际商贸中心的广州,专注于移动电子商务生态系统打造,拥有芸众社交电商系统、区块链数字资产管理系统、供应链管理系统、电子合同等产品/服务。官网 :www.yunzmall.com www.yunzshop.com
- * Date: 2021/11/22
- * Time: 11:28
- */
- namespace app\exports;
- use Illuminate\Support\Collection;
- use Maatwebsite\Excel\Facades\Excel;
- class ExcelService
- {
- /**
- * 数组数据导出通用类
- * @param array $exportData 导出数据
- * @param string $fileName 导出文件名
- * @param string|null $fileExt 文件扩展名
- * @param array $headers 下载请求头部信息
- * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
- */
- public static function fromArrayExport($exportData, $fileName, $fileExt = null, $headers =[])
- {
- if (!self::isFileExtension($fileName)) {
- $fileName .= '.'.\Maatwebsite\Excel\Excel::XLSX;
- }
- return Excel::download(new FromArray($exportData),$fileName,$fileExt,$headers)->send();
- }
- /**
- * 自定义数据导出通用类
- * @param object $exportData 导出类
- * @param string $fileName 导出文件名
- * @param string|null $fileExt 文件扩展名
- * @param array $headers 下载请求头部信息
- * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
- */
- public static function customExport($exportModel, $fileName, $fileExt = null, $headers =[])
- {
- if (!self::isFileExtension($fileName)) {
- $fileName .= '.'.\Maatwebsite\Excel\Excel::XLSX;
- }
- return Excel::download($exportModel,$fileName,$fileExt,$headers)->send();
- }
- /**
- * 数据导出并且保存到指定磁盘通用类
- * @param object $exportModel 导出类
- * @param string $fileName 导出文件名
- * @param string $disk 磁盘标识路由 默认 export
- * @param null $fileExt 文件扩展名
- * @param array $diskOptions 磁盘选项
- * @return bool
- */
- public static function storeExport($exportModel, $fileName,$disk = 'export', $fileExt = null, $diskOptions = [])
- {
- if (!self::isFileExtension($fileName)) {
- $fileName .= '.'.\Maatwebsite\Excel\Excel::XLSX;
- }
- return Excel::store($exportModel,$fileName,$disk, $fileExt,$diskOptions);
- }
- /**
- * 导入 返回数组
- * @param mixed $file 文件路径或者文件上传类
- * @return array
- */
- public static function importToArray($file)
- {
- $result = Excel::toArray(new \app\exports\ToArrayModel(),$file);
- $importData = $result?$result:[];
- return $importData;
- }
- /**
- * 导入 返回对象
- * @param mixed $file 文件路径或者文件上传类
- * @return Collection
- */
- public static function importToCollection($file)
- {
- $result = Excel::toCollection(new \app\exports\ToCollectionModel(),$file);
- if ($result instanceof Collection) {
- return $result;
- }
- return new Collection([]);
- }
- /**判断是否有文件后缀名
- * @param $fileName
- * @return bool|string
- */
- public static function isFileExtension($fileName)
- {
- $index = strrpos($fileName, '.');
- if ($index !== false) {
- return substr($fileName, $index + 1);
- }
- return false;
- }
- }
|