java生成日历数据列表并按日历格式导出到excel

news/2024/10/15 20:17:26/

日历格式输出

  • 日历数据列表
    • 导出封装日历格式
    • 实体类
    • 效果

日历数据列表

java">/**** 封装日历数据* @param year 年份* @param month 月份*/public List<InspectionDailyStaffPlanCalendarData> selectCalendarDataList(int year,int month,List<InspectionDailyStaffPlan> staffPlansList) {//根据日期创建对象,默认当月第一天LocalDate of = LocalDate.of(year, month, 1);//获取当月第一天是周几int valueOfWeek = of.getDayOfWeek().getValue();//判断是否闰年(闰年2月有29天)boolean leapYear = of.isLeapYear();//获取输入月份共有多少天int monthDay = of.getMonth().length(leapYear);List<InspectionDailyStaffPlanCalendarData> list = new ArrayList<>();int weekNum = valueOfWeek;LocalDate today = LocalDate.now(); // 获取今天的日期//打印每个月的日期数for (int i = 1; i <= monthDay; i++) {InspectionDailyStaffPlanCalendarData calendarData = new InspectionDailyStaffPlanCalendarData();calendarData.setDay(i);calendarData.setYear(year);calendarData.setMonth(month);String dateStr = year+"-"+month+"-"+(i<10?"0"+i:i);calendarData.setPlanDate(DateUtils.getDate(dateStr));LocalDate otherDate = LocalDate.of(year, month, i);//判断是否是今日if (today.equals(otherDate)) {calendarData.setToday(1);} else {calendarData.setToday(0);}calendarData.setWeek(WeekEnums.getName(weekNum));if ((i+valueOfWeek-1) % 7 == 0){System.out.println();weekNum = 1;}else{weekNum = weekNum+1;}if(CollUtil.isNotEmpty(staffPlansList)){//封装值班领导和值班人员for (InspectionDailyStaffPlan staffPlan:staffPlansList) {if(staffPlan.getPlanDate().equals(calendarData.getPlanDate())){calendarData.setId(staffPlan.getId());calendarData.setStaffLeaderName(staffPlan.getStaffUserLNames());calendarData.setStaffUserNames(staffPlan.getStaffUserMNames());}}}list.add(calendarData);}return list;}

导出封装日历格式

