diff --git a/src/main/java/com/ydool/common/utils/EasyExcelAlibabaUtil.java b/src/main/java/com/ydool/common/utils/EasyExcelAlibabaUtil.java index 5700f7d..50063e2 100644 --- a/src/main/java/com/ydool/common/utils/EasyExcelAlibabaUtil.java +++ b/src/main/java/com/ydool/common/utils/EasyExcelAlibabaUtil.java @@ -4,6 +4,8 @@ import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.IdUtil; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.read.listener.ReadListener; +import com.ydool.staff.excel.ExcelPersonnel; +import com.ydool.staff.request.PersonnelDeleteRequest; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.poi.ss.formula.functions.T; @@ -20,6 +22,7 @@ import java.io.InputStream; import java.net.URLEncoder; import java.util.Date; import java.util.List; +import java.util.Set; /** * 基于EasyExcel的导入导出工具类, 在使用时需要先将excel实体类中的字段使用@ExcelProperty来指定表头 @@ -95,6 +98,25 @@ public class EasyExcelAlibabaUtil { } } + public static void exportDownload(HttpServletResponse response, Class clazz, List list, String excelName, Set param) { + try { + ServletOutputStream outputStream = response.getOutputStream(); + response.setCharacterEncoding("UTF-8"); + response.setHeader("content-Type", "application/vnd.ms-excel"); + response.setHeader("Content-Disposition", + "attachment;filename=" + URLEncoder.encode(excelName, "UTF-8")); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + // 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭 + EasyExcel.write(baos, clazz).includeColumnFiledNames(param).sheet("sheet1") + .doWrite(list); + response.setHeader("Content-Length", String.valueOf(baos.size())); + outputStream.write(baos.toByteArray()); + response.flushBuffer(); + } catch (Exception e) { + logger.error("excelToList-download", e); + } + } + /** * 下载excel * diff --git a/src/main/java/com/ydool/staff/controller/PersonnelController.java b/src/main/java/com/ydool/staff/controller/PersonnelController.java index df3a70a..006e524 100644 --- a/src/main/java/com/ydool/staff/controller/PersonnelController.java +++ b/src/main/java/com/ydool/staff/controller/PersonnelController.java @@ -11,6 +11,7 @@ import com.ydool.staff.dto.PersonnelUpdateDto; import com.ydool.staff.entity.Attachment; import com.ydool.staff.entity.Personnel; import com.ydool.staff.entity.SelectRequest; +import com.ydool.staff.excel.ExcelPersonnel; import com.ydool.staff.request.ChangeRequest; import com.ydool.staff.request.PersonnelDeleteRequest; import com.ydool.staff.request.PersonnelRequest; @@ -87,10 +88,16 @@ public class PersonnelController extends BaseController { return personnelService.updateChange(request); } - @GetMapping("export") + @GetMapping("getEntityParam") + @ApiOperation(value = "获取实体参数名",response = ExcelPersonnel.class) + public AjaxResult getEntityParam(){ + return personnelService.getEntityParam(); + } + + @PostMapping("export") @ApiOperation(value = "导出接口") - public void export(){ - personnelService.export(); + public void export(@RequestBody PersonnelDeleteRequest personnelParam,HttpServletResponse response){ + personnelService.export(personnelParam,response); } /** diff --git a/src/main/java/com/ydool/staff/entity/Personnel.java b/src/main/java/com/ydool/staff/entity/Personnel.java index 4b77a2c..7315213 100644 --- a/src/main/java/com/ydool/staff/entity/Personnel.java +++ b/src/main/java/com/ydool/staff/entity/Personnel.java @@ -1,12 +1,11 @@ package com.ydool.staff.entity; -import java.math.BigDecimal; import com.baomidou.mybatisplus.annotation.TableName; import com.fasterxml.jackson.annotation.JsonFormat; import com.ydool.common.base.BaseEntity; -import java.util.Date; +import java.time.LocalDate; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; @@ -46,7 +45,7 @@ public class Personnel extends BaseEntity { @ApiModelProperty(value = "出生日期") @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8") - private Date birthday; + private LocalDate birthday; @ApiModelProperty(value = "身份证号") private String numberId; @@ -101,23 +100,23 @@ public class Personnel extends BaseEntity { @ApiModelProperty(value = "入职时间") @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8") - private Date hireDate; + private LocalDate hireDate; @ApiModelProperty(value = "退休时间") @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8") - private Date retirementTime; + private LocalDate retirementTime; @ApiModelProperty(value = "离职时间") @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8") - private Date resignationTime; + private LocalDate resignationTime; @ApiModelProperty(value = "退休返聘开始时间") @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8") - private Date txfpStartTime; + private LocalDate txfpStartTime; @ApiModelProperty(value = "退休返聘结束时间") @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8") - private Date txfpEndTime; + private LocalDate txfpEndTime; @ApiModelProperty(value = "人员招聘单号") private String recruitNumber; diff --git a/src/main/java/com/ydool/staff/excel/ExcelPersonnel.java b/src/main/java/com/ydool/staff/excel/ExcelPersonnel.java index 19bf6d5..e5ef9fc 100644 --- a/src/main/java/com/ydool/staff/excel/ExcelPersonnel.java +++ b/src/main/java/com/ydool/staff/excel/ExcelPersonnel.java @@ -6,6 +6,8 @@ import com.alibaba.excel.annotation.write.style.ContentStyle; import com.alibaba.excel.annotation.write.style.HeadRowHeight; import com.alibaba.excel.annotation.write.style.HeadStyle; import com.ydool.common.converter.EasyExcelLocalDateConverter; +import io.swagger.annotations.ApiModelProperty; +import io.swagger.annotations.ApiModelProperty; import lombok.Data; import org.apache.poi.ss.usermodel.HorizontalAlignment; import org.apache.poi.ss.usermodel.VerticalAlignment; @@ -24,28 +26,169 @@ import java.time.LocalDate; @ContentStyle(verticalAlignment = VerticalAlignment.CENTER) public class ExcelPersonnel { + @ExcelProperty(value = "统一社会信用代码" ) + @ApiModelProperty(value = "统一社会信用代码") + private String socialCreditCode; + + @ExcelProperty("姓名") + @ApiModelProperty(value = "姓名") private String userName; - @ColumnWidth(40) - @ExcelProperty("身份证号") - private String numberId; @ExcelProperty("企业名称") + @ApiModelProperty(value = "企业名称") private String companyName; + + @ExcelProperty("性别") + @ApiModelProperty(value = "性别") + private String gender; + + + @ExcelProperty(value = "出生日期" , converter = EasyExcelLocalDateConverter.class) + @ApiModelProperty(value = "出生日期") + private LocalDate birthday; + + + @ExcelProperty("身份证号") + @ApiModelProperty(value = "身份证号") + private String numberId; + + + @ExcelProperty("民族") + @ApiModelProperty(value = "民族") + private String nation; + + + @ExcelProperty("政治面貌") + @ApiModelProperty(value = "政治面貌") + private String politicsStatus; + + + @ExcelProperty("学历") + @ApiModelProperty(value = "学历") + private String education; + + + @ExcelProperty("是否应届毕业生") + @ApiModelProperty(value = "是否应届毕业生") + private String freshGraduate; + + + @ExcelProperty("毕业院校") + @ApiModelProperty(value = "毕业院校") + private String academy; + + + @ExcelProperty("专业") + @ApiModelProperty(value = "专业") + private String specialty; + + + @ExcelProperty("录用方式") + @ApiModelProperty(value = "录用方式") + private String hireType; + + + @ExcelProperty("执业资格证书") + @ApiModelProperty(value = "执业资格证书") + private String qualification; + + + @ExcelProperty("专业技术职称") + @ApiModelProperty(value = "专业技术职称") + private String jobTitle; + + @ExcelProperty("所在部门") + @ApiModelProperty(value = "所在部门") private String presentDept; + + @ExcelProperty("岗位职务") + @ApiModelProperty(value = "岗位职务") + private String position; + + + @ExcelProperty("合同形式") + @ApiModelProperty(value = "合同形式") + private String contractModality; + + @ExcelProperty("人员类型") + @ApiModelProperty(value = "人员类型") private String personnelType; - @ExcelProperty(value = "创建时间",converter = EasyExcelLocalDateConverter.class) - private LocalDate createdAt; + + @ExcelProperty("工作状态") + @ApiModelProperty(value = "工作状态") + private String workState; + + + @ExcelProperty("人员性质") + @ApiModelProperty(value = "人员性质") + private String personnelCharacter; + + + @ExcelProperty("年薪") + @ApiModelProperty(value = "年薪") + private Integer annualSalary; + + + @ExcelProperty(value = "入职时间" , converter = EasyExcelLocalDateConverter.class) + @ApiModelProperty(value = "入职时间") + private LocalDate hireDate; + + + @ExcelProperty(value = "退休时间", converter = EasyExcelLocalDateConverter.class) + @ApiModelProperty(value = "退休时间") + private LocalDate retirementTime; + + + @ExcelProperty(value ="离职时间",converter = EasyExcelLocalDateConverter.class) + @ApiModelProperty(value = "离职时间") + private LocalDate resignationTime; + + + @ExcelProperty(value = "退休返聘开始时间",converter = EasyExcelLocalDateConverter.class) + @ApiModelProperty(value = "退休返聘开始时间") + private LocalDate txfpStartTime; + + + @ExcelProperty(value = "退休返聘结束时间",converter = EasyExcelLocalDateConverter.class) + @ApiModelProperty(value = "退休返聘结束时间") + private LocalDate txfpEndTime; + + + @ExcelProperty("人员招聘单号") + @ApiModelProperty(value = "人员招聘单号") + private String recruitNumber; + + + @ExcelProperty("备注") + @ApiModelProperty(value = "备注") + private String remark; + + + @ExcelProperty("初审意见") + @ApiModelProperty(value = "初审意见") + private String firstTrialRemark; + + + @ExcelProperty("终审意见") + @ApiModelProperty(value = "终审意见") + private String finalTrialRemark; + @ExcelProperty("变更标志") + @ApiModelProperty(value = "变更标志") private String alterationSign; + @ExcelProperty("单据状态") + @ApiModelProperty(value = "单据状态") + private String receipts; + } diff --git a/src/main/java/com/ydool/staff/excel/PersonnelExportVO.java b/src/main/java/com/ydool/staff/excel/PersonnelExportVO.java index 8687a30..c887d93 100644 --- a/src/main/java/com/ydool/staff/excel/PersonnelExportVO.java +++ b/src/main/java/com/ydool/staff/excel/PersonnelExportVO.java @@ -14,7 +14,7 @@ import java.time.LocalDate; @NoArgsConstructor public class PersonnelExportVO { - @ExcelProperty("统一社会信用代码") + @ExcelProperty(value = "统一社会信用代码" ) private String socialCreditCode; diff --git a/src/main/java/com/ydool/staff/service/IPersonnelService.java b/src/main/java/com/ydool/staff/service/IPersonnelService.java index fb94b2a..8f2a628 100644 --- a/src/main/java/com/ydool/staff/service/IPersonnelService.java +++ b/src/main/java/com/ydool/staff/service/IPersonnelService.java @@ -60,8 +60,10 @@ public interface IPersonnelService { */ AjaxResult updateChange(ChangeRequest request); - void export(); + void export(PersonnelDeleteRequest param,HttpServletResponse response); AjaxResult importPersonnel(MultipartFile excl); + + AjaxResult getEntityParam(); } diff --git a/src/main/java/com/ydool/staff/service/impl/PersonnelServiceImpl.java b/src/main/java/com/ydool/staff/service/impl/PersonnelServiceImpl.java index 02701d3..5b9ef45 100644 --- a/src/main/java/com/ydool/staff/service/impl/PersonnelServiceImpl.java +++ b/src/main/java/com/ydool/staff/service/impl/PersonnelServiceImpl.java @@ -10,6 +10,7 @@ import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.URLUtil; import cn.hutool.json.JSON; import cn.hutool.json.JSONUtil; +import com.alibaba.excel.EasyExcel; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; @@ -20,6 +21,7 @@ import com.ydool.common.utils.ColumnUtil; import com.ydool.common.utils.EasyExcelAlibabaUtil; import com.ydool.common.utils.ExcelMergeRowByRowUtil; import com.ydool.common.utils.ExcelUtil; +import com.ydool.common.utils.HttpServletUtil; import com.ydool.staff.dto.AttachmentDto; import com.ydool.staff.dto.PersonnelByExchangeDto; import com.ydool.staff.dto.PersonnelDto; @@ -50,10 +52,15 @@ import org.springframework.web.multipart.MultipartFile; import org.thymeleaf.util.StringUtils; import javax.annotation.Resource; +import javax.servlet.http.HttpServletResponse; +import java.io.ByteArrayOutputStream; +import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; +import java.util.HashSet; import java.util.List; +import java.util.Set; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -361,9 +368,10 @@ public class PersonnelServiceImpl extends BaseService list = list(); List personnelList = BeanUtil.copyToList(list, ExcelPersonnel.class); + for (ExcelPersonnel excelPersonnel : personnelList) { CompanyName companyName = companyNameMapper.selectById(excelPersonnel.getCompanyName()); Dept dept = deptMapper.selectById(excelPersonnel.getPresentDept()); @@ -373,12 +381,18 @@ public class PersonnelServiceImpl extends BaseService includeColumnFiledNames = new HashSet<>(); + for (String s : param.getIds()) { + includeColumnFiledNames.add(s); + } + //导出 + EasyExcelAlibabaUtil.exportDownload(response, ExcelPersonnel.class, personnelList, fileName,includeColumnFiledNames); } @Override @@ -413,13 +427,10 @@ public class PersonnelServiceImpl extends BaseService list = new ArrayList<>(); + for (Field declaredField : declaredFields) { + list.add(declaredField.getName()); + } + return AjaxResult.ok().data(list); + } + public PersonnelFailMsgVO checkPersonnelExportVO(PersonnelExportVO personnelExportVO) { PersonnelFailMsgVO failMsgVO = BeanUtil.copyProperties(personnelExportVO, PersonnelFailMsgVO.class); failMsgVO.setFailMsg("");