| | |
| | | package org.springblade.jfpt.animalheat.controller; |
| | | |
| | | import com.alibaba.excel.EasyExcel; |
| | | import com.alibaba.excel.EasyExcelFactory; |
| | | import com.alibaba.excel.ExcelWriter; |
| | | import com.alibaba.excel.write.metadata.WriteSheet; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport; |
| | | import lombok.AllArgsConstructor; |
| | | import org.apache.commons.codec.Charsets; |
| | | import org.jfree.chart.ChartFactory; |
| | | import org.jfree.chart.ChartUtilities; |
| | | import org.jfree.chart.JFreeChart; |
| | | import org.jfree.chart.labels.StandardPieSectionLabelGenerator; |
| | | import org.jfree.chart.plot.PiePlot; |
| | | import org.jfree.data.general.DefaultPieDataset; |
| | | import org.springblade.common.entity.ReportReturnData; |
| | | import org.springblade.core.excel.util.ExcelUtil; |
| | | import org.springblade.core.mp.support.Condition; |
| | | import org.springblade.core.mp.support.Query; |
| | |
| | | import org.springblade.jfpt.animalheat.entity.BladeAnimalHeat; |
| | | import org.springblade.jfpt.animalheat.service.AnimalHeatService; |
| | | import org.springblade.jfpt.animalheat.vo.AnimalHeatVo; |
| | | import org.springblade.jfpt.pie.ImageData; |
| | | 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.awt.*; |
| | | import java.io.ByteArrayInputStream; |
| | | import java.io.ByteArrayOutputStream; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.net.URLEncoder; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | /** |
| | |
| | | @GetMapping("/export-animalHeat") |
| | | public void exportAnimalHeat(AnimalHeatVo animalHeatVo,HttpServletResponse response){ |
| | | List<AnimalHeatExcel> list = animalHeatService.exportAnimalHeat(animalHeatVo); |
| | | ExcelUtil.export(response, "体温检测数据" + DateUtil.time(), "体温检测数据表", list, AnimalHeatExcel.class); |
| | | ExcelUtil.export(response, "体温检测数据" + DateUtil.time(), "体温检测数据表", list,AnimalHeatExcel.class ); |
| | | } |
| | | |
| | | /** |
| | | * 导出体温数据列表 |
| | | * @param animalHeatVo 条件 |
| | | * @param response 返回域 |
| | | */ |
| | | @GetMapping("/getAnimalHeatStatis") |
| | | public void getAnimalHeatStatis(AnimalHeatVo animalHeatVo,HttpServletResponse response){ |
| | | //获取统计数据 |
| | | List<AnimalHeatExcel> animalHeatExcels = animalHeatService.exportAnimalHeat(animalHeatVo); |
| | | List<ReportReturnData> reportReturnData = animalHeatService.getAnimalHeatPie(animalHeatVo); |
| | | |
| | | //创建jfreechart 画图对象 |
| | | DefaultPieDataset pds = new DefaultPieDataset(); |
| | | |
| | | //遍历统计数据,将数据保存到饼图对象中 |
| | | for (ReportReturnData returnData:reportReturnData) { |
| | | pds.setValue(returnData.getType(),returnData.getCount()); |
| | | } |
| | | |
| | | //声明excelWriter对象 |
| | | ExcelWriter excelWriter = null; |
| | | try { |
| | | // 分别是:显示图表的标题、需要提供对应图表的DateSet对象、是否显示图例、是否生成贴士以及是否生成URL链接 |
| | | JFreeChart chart = ChartFactory.createPieChart("体温数据统计图", pds, false, false, true); |
| | | // 如果不使用Font,中文将显示不出来 |
| | | Font font = new Font("宋体", Font.BOLD, 24); |
| | | // 设置图片标题的字体 |
| | | chart.getTitle().setFont(font); |
| | | // 得到图块,准备设置标签的字体 |
| | | PiePlot plot = (PiePlot) chart.getPlot(); |
| | | // 设置标签字体 |
| | | plot.setLabelFont(font); |
| | | plot.setStartAngle(new Float(3.14f / 2f)); |
| | | // 设置plot的前景色透明度 |
| | | plot.setForegroundAlpha(0.7f); |
| | | // 设置plot的背景色透明度 |
| | | plot.setBackgroundAlpha(0.0f); |
| | | // 设置标签生成器(默认{0}) |
| | | // {0}:key {1}:value {2}:百分比 {3}:sum |
| | | plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}({1}占{2})")); |
| | | ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| | | |
| | | //创建图片数据集合,对象 |
| | | ArrayList<ImageData> imageDatas = new ArrayList<>(); |
| | | ImageData imageData = new ImageData(); |
| | | try { |
| | | // 将图表写入到ByteArrayOutputStream流中 |
| | | ChartUtilities.writeChartAsJPEG(bos, chart, 800, 700); |
| | | //将字节数组输出流写入到输入流中 |
| | | InputStream inputStream = new ByteArrayInputStream(bos.toByteArray()); |
| | | //excel 文件名称 |
| | | String fileName = "体温统计报表" + System.currentTimeMillis() + ".xlsx"; |
| | | //封装数据到图表 |
| | | imageData.setInputStream(inputStream); |
| | | imageDatas.add(imageData); |
| | | |
| | | //设置响应体 |
| | | response.setContentType("application/vnd.ms-excel"); |
| | | response.setCharacterEncoding(Charsets.UTF_8.name()); |
| | | fileName = URLEncoder.encode(fileName, Charsets.UTF_8.name()); |
| | | response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx"); |
| | | //写入 |
| | | excelWriter = EasyExcel.write(response.getOutputStream()).build(); |
| | | |
| | | //shell0 |
| | | WriteSheet writeSheet = EasyExcel.writerSheet(0, "图表").head(ImageData.class).build(); |
| | | excelWriter.write(imageDatas, writeSheet); |
| | | |
| | | //shell1 |
| | | WriteSheet writeSheet0 = EasyExcel.writerSheet(1, "数据列表").head(AnimalHeatExcel.class).build(); |
| | | excelWriter.write(animalHeatExcels, writeSheet0); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | }finally { |
| | | //完成后会关闭流 |
| | | excelWriter.finish(); |
| | | } |
| | | } |
| | | |
| | | } |