实体,每个实体一样,加注解就行。

/*** @author lnxu* @date 2021/12/2 19:13* 就餐次数统计:*/
@Data
@ExcelTarget("mealTimesByCount")
public class MealTimesByCount {@Excel(name = "部门", width = 20,orderNum = "0")private String dept ;@Excel(name = "姓名", width = 20,orderNum = "1")private String name ;@Excel(name = "总数", width = 20,orderNum = "2")private String count ;}

pom.xml添加

<!--excel表格导入导出--><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-base</artifactId><version>3.0.3</version></dependency><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-web</artifactId><version>3.0.3</version></dependency><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-annotation</artifactId><version>3.0.3</version></dependency>

调用方式

package com.tendyron.acs.adaptor.oa.controller.accessExcel;import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.tendyron.acs.adaptor.oa.controller.accessExcel.resultDto.MealTimesByCount;
import com.tendyron.acs.adaptor.oa.controller.accessExcel.resultDto.MealTimesByDay;
import com.tendyron.acs.adaptor.oa.controller.accessExcel.resultDto.MealTimesDetail;
import com.tendyron.acs.adaptor.oa.dao.tdr.repository.AccessLogEntityRepository;
import lombok.extern.slf4j.Slf4j;import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;import java.io.FileOutputStream;
import java.net.InetAddress;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.*;
import java.util.stream.Collectors;/*** @author lnxu* @date 2021/12/2 19:13* 用餐统计*/
@Slf4j
@RestController
@RequestMapping("/excel/")
public class AccessExcelController {@ResourceAccessLogEntityRepository accessLogEntityRepository;@Value("${excel.path}")private String excelPath;@Value("${server.port}")private String port;public String getDateTimeFormatter(LocalDateTime time){DateTimeFormatter dateTimeFormatter =  DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");return dateTimeFormatter.format(time);}public String getStartDateTime(){LocalDateTime now = LocalDateTime.now().plusDays(-10L);return getDateTimeFormatter(LocalDateTime.of(LocalDate.from(now.with(TemporalAdjusters.firstDayOfMonth())), LocalTime.MIN));}public String getEndDateTime(){LocalDateTime now = LocalDateTime.now().plusDays(-10L);return getDateTimeFormatter(LocalDateTime.of(LocalDate.from(now.with(TemporalAdjusters.lastDayOfMonth())), LocalTime.MAX));}public List<MealTimesByCount> getCount(List<Map<String,Object>> mealTimesByCountMap){List<MealTimesByCount> list = new ArrayList<>();mealTimesByCountMap.stream().forEach(m -> {MealTimesByCount  mealTimesByCount= new MealTimesByCount();mealTimesByCount.setCount(m.get("count").toString());mealTimesByCount.setDept(m.get("dept").toString());mealTimesByCount.setName(m.get("name").toString());list.add(mealTimesByCount);});return list;}public List<MealTimesByDay> getDay(List<Map<String,Object>> mealTimesByDayMap){List<MealTimesByDay> mealTimesByDays = mealTimesByDayMap.stream().map(m -> {MealTimesByDay mealTimesByDay = new MealTimesByDay();mealTimesByDay.setDay(LocalDate.parse(m.getOrDefault("day", "").toString()));mealTimesByDay.setCount(m.getOrDefault("count", "").toString());return mealTimesByDay;}).collect(Collectors.toList());return mealTimesByDays;}public List<MealTimesDetail> getDetail(List<Map<String,Object>> mealTimesDetailMap){List<MealTimesDetail> mealTimesDetailList = mealTimesDetailMap.stream().map(m -> {MealTimesDetail mealTimesDetail = new MealTimesDetail();mealTimesDetail.setDept(m.getOrDefault("dept", "").toString());mealTimesDetail.setName(m.getOrDefault("name", "").toString());mealTimesDetail.setOpenTime(m.getOrDefault("openTime", "").toString().substring(0,19));mealTimesDetail.setStaffNo(m.getOrDefault("staffNo", "").toString());return mealTimesDetail;}).collect(Collectors.toList());return mealTimesDetailList;}public String getLastMonth(String title){LocalDate localDate = LocalDate.now().plusDays(-10L);return localDate.toString().substring(0,7)+'月'+title;}public String createExcel(String fileName, Class<?> pojoClass, Collection<?> dataSet){try {ExportParams params = new ExportParams(getLastMonth(fileName), "sheetName1", ExcelType.XSSF);Workbook workbook = ExcelExportUtil.exportExcel(params,pojoClass,dataSet);FileOutputStream fos = new FileOutputStream(excelPath+"/"+getLastMonth(fileName)+".xls");workbook.write(fos);fos.close();return "http://"+InetAddress.getLocalHost().getHostAddress()+":"+port +"/excel/"+getLastMonth(fileName)+".xls";} catch (Exception e) {log.error(e.getMessage());return getLastMonth(fileName)+"下载失败。";}}@PostMapping(value = "/mealTimes")public String createManySheetExcel(){List<Map<String ,Object>> list = new ArrayList<>();List<Map<String,Object>> lunchMealTimesByCount = accessLogEntityRepository.lunchMealTimesByCount(getStartDateTime(),getEndDateTime());List<MealTimesByCount> mealTimesByCountList = getCount(lunchMealTimesByCount);ExportParams lunchMealTimesByCountParams = new ExportParams("中午就餐次数统计", "中午就餐次数统计", ExcelType.XSSF);Map<String,Object> lunchMealTimesByCountMap = new HashMap<>();lunchMealTimesByCountMap.put("title",lunchMealTimesByCountParams);lunchMealTimesByCountMap.put("entity",MealTimesByCount.class);lunchMealTimesByCountMap.put("data",mealTimesByCountList);list.add(lunchMealTimesByCountMap);List<Map<String,Object>> lunchMealTimesByDay = accessLogEntityRepository.lunchMealTimesByDay(getStartDateTime(),getEndDateTime());List<MealTimesByDay> mealTimesByDayList = getDay(lunchMealTimesByDay);ExportParams lunchMealTimesByDayParams = new ExportParams("中午就餐次数按天统计", "中午就餐次数按天统计", ExcelType.XSSF);Map<String,Object> lunchMealTimesByDayMap = new HashMap<>();lunchMealTimesByDayMap.put("title",lunchMealTimesByDayParams);lunchMealTimesByDayMap.put("entity",MealTimesByDay.class);lunchMealTimesByDayMap.put("data",mealTimesByDayList);list.add(lunchMealTimesByDayMap);List<Map<String,Object>> lunchMealTimesDetail = accessLogEntityRepository.lunchMealTimesDetail(getStartDateTime(),getEndDateTime());List<MealTimesDetail> mealTimesDetailList = getDetail(lunchMealTimesDetail);ExportParams lunchMealTimesDetailParams = new ExportParams("中午就餐详情", "中午就餐详情", ExcelType.XSSF);Map<String,Object> lunchMealTimesDetailMap = new HashMap<>();lunchMealTimesDetailMap.put("title",lunchMealTimesDetailParams);lunchMealTimesDetailMap.put("entity",MealTimesDetail.class);lunchMealTimesDetailMap.put("data",mealTimesDetailList);list.add(lunchMealTimesDetailMap);List<Map<String,Object>> dinnerMealTimesByCount = accessLogEntityRepository.dinnerMealTimesByCount(getStartDateTime(),getEndDateTime());List<MealTimesByCount> dinnerMMealTimesByCountList = getCount(dinnerMealTimesByCount);ExportParams dinnerMealTimesByCountParams = new ExportParams("晚餐次数统计", "晚餐次数统计", ExcelType.XSSF);Map<String,Object> dinnerMealTimesByCountMap = new HashMap<>();dinnerMealTimesByCountMap.put("title",dinnerMealTimesByCountParams);dinnerMealTimesByCountMap.put("entity",MealTimesByCount.class);dinnerMealTimesByCountMap.put("data",dinnerMMealTimesByCountList);list.add(dinnerMealTimesByCountMap);List<Map<String,Object>> dinnerMealTimesByDay = accessLogEntityRepository.dinnerMealTimesByDay(getStartDateTime(),getEndDateTime());List<MealTimesByDay> dinnerMealTimesByDayList = getDay(dinnerMealTimesByDay);ExportParams dinnerMealTimesByDayParams = new ExportParams("晚餐次数按天统计", "晚餐次数按天统计", ExcelType.XSSF);Map<String,Object> dinnerMealTimesByDayMap = new HashMap<>();dinnerMealTimesByDayMap.put("title",dinnerMealTimesByDayParams);dinnerMealTimesByDayMap.put("entity",MealTimesByDay.class);dinnerMealTimesByDayMap.put("data",dinnerMealTimesByDayList);list.add(dinnerMealTimesByDayMap);List<Map<String,Object>> dinnerMealTimesDetail = accessLogEntityRepository.dinnerMealTimesDetail(getStartDateTime(),getEndDateTime());List<MealTimesDetail> dinnerMealTimesDetailList = getDetail(dinnerMealTimesDetail);ExportParams dinnerMealTimesDetailParams = new ExportParams("晚餐详情", "晚餐详情", ExcelType.XSSF);Map<String,Object> dinnerMealTimesDetailMap = new HashMap<>();dinnerMealTimesDetailMap.put("title",dinnerMealTimesDetailParams);dinnerMealTimesDetailMap.put("entity",MealTimesDetail.class);dinnerMealTimesDetailMap.put("data",dinnerMealTimesDetailList);list.add(dinnerMealTimesDetailMap);return createManySheetExcel(list);}public String createManySheetExcel(List<Map<String,Object>> list){try {Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.XSSF);FileOutputStream fos = new FileOutputStream(excelPath+"/"+getLastMonth("就餐统计")+".xlsx");workbook.write(fos);fos.close();return "http://"+InetAddress.getLocalHost().getHostAddress()+":"+port +"/excel/"+getLastMonth("就餐统计")+".xlsx";} catch (Exception e) {log.error(e.getMessage());return getLastMonth("就餐统计")+"下载失败。";}}
}

调用的方法

Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.XSSF);FileOutputStream fos = new FileOutputStream(excelPath+"/"+getLastMonth("就餐统计")+".xlsx");workbook.write(fos);fos.close();

