From 0ab23fe59105dd35b215447789ee5b11ac218380 Mon Sep 17 00:00:00 2001 From: yang_shj <1069818635@QQ.com> Date: Wed, 25 Sep 2024 17:02:16 +0800 Subject: [PATCH] =?UTF-8?q?#=E8=BF=90=E8=A1=8C=E6=8A=A5=E8=A1=A8=E5=AF=BC?= =?UTF-8?q?=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ElectricReportController.java | 8 ++ .../electric/service/IElectricReportService.java | 2 + .../service/impl/ElectricReportServiceImpl.java | 91 ++++++++++++++++++++++ 3 files changed, 101 insertions(+) diff --git a/hzims-service/equipment/src/main/java/com/hnac/hzims/electric/controller/ElectricReportController.java b/hzims-service/equipment/src/main/java/com/hnac/hzims/electric/controller/ElectricReportController.java index 6315535..4bc0f4f 100644 --- a/hzims-service/equipment/src/main/java/com/hnac/hzims/electric/controller/ElectricReportController.java +++ b/hzims-service/equipment/src/main/java/com/hnac/hzims/electric/controller/ElectricReportController.java @@ -68,4 +68,12 @@ public class ElectricReportController extends BladeController { public R> run(@RequestBody RunParamVo param) { return R.data(service.run(param)); } + + @PostMapping("/run/export") + @ApiOperationSupport(order = 6) + @ApiOperation(value = "综合报表导出", notes = "传入 deptId、year") + public void runExport(HttpServletResponse response, + @RequestBody RunParamVo param) { + service.runExport(response,param); + } } \ No newline at end of file diff --git a/hzims-service/equipment/src/main/java/com/hnac/hzims/electric/service/IElectricReportService.java b/hzims-service/equipment/src/main/java/com/hnac/hzims/electric/service/IElectricReportService.java index 978b6fb..2681956 100644 --- a/hzims-service/equipment/src/main/java/com/hnac/hzims/electric/service/IElectricReportService.java +++ b/hzims-service/equipment/src/main/java/com/hnac/hzims/electric/service/IElectricReportService.java @@ -22,4 +22,6 @@ public interface IElectricReportService { void comprehensiveExport(HttpServletResponse response, Long deptId, String year); Map run(RunParamVo param); + + void runExport(HttpServletResponse response, RunParamVo param); } diff --git a/hzims-service/equipment/src/main/java/com/hnac/hzims/electric/service/impl/ElectricReportServiceImpl.java b/hzims-service/equipment/src/main/java/com/hnac/hzims/electric/service/impl/ElectricReportServiceImpl.java index 50ac589..f5cf6ac 100644 --- a/hzims-service/equipment/src/main/java/com/hnac/hzims/electric/service/impl/ElectricReportServiceImpl.java +++ b/hzims-service/equipment/src/main/java/com/hnac/hzims/electric/service/impl/ElectricReportServiceImpl.java @@ -1660,4 +1660,95 @@ public class ElectricReportServiceImpl implements IElectricReportService { table.put("tableDate",tableDates); return table; } + + /** + * 运行报表导出 + * @param response + * @param param + */ + @Override + public void runExport(HttpServletResponse response, RunParamVo param) { + Map runs = this.run(param); + if(MapUtils.isEmpty(runs)){ + throw new ServiceException("暂无可进行导出报表!"); + } + // 创建Excel文件 + HSSFWorkbook hssWB = new HSSFWorkbook(); + // 添加sheet页 + HSSFSheet sheet = hssWB.createSheet("运行报表"); + // 行高 + sheet.setDefaultRowHeight((short) (20 * 25)); + // 列宽 + sheet.setDefaultColumnWidth(15); + // 填充数据 + List tableHeads = JSONObject.parseArray(JSONObject.toJSONString(runs.get("tableHead")),Object.class); + List> tableBodys = JSON.parseObject(JSONObject.toJSONString(runs.get("tableDate")), new TypeReference>>() {}); + // 表头 + this.setRunHeader(hssWB, sheet,tableHeads); + if (!CollectionUtil.isEmpty(tableBodys)) { + // 数据填充 + this.setRunWork(hssWB, sheet, tableBodys); + } + + // 下载导出 + String filename = param.getTime() + "日_运行报表"; + // 设置头信息 + response.setCharacterEncoding("UTF-8"); + response.setContentType("application/vnd.ms-excel"); + ServletOutputStream outputStream; + try { + //设置xlsx格式 + response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename + ".xls", "UTF-8")); + //创建一个输出流 + outputStream = response.getOutputStream(); + //写入数据 + hssWB.write(outputStream); + // 关闭 + outputStream.close(); + hssWB.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + /** + * 运行报表表头 + * @param hssWB + * @param sheet + * @param tableHeads + */ + private void setRunHeader(HSSFWorkbook hssWB, HSSFSheet sheet, List tableHeads) { + // 第一行 + int size = tableHeads.size(); + HSSFRow row = sheet.createRow(0); + // 下标从0开始,所以要减1 + this.setTitileStyle(hssWB, row, size - 1 , true); + for (int i = 0; i < size; i++) { + HSSFCell heade_cell = row.getCell(i); + heade_cell.setCellValue(tableHeads.get(i).toString()); + } + } + + /** + * 运行报表内容 + * @param hssWB + * @param sheet + * @param tableBodys + */ + private void setRunWork(HSSFWorkbook hssWB, HSSFSheet sheet, List> tableBodys) { + // 第一行 + int rowIndex = 1; + // 下标从0开始,所以要减1 + for(List bodys : tableBodys){ + HSSFRow bodyRow = sheet.createRow(rowIndex); + // 下标从0开始,所以要减1 + this.setTitileStyle(hssWB, bodyRow, bodys.size() - 1, false); + // 内容填充 + for(int i = 0; i < bodys.size() ; i++){ + HSSFCell body_cell = bodyRow.getCell(i); + body_cell.setCellValue(bodys.get(i).toString()); + } + rowIndex++; + } + } } \ No newline at end of file