ExportService.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace App\Services\common;
  3. use Exception;
  4. use Illuminate\Http\Response;
  5. use Illuminate\Support\Facades\Http;
  6. use App\Traits\ServiceAppAop;
  7. Class ExportService
  8. {
  9. use ServiceAppAop;
  10. /**
  11. * @param array $row 表头 一维数组
  12. * @param array $list 表体 二维数组
  13. * @param string $fileName 导出Excel文件名
  14. * @param string $createFormat 导出数据格式化方式,默认非格式化
  15. * @param array $mergeColumn 需要合并时 合并列 一维数组
  16. * @param array $mergeRow 需要合并时 合并行 key-val数组 key为合并起点val为终点 例:["1"=>"4"] 第一行到第四行合并为一列
  17. * @return Response
  18. * @throws Exception 请求接口返回异常
  19. */
  20. public function json(array $row, array $list, string $fileName = '记录',
  21. string $createFormat = null, array $mergeColumn = [], array $mergeRow = []) :Response
  22. {
  23. $request['type'] = 'base';
  24. $data = ['row'=>$row,'list'=>$list];
  25. if ($createFormat){
  26. $data["mergeColumn"] = $mergeColumn;
  27. if($mergeRow)$data["mergeRow"] = $mergeRow;
  28. $request["createFormat"] = "merge";
  29. }
  30. $request["data"] = json_encode($data,JSON_UNESCAPED_UNICODE);
  31. $post=Http::post(config('go.export.url'),$request);
  32. if ($post->status() == 500){
  33. throw new Exception($post->header("Msg"));
  34. }
  35. return response($post,200, [
  36. "Content-type"=>"application/octet-stream",
  37. "Content-Disposition"=>"attachment; filename=".$fileName."-".date('ymdHis').'.xlsx',
  38. ]);
  39. }
  40. }