ExcelService.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Name: 芸众商城系统
  5. * Author: 广州市芸众信息科技有限公司
  6. * Profile: 广州市芸众信息科技有限公司位于国际商贸中心的广州,专注于移动电子商务生态系统打造,拥有芸众社交电商系统、区块链数字资产管理系统、供应链管理系统、电子合同等产品/服务。官网 :www.yunzmall.com www.yunzshop.com
  7. * Date: 2021/11/22
  8. * Time: 11:28
  9. */
  10. namespace app\exports;
  11. use Illuminate\Support\Collection;
  12. use Maatwebsite\Excel\Facades\Excel;
  13. class ExcelService
  14. {
  15. /**
  16. * 数组数据导出通用类
  17. * @param array $exportData 导出数据
  18. * @param string $fileName 导出文件名
  19. * @param string|null $fileExt 文件扩展名
  20. * @param array $headers 下载请求头部信息
  21. * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
  22. */
  23. public static function fromArrayExport($exportData, $fileName, $fileExt = null, $headers =[])
  24. {
  25. if (!self::isFileExtension($fileName)) {
  26. $fileName .= '.'.\Maatwebsite\Excel\Excel::XLSX;
  27. }
  28. return Excel::download(new FromArray($exportData),$fileName,$fileExt,$headers)->send();
  29. }
  30. /**
  31. * 自定义数据导出通用类
  32. * @param object $exportData 导出类
  33. * @param string $fileName 导出文件名
  34. * @param string|null $fileExt 文件扩展名
  35. * @param array $headers 下载请求头部信息
  36. * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
  37. */
  38. public static function customExport($exportModel, $fileName, $fileExt = null, $headers =[])
  39. {
  40. if (!self::isFileExtension($fileName)) {
  41. $fileName .= '.'.\Maatwebsite\Excel\Excel::XLSX;
  42. }
  43. return Excel::download($exportModel,$fileName,$fileExt,$headers)->send();
  44. }
  45. /**
  46. * 数据导出并且保存到指定磁盘通用类
  47. * @param object $exportModel 导出类
  48. * @param string $fileName 导出文件名
  49. * @param string $disk 磁盘标识路由 默认 export
  50. * @param null $fileExt 文件扩展名
  51. * @param array $diskOptions 磁盘选项
  52. * @return bool
  53. */
  54. public static function storeExport($exportModel, $fileName,$disk = 'export', $fileExt = null, $diskOptions = [])
  55. {
  56. if (!self::isFileExtension($fileName)) {
  57. $fileName .= '.'.\Maatwebsite\Excel\Excel::XLSX;
  58. }
  59. return Excel::store($exportModel,$fileName,$disk, $fileExt,$diskOptions);
  60. }
  61. /**
  62. * 导入 返回数组
  63. * @param mixed $file 文件路径或者文件上传类
  64. * @return array
  65. */
  66. public static function importToArray($file)
  67. {
  68. $result = Excel::toArray(new \app\exports\ToArrayModel(),$file);
  69. $importData = $result?$result:[];
  70. return $importData;
  71. }
  72. /**
  73. * 导入 返回对象
  74. * @param mixed $file 文件路径或者文件上传类
  75. * @return Collection
  76. */
  77. public static function importToCollection($file)
  78. {
  79. $result = Excel::toCollection(new \app\exports\ToCollectionModel(),$file);
  80. if ($result instanceof Collection) {
  81. return $result;
  82. }
  83. return new Collection([]);
  84. }
  85. /**判断是否有文件后缀名
  86. * @param $fileName
  87. * @return bool|string
  88. */
  89. public static function isFileExtension($fileName)
  90. {
  91. $index = strrpos($fileName, '.');
  92. if ($index !== false) {
  93. return substr($fileName, $index + 1);
  94. }
  95. return false;
  96. }
  97. }