> 本章介绍如何调用excel导出接口 #### 一、excel接口调用说明: ##### 1、请求路径: ``` ip 端口号(有则写) 项目路径 (有则写) /jmreport/exportAllExcel(固定访问接口路径) 如:http://127.0.0.1:8080/jeecg-boot/jmreport/exportAllExcel ``` ##### 2、需要传递的参数 ``` excelConfigId: 积木报表的id 如图一 token: 如果需要字典的话需要传递,放在请求头 queryParam:{} 参数数组,如{"name":"张三"} 自己api或者sql需要的参数 ``` ![](https://img.kancloud.cn/84/96/8496d80c44f4989d7f69d0d8c9af7969_1065x988.png) :-: 图一 ##### 3、返回参数详解 返回结果为json格式 ``` { code: 200(调用成功),500(调用失败),405(请求方式错误) success: true(调用成功),false(调用失败) message: 失败信息文本内容 result:{ file: base64字符串 name: excel导出名称 }, timestamp:时间戳 } ``` #### 二、后台代码调用示例 ``` /* *导出excel **/ public void exportExcel(HttpServletResponse response){ RestTemplate restTemplate = new RestTemplate(); //创建url路径 String url = "http://192.168.1.107:8080/jeecg-boot/jmreport/exportAllExcel"; Map<String,Object> map = new HashMap<>(); //如需调用请动态传递 map.put("excelConfigId","6df599d933df24de007764d0e98eb105"); HttpHeaders headers = new HttpHeaders(); MediaType type = MediaType.parseMediaType("application/x-www-form-urlencoded;charset=UTF-8"); headers.setContentType(type); //如果有字典需要传token headers.add("token",""); HttpEntity<Map<String,Object>> httpEntity = new HttpEntity<>(map,headers); try { String apiResult = restTemplate.postForObject(url, map, String.class); JSONObject jsonObject = JSONObject.parseObject(apiResult); Object code = jsonObject.get("code"); //判断code是否为空,并且为200 if(null != code && !"".equals(String.valueOf(code)) && "200".equals(String.valueOf(code))){ JSONObject result = jsonObject.getJSONObject("result"); //文件byte64字符串 String file = result.getString("file"); //文件名称 String name = result.getString("name"); //转换成byte byte[] buffer = Base64Utils.decodeFromString(file); name = URLEncoder.encode(name, "UTF-8"); response.setContentType("application/vnd.ms-excel;charset=GBK"); response.setHeader("Content-Disposition", "attachment;filename=" + name); response.getOutputStream().write(buffer); response.getOutputStream().flush(); }else{ log.info("调用失败:"+String.valueOf(message)); } } catch (RestClientException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } ``` ##### 三、前台代码示例 ~~~ let params=JSON.stringify({excelConfigId:"报表id"}) let config={ url:"/jmreport/exportAllExcel", data:params, method:'post' } //有token则传递token config.headers={"Content-Type":"application/json;charset=UTF-8","token":""}; axios.request(config).then((res) => { if (res.data.success){ //进行导出excel,步骤省略.....,代码返回参数见:步骤一第三点返回参数详解 } }) ~~~ ![](https://img.kancloud.cn/23/e7/23e700751ef893d7010333bc1b802fcf_329x240.png) :-: excel导出参数截图