springboot+easypoi excel表格多个sheet导出相关推荐

  1. 基于SpringBoot+EasyExcel+vue3实现excel表格的导入和导出

    目录 基于SpringBoot+EasyExcel+vue3实现excel表格的导入和导出 一.导入和导出 二.导出数据为excel实现过程 三.将excel中的数据导入到数据库中 基于SpringB ...

  2. Springboot之Excel表格导出

    Springboot之Excel表格导出 表格导出使用的还是POI,POI的介绍请查看 https://blog.csdn.net/qq_36654629/article/details/901729 ...

  3. SpringBoot下载excel表格

    SpringBoot下载excel表格 git地址:https://gitee.com/benming-walnut/download-excel.git 1.目录结构 2.相关依赖 <pare ...

  4. java excel表格导入_Java实现Excel表格的导入和导出(一)

    多说两句:表格的导入导出,是比较常见的系统操作,一般涉及数据批量导入导出时会用到,以前遇到过的业务场景有批量添加会员信息,数据迁移等.实现工具,Apache的poi最为常见.实现简单,博客简单一记. ...

  5. SpringBoot读取excel表格

    文章目录 SpringBoot读取excel表格 pom.xml依赖 POIUtils工具类 controller测试 注意问题 SpringBoot读取excel表格 共同探讨,向各位大佬学习 走向 ...

  6. 如何读取Excel表格中不同sheet表的同一位置单元格数据,并绘制条形图呢?

    作者 | 黄伟呢 来源 | 数据分析与统计学之美 今天,有位朋友在群里面咨询了一个问题:如何读取Excel表格中"不同sheet表"的同一位置单元格数据,并绘制条形图呢? 有人提议 ...

  7. php操作excel表格的导入和导出

    前言:对于excel大家肯定熟悉不过了的,那么我们在日常的业务中应该是有对这些文件的导入导出操作的 类的下载:composer require phpoffice/phpexcel,其中Classes ...

  8. java io导出excel表格_Java IO 导入导出Excel表格

    1.将excel导入到内存 1. 调用工作簿Workbook的静态方法getWorkbook(),获得工作簿Workbook对象 InputStream in = new FileInputStrea ...

  9. 六.实战——Excel表格的导入和导出

    总结: 导出:写操作,将数据库中的内容写入到excel表格中. 导入:读操作,将excel表格中的内容读出来,插入到数据库中,一般都是先将excel表格中的内容使用流,读到集合(list)中,然后再对 ...

  10. java填充excel表格中_填充导出Java导出excel表格

    近期朋友几篇文章介绍了改填充导出的文章. 关联文章的地址 之前做项目的时候需要数据库导出excel格式,由于项目赶没实现,现在分享下如何用java导出excel.话不多说案例如下: 首先要做的是导入一 ...

