| | |
| | | package com.moral.api.controller; |
| | | |
| | | import cn.afterturn.easypoi.entity.ImageEntity; |
| | | import cn.afterturn.easypoi.word.WordExportUtil; |
| | | import com.alibaba.excel.EasyExcel; |
| | | import com.alibaba.excel.ExcelWriter; |
| | |
| | | import org.apache.commons.collections4.CollectionUtils; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.apache.poi.xwpf.usermodel.XWPFDocument; |
| | | import org.jfree.chart.ChartFactory; |
| | | import org.jfree.chart.ChartUtils; |
| | | import org.jfree.chart.JFreeChart; |
| | | import org.jfree.chart.StandardChartTheme; |
| | | import org.jfree.chart.labels.StandardPieSectionLabelGenerator; |
| | | import org.jfree.chart.plot.PiePlot; |
| | | import org.jfree.data.general.DefaultPieDataset; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.util.Assert; |
| | | import org.springframework.util.ResourceUtils; |
| | | import org.springframework.web.bind.annotation.GetMapping; |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.File; |
| | | import java.io.FileOutputStream; |
| | | import java.io.OutputStream; |
| | | import java.awt.*; |
| | | import java.io.*; |
| | | import java.lang.reflect.Field; |
| | | import java.net.URLEncoder; |
| | | import java.util.*; |
| | | import java.util.List; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | |
| | | } |
| | | } |
| | | |
| | | @GetMapping("resultWordMonth") |
| | | public void resultWordMonth(HttpServletResponse response) { |
| | | String ks = "2020-06-01"; |
| | | Date ksDate = DateUtils.convertDate(ks); |
| | | |
| | | Date jsDate = DateUtils.getLastDayOfCurrMonth(ksDate);; |
| | | 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,1,time1+"-"+endTime1); |
| | | downloadWord(fileName, map, response,"空气质量周报"+time+".docx"); |
| | | //ksDate = DateUtils.addDays(ksDate,7); |
| | | |
| | | } |
| | | |
| | | public static void downloadWord(String fileName, Map<String, Object> params, HttpServletResponse response,String newFileName) { |
| | | String path = getPath(fileName);//我这放 resources包下 |
| | |
| | | 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); |
| | | // String bodyName = stringButterStrWeek(listAll); |
| | | HashMap<String, Integer> datas = new HashMap<>(3); |
| | | datas.put("一号",10); |
| | | datas.put("二号",20); |
| | | datas.put("三号",40); |
| | | ImageEntity imageEntity = pieChart("测试",datas, 500, 300); |
| | | resultMap.put("主要内容",imageEntity); |
| | | return resultMap; |
| | | } |
| | | private String stringButterStr(List<SysTest> listAll){ |
| | |
| | | return map; |
| | | |
| | | } |
| | | private static String tempImgPath="E:\\home\\image\\tempJfree.jpg"; |
| | | private static byte[] imgToByte(){ |
| | | File file = new File(tempImgPath); |
| | | byte[] buffer = null; |
| | | try { |
| | | FileInputStream fis = new FileInputStream(file); |
| | | ByteArrayOutputStream bos = new ByteArrayOutputStream(1000); |
| | | byte[] b = new byte[1000]; |
| | | int n; |
| | | while ((n = fis.read(b)) != -1) { |
| | | bos.write(b, 0, n); |
| | | } |
| | | fis.close(); |
| | | bos.close(); |
| | | buffer = bos.toByteArray(); |
| | | } catch (IOException e) { |
| | | log.error(e.getMessage()); |
| | | } |
| | | //删除临时文件 |
| | | //file.delete(); |
| | | return buffer; |
| | | } |
| | | public ImageEntity pieChart(String title, Map<String, Integer> datas, int width, int height) { |
| | | |
| | | //创建主题样式 |
| | | StandardChartTheme standardChartTheme = new StandardChartTheme("CN"); |
| | | //设置标题字体 |
| | | standardChartTheme.setExtraLargeFont(new Font("宋体", Font.BOLD, 20)); |
| | | //设置图例的字体 |
| | | standardChartTheme.setRegularFont(new Font("宋体", Font.PLAIN, 15)); |
| | | //设置轴向的字体 |
| | | standardChartTheme.setLargeFont(new Font("宋体", Font.PLAIN, 15)); |
| | | //设置主题样式 |
| | | ChartFactory.setChartTheme(standardChartTheme); |
| | | |
| | | //根据jfree生成一个本地饼状图 |
| | | DefaultPieDataset pds = new DefaultPieDataset(); |
| | | datas.forEach(pds::setValue); |
| | | //图标标题、数据集合、是否显示图例标识、是否显示tooltips、是否支持超链接 |
| | | JFreeChart chart = ChartFactory.createPieChart(title, pds, true, false, false); |
| | | //设置抗锯齿 |
| | | chart.setTextAntiAlias(false); |
| | | PiePlot plot = (PiePlot) chart.getPlot(); |
| | | plot.setNoDataMessage("暂无数据"); |
| | | //忽略无值的分类 |
| | | plot.setIgnoreNullValues(true); |
| | | plot.setBackgroundAlpha(0f); |
| | | //设置标签阴影颜色 |
| | | plot.setShadowPaint(new Color(255,255,255)); |
| | | //设置标签生成器(默认{0}) |
| | | plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}({1})/{2}")); |
| | | try { |
| | | ChartUtils.saveChartAsJPEG(new File(tempImgPath), chart, width, height); |
| | | } catch (IOException e1) { |
| | | log.error("生成饼状图失败!"); |
| | | } |
| | | ImageEntity imageEntity = new ImageEntity(imgToByte(), width, height); |
| | | Assert.notNull(imageEntity.getData(),"生成饼状图对象失败!"); |
| | | return imageEntity; |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | /*String ks = "2020-06-01"; |