| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- <?php
- namespace App\Services\common;
- use Exception;
- use Illuminate\Http\Response;
- use Illuminate\Support\Facades\Http;
- use App\Traits\ServiceAppAop;
- Class ExportService
- {
- use ServiceAppAop;
- /**
- * @param array $row 表头 一维数组
- * @param array $list 表体 二维数组
- * @param string $fileName 导出Excel文件名
- * @param string $createFormat 导出数据格式化方式,默认非格式化
- * @param array $mergeColumn 需要合并时 合并列 一维数组
- * @param array $mergeRow 需要合并时 合并行 key-val数组 key为合并起点val为终点 例:["1"=>"4"] 第一行到第四行合并为一列
- * @return Response
- * @throws Exception 请求接口返回异常
- */
- public function json(array $row, array $list, string $fileName = '记录',
- string $createFormat = null, array $mergeColumn = [], array $mergeRow = []) :Response
- {
- $request['type'] = 'base';
- $data = ['row'=>$row,'list'=>$list];
- if ($createFormat){
- $data["mergeColumn"] = $mergeColumn;
- if($mergeRow)$data["mergeRow"] = $mergeRow;
- $request["createFormat"] = "merge";
- }
- $request["data"] = json_encode($data,JSON_UNESCAPED_UNICODE);
- $post=Http::post(config('go.export.url'),$request);
- if ($post->status() == 500){
- throw new Exception($post->header("Msg"));
- }
- return response($post,200, [
- "Content-type"=>"application/octet-stream",
- "Content-Disposition"=>"attachment; filename=".$fileName."-".date('ymdHis').'.xlsx',
- ]);
- }
- }
|