最新文章

  1. 日常安排php,PHP日常开发小技巧
  2. java和equals区别_JAVA中==与equals的区别
  3. 在K8S上的Web服务该怎么做域名解析呢?
  4. 数据预处理|关于标准化和归一化的一切
  5. EasyTrack项目管理软件的四大版本和相关组件介绍
  6. win1编辑java环境,在win7下配置java编译环境
  7. 【操作系统】虚拟化CPU、Memory,共享文件
  8. vim学习笔记(4)帮助与配置
  9. Win7系统隐藏文件恢复的方法
  10. 明晚直播预告丨一则ORA-600案例分析
  11. python网页信息_利用python处理网页信息
  12. Java基于WEB的学生考勤管理系统
  13. 网页截长图、全图的方法
  14. tplink错误代码51215_TPLINK路由器设置后访问受限
  15. Android之伪装QQ后台偷偷发短信
  16. 解决炉石传说战网无法更新问题
  17. 内农大计算机学院宿舍,小薇逛农大宿舍,你想看哪栋?
  18. 使用软碟通(UltraISO)刻录Linux系统
  19. 计算机操作系统实训心得总结,计算机操作系统安全实训心得总结
  20. 常见专业术语名词解释(持续更新)

热门文章

  1. mac误删除文件恢复,mac文件丢失如何找回
  2. 计算机网络复习03——数据链路层
  3. MySQLIntegrityConstraintViolationException异常处理
  4. java 对接微信公众号(二)获取关注取关事件/用户回复消息
  5. swiper——AutoPlay
  6. 哪几种MM最受男孩喜欢?(组图)
  7. 遭遇Trojan.PSW.OnlineGames、Trojan.HiJack.a、Trojan.PSW.ZhuXian.b等
  8. 华为云Debina登录界面输入正确密码却显示认证失败
  9. 通过C2progv1.7进行dsp28069串口下载程序
  10. Adobe Photoshop CC 2017图文安装教程,附下载地址