java">@Override@SneakyThrowspublic void exportCalendarData(InspectionDailyStaffPlan query) {Date planDate = query.getPlanDate();List<InspectionDailyStaffPlanCalendarData> list = selectCalendarDataList(query);if(CollUtil.isEmpty(list)){throw new BusinessException("导出数据为空.");}ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();HttpServletResponse response = requestAttributes.getResponse();response.setContentType("application/vnd.ms-excel");response.setCharacterEncoding("utf-8");// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系String fileName = URLEncoder.encode("每月排班记录" + DateUtil.today(), StandardCharsets.UTF_8.toString());response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");EasyExcel.write(response.getOutputStream()).head(StaffPlanCalendarExcelData.head()).sheet().doWrite(StaffPlanCalendarExcelData.getCalendarData(planDate,list));}/**** 导出表头封装* @param* @return*/public static List<List<String>> head() {List<List<String>> headTitles = ListUtils.newArrayList();headTitles.add(ListUtils.newArrayList("星期一"));headTitles.add(ListUtils.newArrayList("星期二"));headTitles.add(ListUtils.newArrayList("星期三"));headTitles.add(ListUtils.newArrayList("星期四"));headTitles.add(ListUtils.newArrayList("星期五"));headTitles.add(ListUtils.newArrayList("星期六"));headTitles.add(ListUtils.newArrayList("星期日"));return headTitles;}/**** 封装日历数据* dataList 封装好的日历列表*/public static List<List<Object>> getCalendarData(Date planDate, List<InspectionDailyStaffPlanCalendarData> dataList) {LocalDate localDate = planDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();int year = localDate.getYear();int month = localDate.getMonthValue();List<List<Object>> contentList = ListUtils.newArrayList();if (CollUtil.isEmpty(dataList)) {return contentList;}//根据日期创建对象,默认当月第一天LocalDate of = LocalDate.of(year, month, 1);//获取当月第一天是周几int valueOfWeek = of.getDayOfWeek().getValue();List<Object> data = new ArrayList<>();for (int i = 1; i < valueOfWeek; i++) {data.add("");}//判断是否闰年(闰年2月有29天)boolean leapYear = of.isLeapYear();//获取输入月份共有多少天int monthDay = of.getMonth().length(leapYear);//打印每个月的日期数int weekNum = 1;for (int i = 1; i <= monthDay; i++) {if ((i+valueOfWeek-1) % 7 == 0){weekNum++;}}//周int dayOfWeek = 7-valueOfWeek;int val = 0;for (int y = 1; y <= weekNum; y++) {List<Object> obj = new ArrayList<>();for (int i = 1; i <=7; i++) {String str = "";if(dataList.size()>0){InspectionDailyStaffPlanCalendarData calendarData = dataList.get(0);str = "\t\t"+ calendarData.getDay()+"\n";String lNames = StringUtils.isNoneBlank(calendarData.getStaffLeaderName())?"值班领导:"+calendarData.getStaffLeaderName()+"\n":"";String mNames = StringUtils.isNoneBlank(calendarData.getStaffUserNames())?"值班人员:"+calendarData.getStaffUserNames():"";str = "\t\t"+ calendarData.getDay()+"\n"+lNames+mNames;dataList.remove(calendarData);}if(dayOfWeek>=0){dayOfWeek--;data.add(str);}else{obj.add(str);}val++;if ((val+valueOfWeek-1) % 7 == 0){break;}}if(y == 1){contentList.add(data);}else{contentList.add(obj);}}return contentList;}

实体类

java">public class InspectionDailyStaffPlanCalendarData
{private static final long serialVersionUID = 1L;/** 主键id */private Long id;private int year;private int month;private int day;/** 值班计划日期 */@JsonFormat(pattern = "yyyy-MM-dd")private Date planDate;/** 星期 */private String week;/** 值班领导 */private String staffLeaderName;/** 值班人员 */private String staffUserNames;/** 是否今日 1:是 */private int today;}

效果

日历列表图:
在这里插入图片描述
导出数据:
在这里插入图片描述


http://www.ppmy.cn/news/1538285.html

相关文章

[单master节点k8s部署]34.ingress 反向代理(一)

ingress是k8s中的标准API资源&#xff0c;作用是定义外部流量如何进入集群&#xff0c;并根据核心路由规则将流量转发到集群内的服务。 ingress和Istio工作栈中的virtual service都是基于service之上&#xff0c;更细致准确的一种流量规则。每一个pod对应的service是四层代理&…

AI资深导师指导-ChatGPT深度科研工作应用、论文撰写、数据分析及机器学习与AI绘图

2022年11月30日&#xff0c;可能将成为一个改变人类历史的日子——美国人工智能开发机构OpenAI推出了聊天机器人ChatGPT3.5&#xff0c;将人工智能的发展推向了一个新的高度。2023年4月&#xff0c;更强版本的ChatGPT4.0上线&#xff0c;文本、语音、图像等多模态交互方式使其在…

【中短文】区分神经网络中 表征特征、潜层特征、低秩 概念

1. 表征特征&#xff08;Representational Feature&#xff09;&#xff1a; 表征特征通常指的是输入数据经过NN处理就得到的中间表示或输出表示。 这些特征由NN经学习过程自动提取&#xff0c;能更好捕捉输入数据的本质属性。 例如&#xff1a;在图像识别任务中&…

概率 期望与方差

一、期望 1、定义 对随机变量可能取值的加权平均&#xff0c;其中权重是每个可能取值的概率。用E表示&#xff0c;如x是随机变量&#xff0c;则该期望为EX 2、离散型随机变量的期望 对于离散随机变量 X &#xff0c;其可能的取值为 x1,x2,…,xn&#xff0c;对应的概率为 E(X)…

从200台手机到一台电脑的时代到了

大家都知道&#xff0c;现在这个时代呀&#xff01;到处都是网络&#xff0c;社交平台那可老火了。其中TK这个平台&#xff0c;好多人都喜欢在上面玩&#xff0c;也有不少人想借着它来做营销呢。但是啊&#xff0c;在做TK营销的时候&#xff0c;有个账号关联的问题老让人又疼。…

python数据分析与可视化介绍

本文主要讲述了数据可视化的基础知识&#xff0c;包括什么是数据可视化&#xff0c;数据可视化应用以及Python可视化工具库。 什么是数据可视化 可视化是一种通过视觉的方式有效传达信息的技术。数据可视化旨在借助于图形化手段&#xff0c;将数据以视觉形式来呈现&#xff0c…

设计模式——门面模式 | 外观模式

哈喽&#xff0c;各位盆友们&#xff01;我是你们亲爱的学徒小z&#xff0c;今天给大家分享的文章是设计模式的——门面模式。 文章目录 定义通用类图1.通用结构2.优点3.缺点 使用场景注意事项1.一个子系统可以有多个门面2.门面不参与子系统内的业务逻辑 定义 定义&#xff1a;…

vscode播放MP4文件时候没声音

问题描述&#xff1a; vscode 播放MP4文件时候没有声音 原因分析&#xff1a; https://github.com/microsoft/vscode-docs/blob/vnext/release-notes/v1_72.md#built-in-preview-for-some-audio-and-video-files 解决方案&#xff1a; 从上面描述可以看出&#xff0c;大概…