首页 > 其他分享 >easypoi基于springboot实现多sheet导出

easypoi基于springboot实现多sheet导出

时间:2023-03-24 23:12:15浏览次数:30  
标签:map sheet springboot excel 报警 List new put easypoi

maven

<dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-spring-boot-starter</artifactId>
            <version>4.4.0</version>
        </dependency>

service

        Map<String,Object> map=new HashMap<>();
        LocalDateTime beginTime=body.getBeginTime();
        LocalDateTime endTime=body.getEndTime();
        if(Objects.isNull(beginTime)&&Objects.isNull(endTime)){
            beginTime=LocalDateTime.of(LocalDate.from(LocalDateTime.now().with(TemporalAdjusters.firstDayOfMonth())), LocalTime.MIN);
            endTime=LocalDateTime.of(LocalDate.from(LocalDateTime.now().with(TemporalAdjusters.lastDayOfMonth())), LocalTime.MAX);
        }
        map.put("beginTime", beginTime);
        map.put("endTime",endTime);
        //报警类型统计
        map.put("groupByColumn","alarm_type");
        List<AlarmStatisticsVo> alarmTypeStatistics=statisticsByAlarmType(map);
        List<ExcelAlarm> excelAlarmList=new ArrayList<>();
        alarmTypeStatistics.stream().forEach(t->{
            ExcelAlarm excelAlarm=new ExcelAlarm();
            excelAlarm.setAlarmTypeName(t.getAlarmTypeName());
            excelAlarm.setAlarmCount(t.getAlarmCount());
            excelAlarmList.add(excelAlarm);
        });
        List<Map<String, Object>> exportData = new ArrayList<>();
        Map<String, Object> excelAlarmMap = WorkBookUtils.createOneSheet("报警类型", "报警类型统计", ExcelAlarm.class, excelAlarmList);
        exportData.add(excelAlarmMap);
        //报警人员类型统计
        map.put("groupByColumn","person_type");
        List<AlarmStatisticsVo> personTypeStatistics=statisticsByPersonType(map);
        List<ExcelPerson> excelPersonList=new ArrayList<>();
        personTypeStatistics.stream().forEach(t->{
            ExcelPerson excelPerson=new ExcelPerson();
            excelPerson.setPersonTypeName(t.getPersonTypeName());
            excelPerson.setAlarmCount(t.getAlarmCount());
            excelPersonList.add(excelPerson);
        });
        Map<String, Object> excelPersonMap = WorkBookUtils.createOneSheet("报警人员统计", "报警人员统计", ExcelPerson.class, excelPersonList);
        exportData.add(excelPersonMap);
        //报警高发区域统计
        map.put("groupByColumn","layer_id");
        map.put("top",Objects.isNull(body.getTop())?6:body.getTop());
        List<ExcelLayer> excelLayerList=new ArrayList<>();
        statisticsByLayerId(map).stream().forEach(t->{
            ExcelLayer excelLayer=new ExcelLayer();
            excelLayer.setLayerId(t.getLayerId());
            excelLayer.setAlarmCount(t.getAlarmCount());
            excelLayerList.add(excelLayer);
        });
        Map<String, Object> excelLayerMap = WorkBookUtils.createOneSheet("高发报警区域统计", "高发报警区域统计", ExcelLayer.class, excelLayerList);
        exportData.add(excelLayerMap);
        //报警数量走势统计
        List<ExcelTrend> excelTrendList=new ArrayList<>();
        alarmMapper.statisticsAlarmByDate(map).stream().forEach(t->{
            ExcelTrend excelTrend=new ExcelTrend();
            excelTrend.setAlarmDate(t.getAlarmDate());
            excelTrend.setAlarmTypeName(t.getAlarmTypeName());
            excelTrend.setAlarmCount(t.getAlarmCount());
            excelTrendList.add(excelTrend);
        });
        Map<String, Object> excelTrendMap = WorkBookUtils.createOneSheet("报警数量走势统计", "报警数量走势统计", ExcelTrend.class, excelTrendList);
        exportData.add(excelTrendMap);
        try {
            Workbook workbook = ExcelExportUtil.exportExcel(exportData, ExcelType.XSSF);
            File savefile = new File("D:/excel/");
            if (!savefile.exists()) {
                savefile.mkdirs();
            }
            FileOutputStream fos = new FileOutputStream("D:/excel/报警统计233.xls");
            workbook.write(fos);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

utils

package com.xrkc.monitor.utils;

import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Auther: wangjiao
 * @Date: 2021-08-03 13:38
 */
public class WorkBookUtils {
    /**
     * 创建sheet
     * @param sheetName sheet名称
     * @param title 表格标题
     * @param clazz 类型
     * @param data 数据
     * @return
     */
    public static Map<String, Object> createOneSheet(String sheetName,String title,Class<?> clazz,List<?> data){
        ExportParams exportParams = new ExportParams(title,sheetName, ExcelType.XSSF);
        Map<String, Object> map = new HashMap<>();
        map.put("title",exportParams);
        map.put("entity", clazz);
        map.put("data",data);
        return map;
    }
}

vo

package com.xrkc.monitor.service.excel;

import cn.afterturn.easypoi.excel.annotation.Excel;

/**
 * @Description:
 * @Auther: wangjiao
 * @Date: 2021-08-03 14:41
 */
public class ExcelAlarm {
    @Excel(name = "报警类型",width = 30)
    private String alarmTypeName;

    @Excel(name = "报警数量", width = 30)
    private Integer alarmCount;

    public String getAlarmTypeName() {
        return alarmTypeName;
    }

    public void setAlarmTypeName(String alarmTypeName) {
        this.alarmTypeName = alarmTypeName;
    }

    public Integer getAlarmCount() {
        return alarmCount;
    }

    public void setAlarmCount(Integer alarmCount) {
        this.alarmCount = alarmCount;
    }
}

如果直接导出到浏览器

 try {
            response.setContentType("application/vnd.ms-excel");
            StringBuilder fileName = new StringBuilder().append("报警统计").append("_")
                    .append(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"))).append(".xls");
            response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode(fileName.toString(), "UTF-8"));
            ServletOutputStream out = response.getOutputStream();
            workbook.write(out);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
            log.error("excel导出报警统计失败:", e.getMessage());
        }

 

标签:map,sheet,springboot,excel,报警,List,new,put,easypoi
From: https://www.cnblogs.com/Ning-Blog/p/17253615.html

相关文章

  • SpringBoot整合elasticsearch实操
    大家好,我是小悟ElasticSearch是一个分布式的开源搜索和分析引擎,适用于所有类型的数据,包括文本、数字、地理空间、结构化和非结构化数据。Elasticsearch以其简单的REST风......
  • SpringBoot 整合第三方登录(微信、支付宝、QQ、Github)
    SpringBoot是一种轻量级的Java框架,可以简化Web应用程序的开发过程。同时,SpringBoot也提供了很多的扩展功能和插件,可以轻松地集成第三方登录功能,如微信、支付宝、QQ等等。......
  • Springboot整合kafka实现高效的消息传递和处理
    Kafka是一个分布式的流处理平台,它可以处理高吞吐量的消息。SpringBoot是一个流行的Java开发框架,提供了快速构建应用程序的能力。将这两者结合起来可以实现高效的消息传递......
  • day12-SpringBoot数据库操作
    SpringBoot数据库操作1.JDBC+HikariDataSource在SpringBoot2.x项目中,默认使用Hikari连接池管理数据源。相比于传统的C3P0、DBCP、Tomcatjdbc等连接池更加优秀。当......
  • SpringBoot中使用FastJson解析Json数据
    场景1.SpringBoot默认配置的是Jackson。实现引入fastJson的依赖<!--fastjson的依赖--><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</art......
  • SpringBoot中定义全局异常处理器
    场景SpringBoot中创建全局异常处理器,使发生异常时能处理。实现在项目下新建handler包,在包下新建GlobalExceptionHnadler.javapackagecom.example.demo.handler;importja......
  • SpringBoot中配置为开发模式,代码修改后不用重新运行
    场景SpringBoot中配置为开发模式,修改代码后不用重新运行。实现打开pom.xml,添加依赖<!--添加如下依赖,配置为开发模式,代码做了修改,不用重新运行--><!--https://mvnrepos......
  • SpringBoot中使用log4j进行日志管理
    场景SpringBoot项目中使用log4j进行日志管理。实现1.因为SpringBoot默认是使用logback,所以要修改pom.xml过滤掉自带的spring-boot-starter-logging,然后添加spring-boot-st......
  • SpringBoot加Jquery实现ajax传递json字符串并回显消息(已实践)
    场景inspinia前端页面模板+thymeleaf模板+jquery+springboot点击提交将当前选中行的id以json字符串传到后台,后台实现状态更改并刷新表格。实现提交按钮的点击事件://提交按......
  • SpringBoot中使用POI实现Excel导入到数据库(图文教程已实践)
    场景IDEA中开发SpringBoot项目时需要将Excel模板中数据导入的到数据库。Excel模板如下实现思想首先将模板上传到服务器中某路径,并将当前路径存储,然后使用POI自带的工具类获......