| | |
| | | package com.moral.api.controller; |
| | | |
| | | import cn.afterturn.easypoi.word.WordExportUtil; |
| | | import com.alibaba.excel.EasyExcel; |
| | | import com.alibaba.excel.ExcelWriter; |
| | | import com.alibaba.excel.write.metadata.WriteSheet; |
| | | import com.moral.api.dto.SysTestDTO; |
| | | import com.moral.api.entity.SysTest; |
| | | import com.moral.api.service.SysTestService; |
| | | import com.moral.util.DateUtils; |
| | |
| | | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.File; |
| | | import java.io.FileOutputStream; |
| | | import java.io.OutputStream; |
| | | import java.lang.reflect.Field; |
| | | import java.net.URLEncoder; |
| | |
| | | |
| | | |
| | | @GetMapping("resultWord") |
| | | public void adresultWordd(HttpServletResponse response,String time) { |
| | | List<SysTest> listAll = sysTestService.listAll(time,"2022-06-22",null); |
| | | public void adresultWordd(HttpServletResponse response,String time,String endTime) { |
| | | // for(int i = 495;i<1095;i++){ |
| | | List<SysTest> listAll = sysTestService.listAll(time,endTime,null); |
| | | if(CollectionUtils.isEmpty(listAll)){ |
| | | return; |
| | | } |
| | | /* Date endTime =listAll.get(0).getTime(); |
| | | String endTimeStr = DateUtils.dateToDateString(endTime,DateUtils.yyyyMMdd_EN); |
| | | String fileName = "空气质量日报.docx"; |
| | | Map<String,Object> map = resultMap(listAll); |
| | | downloadWord(fileName, map, response,"空气质量日报"+endTimeStr+".docx");*/ |
| | | // time = DateUtils.dateToDateString(DateUtils.addDays(endTime,1),DateUtils.yyyy_MM_dd_EN); |
| | | // } |
| | | } |
| | | |
| | | @GetMapping("excelM") |
| | | public void excelM(HttpServletResponse response,String time,String endTime) { |
| | | // for(int i = 495;i<1095;i++){ |
| | | String ks = "2020-06-01"; |
| | | Date ksDate = DateUtils.convertDate(ks); |
| | | List<SysTest> result = new ArrayList<>(); |
| | | for(int i = 0;i<37;i++){ |
| | | Date jsDate = DateUtils.getLastDayOfCurrMonth(ksDate); |
| | | List<SysTest> listAll = sysTestService.listAllAvg(DateUtils.dateToDateString(ksDate,DateUtils.yyyy_MM_dd_EN),DateUtils.dateToDateString(jsDate,DateUtils.yyyy_MM_dd_EN),null); |
| | | System.out.println("开始:"+DateUtils.dateToDateString(ksDate,DateUtils.yyyy_MM_dd_EN)); |
| | | System.out.println("结束:"+DateUtils.dateToDateString(jsDate,DateUtils.yyyy_MM_dd_EN)); |
| | | ksDate = DateUtils.addMonths(ksDate,1); |
| | | result.addAll(listAll); |
| | | } |
| | | |
| | | if(CollectionUtils.isEmpty(result)){ |
| | | return; |
| | | } |
| | | String path = "D:\\空气质量月数据.xls"; |
| | | WriteSheet build = EasyExcel.writerSheet("因子信息").head(SysTestDTO.class).build(); |
| | | // 创建 easyexcel 的写出类构造器 参数 告诉构造器,我的excel将来要写到哪里,以及execl中数据是基于哪个java对象模板创建的 |
| | | ExcelWriter excelWriter = EasyExcel.write(path, SysTestDTO.class).build(); |
| | | excelWriter.write(result, build); |
| | | excelWriter.finish(); |
| | | } |
| | | |
| | | @GetMapping("excelDay") |
| | | public void excelDay(HttpServletResponse response,String time,String endTime) { |
| | | |
| | | List<SysTest> listAll = sysTestService.listAll(time,endTime,null); |
| | | |
| | | if(CollectionUtils.isEmpty(listAll)){ |
| | | return; |
| | | } |
| | | Date endTime =listAll.get(0).getTime(); |
| | | String endTimeStr = DateUtils.dateToDateString(endTime,DateUtils.yyyyMMdd_EN); |
| | | String fileName = "空气质量日报.docx"; |
| | | Map<String,Object> map = resultMap(listAll); |
| | | String path = "D:\\空气质量日数据.xls"; |
| | | WriteSheet build = EasyExcel.writerSheet("因子信息").head(SysTestDTO.class).build(); |
| | | // 创建 easyexcel 的写出类构造器 参数 告诉构造器,我的excel将来要写到哪里,以及execl中数据是基于哪个java对象模板创建的 |
| | | ExcelWriter excelWriter = EasyExcel.write(path, SysTestDTO.class).build(); |
| | | excelWriter.write(listAll, build); |
| | | excelWriter.finish(); |
| | | } |
| | | |
| | | //实体转map |
| | | // Map result = JSON.parseObject(JSON.toJSONString(entity), Map.class); |
| | | // Map<String, Object> result = EasyPoiUtil.entityToMap(entity); |
| | | downloadWord(fileName, map, response,"空气质量日报"+endTimeStr+".docx"); |
| | | |
| | | |
| | | |
| | | @GetMapping("resultWordWeek") |
| | | public void resultWordWeek(HttpServletResponse response) { |
| | | String ks = "2020-06-22"; |
| | | Date ksDate = DateUtils.convertDate(ks); |
| | | for(int i = 0;i<157;i++){ |
| | | Date jsDate = DateUtils.addDays(ksDate,6); |
| | | String time = DateUtils.dateToDateString(ksDate,DateUtils.yyyy_MM_dd_EN); |
| | | String endTime = DateUtils.dateToDateString(jsDate,DateUtils.yyyy_MM_dd_EN); |
| | | List<SysTest> listAll = sysTestService.listAllAvg(time,endTime,null); |
| | | if(CollectionUtils.isEmpty(listAll)){ |
| | | return; |
| | | } |
| | | String fileName = "空气质量周报.docx"; |
| | | String time1 = DateUtils.dateToDateString(ksDate,"yyyy.MM.dd"); |
| | | String endTime1= DateUtils.dateToDateString(jsDate,"yyyy.MM.dd"); |
| | | Map<String,Object> map = resultWeekMap(listAll,i+1,time1+"-"+endTime1); |
| | | downloadWord(fileName, map, response,"空气质量周报"+time+".docx"); |
| | | ksDate = DateUtils.addDays(ksDate,7); |
| | | } |
| | | } |
| | | |
| | | |
| | |
| | | String path = getPath(fileName);//我这放 resources包下 |
| | | try { |
| | | //获取模板文档 |
| | | |
| | | OutputStream out = new FileOutputStream("E:\\home\\week\\" + newFileName); |
| | | File rootFile = new File(ResourceUtils.getURL("classpath:").getPath()); |
| | | File file= new File(rootFile, path); |
| | | |
| | | XWPFDocument word = WordExportUtil.exportWord07(file.getPath(), params); |
| | | response.setCharacterEncoding("utf-8"); |
| | | response.setContentType("multipart/form-data"); |
| | | response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(newFileName, "utf-8")); |
| | | OutputStream out = response.getOutputStream(); |
| | | //OutputStream out = response.getOutputStream(); |
| | | word.write(out); |
| | | out.flush(); |
| | | out.close(); |
| | |
| | | resultMap.put(name+"S", StringUtils.isNotEmpty(s.getSu())?s.getSu():"--"); |
| | | } |
| | | |
| | | String bodyName = stringButterStr(listAll); |
| | | //日报 |
| | | String bodyName = stringButterStr(listAll); |
| | | |
| | | resultMap.put("主要内容",bodyName); |
| | | return resultMap; |
| | | } |
| | | |
| | | private Map<String,Object> resultWeekMap(List<SysTest> listAll,int week,String weekTime){ |
| | | Map<String,Object> resultMap = new HashMap<>(); |
| | | resultMap.put("周",week); |
| | | resultMap.put("日期",weekTime); |
| | | String name = "沙"; |
| | | for(SysTest s : listAll){ |
| | | if(s.getName().contains("友")){ |
| | | name = "友"; |
| | | }else if(s.getName().contains("中")){ |
| | | name = "中"; |
| | | }else { |
| | | name = "沙"; |
| | | } |
| | | resultMap.put(name+"AQI", StringUtils.isNotEmpty(s.getAqi())?s.getAqi():"--"); |
| | | resultMap.put(name+"PM10", StringUtils.isNotEmpty(s.getPm10())?s.getPm10():"--"); |
| | | resultMap.put(name+"PM2", StringUtils.isNotEmpty(s.getPm25())?s.getPm25():"--"); |
| | | resultMap.put(name+"SO2", StringUtils.isNotEmpty(s.getSo2())?s.getSo2():"--"); |
| | | resultMap.put(name+"NO2", StringUtils.isNotEmpty(s.getNo2())?s.getNo2():"--"); |
| | | resultMap.put(name+"CO", StringUtils.isNotEmpty(s.getCo())?s.getCo():"--"); |
| | | resultMap.put(name+"O3", StringUtils.isNotEmpty(s.getO3())?s.getO3():"--"); |
| | | resultMap.put(name+"TV", StringUtils.isNotEmpty(s.getVoc())?s.getVoc():"--"); |
| | | resultMap.put(name+"S", StringUtils.isNotEmpty(s.getSu())?s.getSu():"--"); |
| | | } |
| | | String bodyName = stringButterStrWeek(listAll); |
| | | resultMap.put("主要内容",bodyName); |
| | | return resultMap; |
| | | } |
| | | private String stringButterStr(List<SysTest> listAll){ |
| | | |
| | | // 0 一样1 两高 2两低,3高,低 |
| | |
| | | |
| | | } |
| | | |
| | | |
| | | private String stringButterStrWeek(List<SysTest> listAll){ |
| | | StringBuffer stringBuffer = new StringBuffer(); |
| | | SysTest result0 = listAll.get(0); |
| | | SysTest result1 = listAll.get(1); |
| | | SysTest result2 = listAll.get(2); |
| | | Map<String,String> pm10 = nameCode(result0.getName(),result1.getName(),result2.getName(),result0.getPm10(),result1.getPm10(),result2.getPm10()); |
| | | Map<String,String> pm25 = nameCode(result0.getName(),result1.getName(),result2.getName(),result0.getPm25(),result1.getPm25(),result2.getPm25()); |
| | | Map<String,String> so = nameCode(result0.getName(),result1.getName(),result2.getName(),result0.getSo2(),result1.getSo2(),result2.getSo2()); |
| | | Map<String,String> no = nameCode(result0.getName(),result1.getName(),result2.getName(),result0.getNo2(),result1.getNo2(),result2.getNo2()); |
| | | Map<String,String> co = nameCode(result0.getName(),result1.getName(),result2.getName(),result0.getCo(),result1.getCo(),result2.getCo()); |
| | | Map<String,String> o3 = nameCode(result0.getName(),result1.getName(),result2.getName(),result0.getO3(),result1.getO3(),result2.getO3()); |
| | | if(pm10.isEmpty()&& pm25.isEmpty()&&so.isEmpty()&& no.isEmpty()&& co.isEmpty()&& o3.isEmpty()){ |
| | | return stringBuffer.toString(); |
| | | } |
| | | stringBuffer.append("其中:"); |
| | | if(!pm10.isEmpty()){ |
| | | stringBuffer.append("PM10较高的是:"+pm10.get("high")+";较低的是:"+pm10.get("low")+"。"); |
| | | } |
| | | if(!pm25.isEmpty()){ |
| | | stringBuffer.append("PM2.5较高的是:"+pm25.get("high")+";较低的是:"+pm25.get("low")+"。"); |
| | | } |
| | | if(!so.isEmpty()){ |
| | | stringBuffer.append("SO2较高的是:"+so.get("high")+";较低的是:"+so.get("low")+"。"); |
| | | } |
| | | if(!no.isEmpty()){ |
| | | stringBuffer.append("NO2较高的是:"+no.get("high")+";较低的是:"+no.get("low")+"。"); |
| | | } |
| | | if(!co.isEmpty()){ |
| | | stringBuffer.append("CO较高的是:"+co.get("high")+";较低的是:"+co.get("low")+"。"); |
| | | } |
| | | if(!o3.isEmpty()){ |
| | | stringBuffer.append("O3较高的是:"+o3.get("high")+";较低的是:"+o3.get("low")+"。"); |
| | | } |
| | | return stringBuffer.toString(); |
| | | } |
| | | private Map<String,String> nameCode(String name1,String name2,String name3,String nums1,String nums2,String nums3){ |
| | | double num1 = StringUtils.isNotEmpty(nums1)?Double.parseDouble(nums1):0; |
| | | double num2 = StringUtils.isNotEmpty(nums2)?Double.parseDouble(nums2):0; |
| | |
| | | |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | /*String ks = "2020-06-01"; |
| | | Date ksDate = DateUtils.convertDate(ks); |
| | | for(int i = 0;i<37;i++){ |
| | | Date jsDate = DateUtils.getLastDayOfCurrMonth(ksDate); |
| | | System.out.println("开始:"+DateUtils.dateToDateString(ksDate,DateUtils.yyyy_MM_dd_EN)); |
| | | System.out.println("结束:"+DateUtils.dateToDateString(jsDate,DateUtils.yyyy_MM_dd_EN)); |
| | | ksDate = DateUtils.addMonths(ksDate,1); |
| | | }*/ |
| | | |
| | | |
| | | String ks = "2020-06-22"; |
| | | Date ksDate = DateUtils.convertDate(ks); |
| | | for(int i = 0;i<157;i++){ |
| | | Date jsDate = DateUtils.addDays(ksDate,6); |
| | | System.out.println("开始:"+DateUtils.dateToDateString(ksDate,DateUtils.yyyy_MM_dd_EN)); |
| | | System.out.println("结束:"+DateUtils.dateToDateString(jsDate,DateUtils.yyyy_MM_dd_EN)); |
| | | ksDate = DateUtils.addDays(ksDate,7); |
| | | } |
| | | |
| | | |
| | | } |
| | | |
| | | |
| | | } |