一、为什么老是会有如此nt的需求?
这是之前接到的一个项目需求,我觉得还ex的
为什么会有如此nt的需求,先狠狠吐槽下。如果你没接到,那是因为你的甲方不够恶心。
解决的问题方法:就是你变成甲方
二、什么是单元格策略?
EasyExcel 是java处理表格导入导出的一种工具类,版本为:com.alibaba:easyexcel-core:3.1.1
显而易见,又是阿里巴巴下的一个工具类,贼好用。
依赖的话自己网上看看吧,我这个是公司的maven私服,对于你没有帮助
今天讲的就是WriteCellStyle 类,先说下需求,要求导出表格是根据每年的节假日作为条件,导出表格时通过后端渲染节假日的列。(算是他们查看比较方便吧。
1、准备条件
建了一张Holiday 节假日表,自己维护的,网上在线获取都开始收费了。每年让甲方自己把节假日的数据导到表中。
2、样例代码
public void writerClockDataAll(HttpServletResponse response, AttendanceClock clock) throws IOException {
//总标题
String title = "打卡时间表 统计日期:"+clock.getStartTime()+" 至 "+clock.getEndTime();
//副标题
String title1 = "报表生成时间:"+LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
ClockDataAllVo clockDataAllVo = this.clockDataAll(clock, title, title1);
//需要返回的数据
List<List<String>> lists = clockDataAllVo.getMap().get("data");
// 设置标题
List<List<String>> titleTotalList = clockDataAllVo.getMap().get("title");
LocalDate startDate=clock.getStartTime();
LocalDate endDate=clock.getEndTime();
List<LocalDate> dateList=new ArrayList<>();
LocalDate currentDate=startDate;
while (!currentDate.isAfter(endDate)){
dateList.add(currentDate);
currentDate = currentDate.plus(1, ChronoUnit.DAYS);
}
List<Holiday> holiday= holidayService.selectAndDate(clock.getStartTime(),clock.getEndTime());
//以id为主键转为map
Map<LocalDate, Holiday> map = holiday.stream().collect(Collectors.toMap(Holiday::getDate,Function.identity()));
try{
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
String fileName = URLEncoder.encode("打卡时间表", "UTF-8").replaceAll("\\+", "%20");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
CellWriteHandler cellWriteHandler = new CellWriteHandler(){
CellStyle cellStyle=null;
@Override
public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<WriteCellData<?>> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
int buer=0;//校验值
int i = cell.getColumnIndex();
int rowIndex = cell.getRowIndex();
// if (rowIndex<=5){
if (i>=5){//开始计算是否节假日和星期六、天
//
// 使用DateUtil工具类的week方法获取星期几
ZoneId zone = ZoneId.systemDefault();
Instant instant = dateList.get(i-5).atStartOfDay().atZone(zone).toInstant();
Date da = Date.from(instant);
int weekDay = DateUtil.dayOfWeek(da);
// 打印星期几
System.out.println("日期是星期:" + weekDay);
if (weekDay==7 || weekDay==1){
buer++;
}
if (map.size()!=0){
Holiday holiday1 = map.get(dateList.get(i - 5));
if (holiday1!=null) {
if (!holiday1.getHoliday().equals("调休")){
buer++;
}else {
buer=0;
}
}
}
if (buer>0) {
// 根据单元格获取workbook
Workbook workbook = cell.getSheet().getWorkbook();
System.out.println();
// 单元格策略
WriteCellStyle contentWriteCellStyle = new WriteCellStyle(); contentWriteCellStyle.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex());
cellStyle = StyleUtil.buildCellStyle(workbook, cell.getCellStyle(), contentWriteCellStyle);
cellStyle.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex());
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
//设置当前行第i列的样式
cell.setCellStyle(cellStyle);
}
}
}
};
// 这里需要设置不关闭流
EasyExcel.write(response.getOutputStream()).head(titleTotalList)
//不需要控制是否显示字段
// .includeColumnFieldNames(displayList)
.registerWriteHandler(cellWriteHandler)
.autoCloseStream(Boolean.FALSE)
.sheet(1)
.doWrite(lists);
}catch (Exception e) {
// 重置response
response.reset();
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
Map<String, String> messageMap = new HashMap<String, String>();
messageMap.put("status", "failure");
messageMap.put("message", "下载文件失败" + e.getMessage());
response.getWriter().println(JSON.toJSONString(messageMap));
}
}
以上是整体的方法块,根据自身的需求去修改方法、测试。
核心的地方就是判断 buer>0那块。着重看下
三、总结和总结
我在网上看关于这方面的资料比较少,自己当时也是找的很头疼。算是精雕细琢吧!
文章讲述了在Java项目中如何使用EasyExcel库,特别是WriteCellStyle类,根据每年的节假日自定义单元格样式,以满足导出表格时节假日列的渲染需求。开发者分享了具体的需求背景、代码示例以及遇到的挑战和解决方案。
3974

被折叠的 条评论
为什么被折叠?



