diff --git a/common/src/main/java/com/ccsens/common/api/TaskController.java b/common/src/main/java/com/ccsens/common/api/TaskController.java index bbfb2501..d23ef86f 100644 --- a/common/src/main/java/com/ccsens/common/api/TaskController.java +++ b/common/src/main/java/com/ccsens/common/api/TaskController.java @@ -85,6 +85,12 @@ // return JsonResponse.newInstance().ok(); // } // -// +// @MustLogin +// @ApiOperation(value = "导出考勤excel", notes = "") +// @RequestMapping(value = "/export", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"}) +// public JsonResponse export(@ApiParam @Validated @RequestBody QueryDto params) throws Exception{ +// clockingInService.exportRecord(params.getParam(), params.getUserId()); +// return JsonResponse.newInstance().ok(); +// } // //} diff --git a/common/src/main/java/com/ccsens/common/service/ClockingInService.java b/common/src/main/java/com/ccsens/common/service/ClockingInService.java index b9b99cd1..09acce52 100644 --- a/common/src/main/java/com/ccsens/common/service/ClockingInService.java +++ b/common/src/main/java/com/ccsens/common/service/ClockingInService.java @@ -12,14 +12,23 @@ import com.ccsens.common.bean.vo.CClockingInVo; import com.ccsens.common.persist.dao.ProClockingInDao; import com.ccsens.common.persist.dao.ProMemberDao; import com.ccsens.common.util.CommonCodeError; +import com.ccsens.util.PoiUtil; +import com.ccsens.util.PropUtil; +import com.ccsens.util.WebConstant; import com.ccsens.util.exception.BaseException; import lombok.Data; import lombok.extern.slf4j.Slf4j; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.*; @@ -218,12 +227,92 @@ public class ClockingInService implements IClockingInService { } @Override - public CClockingInVo.ExcelUrl exportRecord(CClockingInDto.QueryClockingIn params, Long userId) { + public CClockingInVo.ExcelUrl exportRecord(CClockingInDto.QueryClockingIn params, Long userId) throws IOException { log.info("开始调用查询考勤方法"); List clockingInInfos = queryClockingIn(params, userId); log.info("调用查询考勤方法结束{}",clockingInInfos); + Workbook workbook = new XSSFWorkbook(); + //空白格 + PoiUtil.PoiUtilCell blank = new PoiUtil.PoiUtilCell(); + //excel + List> list = new ArrayList<>(); + //第一行list + List firstCells = new ArrayList<>(); + //第二行list + List secondCells = new ArrayList<>(); + //第一行开始 + PoiUtil.PoiUtilCell poiUtilCell = new PoiUtil.PoiUtilCell(); + poiUtilCell.setValue(""); + poiUtilCell.setColspan(1); + poiUtilCell.setRowspan(2); + firstCells.add(poiUtilCell); + //第二行开始 + secondCells.add(blank); + for (CClockingInVo.ClockingInInfo clockingInInfo : clockingInInfos) { + PoiUtil.PoiUtilCell cellOne = new PoiUtil.PoiUtilCell(); + cellOne.setValue(clockingInInfo.getDateTime()); + cellOne.setColspan(2); + cellOne.setRowspan(1); + firstCells.add(cellOne); + firstCells.add(blank); + PoiUtil.PoiUtilCell cellTwo = new PoiUtil.PoiUtilCell(); + cellTwo.setValue("早"); + secondCells.add(cellTwo); + PoiUtil.PoiUtilCell cellTwo2 = new PoiUtil.PoiUtilCell(); + cellTwo2.setValue("晚"); + secondCells.add(cellTwo2); + } + list.add(firstCells); + list.add(secondCells); + for (CClockingInVo.ClockingInInfo clockingInInfo : clockingInInfos) { + //添加考勤信息 + for (CClockingInVo.ClockRecord clockRecord : clockingInInfo.getRecordList()) { + List memberCells = new ArrayList<>(); + PoiUtil.PoiUtilCell cell = new PoiUtil.PoiUtilCell(); + cell.setValue(clockRecord.getMemberName()); + memberCells.add(cell); + list.add(memberCells); + } + break; + } + + SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss"); + for (CClockingInVo.ClockingInInfo clockingInInfo : clockingInInfos) { + for (int i = 0; i < clockingInInfo.getRecordList().size(); i++) { + List poiUtilCells = list.get(i + 2); + PoiUtil.PoiUtilCell morning = new PoiUtil.PoiUtilCell(); + if (ObjectUtil.isNull(clockingInInfo.getRecordList().get(i).getMorning()) || 0 == clockingInInfo.getRecordList().get(i).getMorning()){ + morning.setValue("未打卡"); + }else{ + morning.setValue(format.format(new Date(clockingInInfo.getRecordList().get(i).getMorning()))); + } + poiUtilCells.add(morning); + PoiUtil.PoiUtilCell night = new PoiUtil.PoiUtilCell(); + if (ObjectUtil.isNull(clockingInInfo.getRecordList().get(i).getNight()) || 0 == clockingInInfo.getRecordList().get(i).getNight()){ + night.setValue("未打卡"); + }else{ + night.setValue(format.format(new Date(clockingInInfo.getRecordList().get(i).getNight()))); + } + poiUtilCells.add(night); + } + } - return null; + //导出操作 + String fileName = cn.hutool.core.date.DateUtil.today() + "/" + System.currentTimeMillis() + ".xlsx"; + String path = PropUtil.path + fileName; + File tmpFile = new File(path); + if (!tmpFile.getParentFile().exists()) { + tmpFile.getParentFile().mkdirs(); + } + + Workbook wbs = PoiUtil.exportWB("Sheet1", list, workbook); + OutputStream stream = new FileOutputStream(tmpFile); + wbs.write(stream); + stream.close(); + CClockingInVo.ExcelUrl excelUrl = new CClockingInVo.ExcelUrl(); + String url = PropUtil.imgDomain+fileName; + excelUrl.setUrl(url); + return excelUrl; } } diff --git a/common/src/main/java/com/ccsens/common/service/IClockingInService.java b/common/src/main/java/com/ccsens/common/service/IClockingInService.java index b5aa4efd..96fead20 100644 --- a/common/src/main/java/com/ccsens/common/service/IClockingInService.java +++ b/common/src/main/java/com/ccsens/common/service/IClockingInService.java @@ -3,6 +3,7 @@ package com.ccsens.common.service; import com.ccsens.common.bean.dto.CClockingInDto; import com.ccsens.common.bean.vo.CClockingInVo; +import java.io.IOException; import java.util.List; /** @@ -38,5 +39,5 @@ public interface IClockingInService { * @param userId 当前用户userId * @return excel的路径 */ - CClockingInVo.ExcelUrl exportRecord(CClockingInDto.QueryClockingIn params,Long userId); + CClockingInVo.ExcelUrl exportRecord(CClockingInDto.QueryClockingIn params,Long userId) throws IOException; } diff --git a/util/src/main/java/com/ccsens/util/PoiUtil.java b/util/src/main/java/com/ccsens/util/PoiUtil.java index 0bd7c4cd..3730ab67 100644 --- a/util/src/main/java/com/ccsens/util/PoiUtil.java +++ b/util/src/main/java/com/ccsens/util/PoiUtil.java @@ -636,33 +636,65 @@ public class PoiUtil { // workbook.write(stream); // stream.close(); - - + //第一行 + PoiUtilCell blank = new PoiUtilCell(); PoiUtilCell poiUtilCell = new PoiUtilCell(); - poiUtilCell.setValue("111222333"); - poiUtilCell.setColspan(2); - poiUtilCell.setRowspan(1); - + poiUtilCell.setValue("空白"); + poiUtilCell.setColspan(1); + poiUtilCell.setRowspan(2); PoiUtilCell poiUtilCell1 = new PoiUtilCell(); - poiUtilCell1.setValue("1112222"); + poiUtilCell1.setValue("2021.8.31"); + poiUtilCell1.setColspan(2); + poiUtilCell1.setRowspan(1); PoiUtilCell poiUtilCell3 = new PoiUtilCell(); - poiUtilCell3.setValue("123"); - - PoiUtilCell poiUtilCell2 = new PoiUtilCell(); - poiUtilCell2.setValue(""); + poiUtilCell3.setValue("2021.9.1"); + poiUtilCell3.setColspan(2); + poiUtilCell3.setRowspan(1); + //第二行 + PoiUtilCell poiUtilCellTwo1 = new PoiUtilCell(); + poiUtilCellTwo1.setValue(""); + poiUtilCellTwo1.setValue(""); + PoiUtilCell poiUtilCellTwo2 = new PoiUtilCell(); + poiUtilCellTwo2.setValue("早"); + PoiUtilCell poiUtilCellTwo3 = new PoiUtilCell(); + poiUtilCellTwo3.setValue("晚"); + PoiUtilCell poiUtilCellTwo4 = new PoiUtilCell(); + poiUtilCellTwo2.setValue("早"); + PoiUtilCell poiUtilCellTwo5 = new PoiUtilCell(); + poiUtilCellTwo3.setValue("晚"); //poiUtilCell2.setFunction("SUM(A1:C1)"); + //第三行 + PoiUtilCell poiUtilCellThree1 = new PoiUtilCell(); + poiUtilCellThree1.setValue("张三"); + PoiUtilCell poiUtilCellThree2 = new PoiUtilCell(); + poiUtilCellThree2.setValue("10:43"); + PoiUtilCell poiUtilCellThree3 = new PoiUtilCell(); + poiUtilCellThree3.setValue("20:00"); List cells = new ArrayList<>(); cells.add(poiUtilCell); cells.add(poiUtilCell1); - cells.add(poiUtilCell1); + cells.add(blank); cells.add(poiUtilCell3); + cells.add(blank); - cells.add(poiUtilCell2); List cells2 = new ArrayList<>(); + cells2.add(poiUtilCellTwo1); + cells2.add(poiUtilCellTwo2); + cells2.add(poiUtilCellTwo3); + cells2.add(poiUtilCellTwo4); + cells2.add(poiUtilCellTwo5); + + List cells3 = new ArrayList<>(); + cells3.add(poiUtilCellThree1); + cells3.add(poiUtilCellThree2); + cells3.add(poiUtilCellThree3); + List> list = new ArrayList<>(); list.add(cells); + list.add(cells2); + list.add(cells3);