ExportService.php 1.6 KB

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