From 22ce13af4a1532a5253fc4ffdf5b8e69629a3a0a Mon Sep 17 00:00:00 2001 From: jinpengyong <jpy123456> Date: Tue, 28 Apr 2020 17:28:31 +0800 Subject: [PATCH] 报表导出 --- src/main/java/com/moral/controller/ReportController.java | 101 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 97 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/moral/controller/ReportController.java b/src/main/java/com/moral/controller/ReportController.java index b4ef1d7..93927f8 100644 --- a/src/main/java/com/moral/controller/ReportController.java +++ b/src/main/java/com/moral/controller/ReportController.java @@ -1,10 +1,8 @@ package com.moral.controller; import java.io.OutputStream; -import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; +import java.text.SimpleDateFormat; +import java.util.*; import javax.annotation.Resource; import javax.servlet.ServletOutputStream; @@ -90,6 +88,101 @@ return new ResultBean<Boolean>(true); } + @GetMapping("newExcel") + public ResultBean<Boolean> getNExcelReport(HttpServletRequest request, HttpServletResponse response) throws Exception { + Map<String, Object> parameters = getParametersStartingWith(request, null); + List<Map<String, Object>> list = historyMinutelyService.getMonitorPointOrDeviceAvgData(parameters); + String type = parameters.get("type").toString(); + String time = parameters.get("time").toString(); + String timeb = parameters.get("timeb").toString(); + String[] endTimes = timeb.split("-"); + String dateFormat = ""; + String beginTime = ""; + String endTime = ""; + List<String> monthList1 = new ArrayList<>(); + monthList1.add("01"); + monthList1.add("03"); + monthList1.add("05"); + monthList1.add("07"); + monthList1.add("08"); + monthList1.add("10"); + monthList1.add("12"); + List<String> monthList2 = new ArrayList<>(); + monthList2.add("04"); + monthList2.add("06"); + monthList2.add("09"); + monthList2.add("11"); + Integer year = Integer.valueOf(endTimes[0]); + int i = 0; + + if (type.equals("year")) { + dateFormat = "yyyy-MM"; + beginTime = time + "-01"; + endTime = timeb + "-12"; + i = Calendar.MONTH; + } else if (type.equals("month")) { + dateFormat = "yyyy-MM-dd"; + beginTime = time + "-01"; + i = Calendar.DAY_OF_MONTH; + if (monthList1.contains(endTimes[1])) { + endTime = timeb + "-31"; + } else if (monthList2.contains(endTimes[1])) { + endTime = timeb + "-30"; + } else { + if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { + endTime = timeb + "-29"; + } else { + endTime = timeb + "-28"; + } + } + } else if (type.equals("day")) { + dateFormat = "yyyy-MM-dd HH"; + beginTime = time + " 00"; + endTime = timeb + " 23"; + i = Calendar.HOUR_OF_DAY; + } + + SimpleDateFormat sdf = new SimpleDateFormat(dateFormat); + Calendar cal = Calendar.getInstance(); + cal.setTime(sdf.parse(beginTime)); + + ArrayList<Map<String, Object>> resultList = new ArrayList<>(); + for (long d = cal.getTimeInMillis(); d <= sdf.parse(endTime).getTime(); cal.set(i, cal.get(i) + 1), d = cal.getTimeInMillis()) { + String format = sdf.format(d); + Map<String, Object> map = new HashMap<>(); + map.put("time", format); + resultList.add(map); + } + + for (Map<String, Object> map : resultList) { + for (Map<String, Object> resultMap : list) { + if (resultMap.get("time").equals(map.get("time"))) { + map.putAll(resultMap); + } + } + } + + List<String> sensors = (List<String>) parameters.get("sensors");//[e1,e2] + String[][] exportColumn = new String[sensors.size() + 1][]; + exportColumn[0] = new String[]{"������", "20", "time"}; + for ( + int index = 0; index < sensors.size(); index++) { + String[] split = sensors.get(index).split("-"); + String name = split[1]; + String key = split[0]; + String unit = split[2]; + if (!ObjectUtils.isEmpty(unit) && !"null".equals(unit)) { + name += "-" + unit; + } + exportColumn[index + 1] = new String[]{name, "10", key}; + } + + OutputStream outputStream = exportData(response, "Excel������", resultList, exportColumn); + outputStream.flush(); + outputStream.close(); + return new ResultBean<Boolean>(true); + } + @GetMapping("pie") public ResultBean<Map<String, Object>> getPieData(HttpServletRequest request) throws Exception { Map<String, Object> parameters = getParametersStartingWith(request, null); -- Gitblit v1.8.0