根据参数只导出指定列

This commit is contained in:
mash 2023-02-15 11:42:19 +08:00
parent 669e023adf
commit 039d6429b3
7 changed files with 220 additions and 26 deletions

View File

@ -4,6 +4,8 @@ import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.IdUtil;
import com.alibaba.excel.EasyExcel; import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.ReadListener; 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.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.apache.poi.ss.formula.functions.T; import org.apache.poi.ss.formula.functions.T;
@ -20,6 +22,7 @@ import java.io.InputStream;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Set;
/** /**
* 基于EasyExcel的导入导出工具类 在使用时需要先将excel实体类中的字段使用@ExcelProperty来指定表头 * 基于EasyExcel的导入导出工具类 在使用时需要先将excel实体类中的字段使用@ExcelProperty来指定表头
@ -95,6 +98,25 @@ public class EasyExcelAlibabaUtil {
} }
} }
public static void exportDownload(HttpServletResponse response, Class<?> clazz, List<?> list, String excelName, Set<String> 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 * 下载excel
* *

View File

@ -11,6 +11,7 @@ import com.ydool.staff.dto.PersonnelUpdateDto;
import com.ydool.staff.entity.Attachment; import com.ydool.staff.entity.Attachment;
import com.ydool.staff.entity.Personnel; import com.ydool.staff.entity.Personnel;
import com.ydool.staff.entity.SelectRequest; import com.ydool.staff.entity.SelectRequest;
import com.ydool.staff.excel.ExcelPersonnel;
import com.ydool.staff.request.ChangeRequest; import com.ydool.staff.request.ChangeRequest;
import com.ydool.staff.request.PersonnelDeleteRequest; import com.ydool.staff.request.PersonnelDeleteRequest;
import com.ydool.staff.request.PersonnelRequest; import com.ydool.staff.request.PersonnelRequest;
@ -87,10 +88,16 @@ public class PersonnelController extends BaseController {
return personnelService.updateChange(request); return personnelService.updateChange(request);
} }
@GetMapping("export") @GetMapping("getEntityParam")
@ApiOperation(value = "获取实体参数名",response = ExcelPersonnel.class)
public AjaxResult getEntityParam(){
return personnelService.getEntityParam();
}
@PostMapping("export")
@ApiOperation(value = "导出接口") @ApiOperation(value = "导出接口")
public void export(){ public void export(@RequestBody PersonnelDeleteRequest personnelParam,HttpServletResponse response){
personnelService.export(); personnelService.export(personnelParam,response);
} }
/** /**

View File

@ -1,12 +1,11 @@
package com.ydool.staff.entity; package com.ydool.staff.entity;
import java.math.BigDecimal;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonFormat;
import com.ydool.common.base.BaseEntity; import com.ydool.common.base.BaseEntity;
import java.util.Date; import java.time.LocalDate;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
@ -46,7 +45,7 @@ public class Personnel extends BaseEntity {
@ApiModelProperty(value = "出生日期") @ApiModelProperty(value = "出生日期")
@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8") @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
private Date birthday; private LocalDate birthday;
@ApiModelProperty(value = "身份证号") @ApiModelProperty(value = "身份证号")
private String numberId; private String numberId;
@ -101,23 +100,23 @@ public class Personnel extends BaseEntity {
@ApiModelProperty(value = "入职时间") @ApiModelProperty(value = "入职时间")
@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8") @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
private Date hireDate; private LocalDate hireDate;
@ApiModelProperty(value = "退休时间") @ApiModelProperty(value = "退休时间")
@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8") @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
private Date retirementTime; private LocalDate retirementTime;
@ApiModelProperty(value = "离职时间") @ApiModelProperty(value = "离职时间")
@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8") @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
private Date resignationTime; private LocalDate resignationTime;
@ApiModelProperty(value = "退休返聘开始时间") @ApiModelProperty(value = "退休返聘开始时间")
@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8") @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
private Date txfpStartTime; private LocalDate txfpStartTime;
@ApiModelProperty(value = "退休返聘结束时间") @ApiModelProperty(value = "退休返聘结束时间")
@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8") @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
private Date txfpEndTime; private LocalDate txfpEndTime;
@ApiModelProperty(value = "人员招聘单号") @ApiModelProperty(value = "人员招聘单号")
private String recruitNumber; private String recruitNumber;

View File

@ -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.HeadRowHeight;
import com.alibaba.excel.annotation.write.style.HeadStyle; import com.alibaba.excel.annotation.write.style.HeadStyle;
import com.ydool.common.converter.EasyExcelLocalDateConverter; import com.ydool.common.converter.EasyExcelLocalDateConverter;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
import org.apache.poi.ss.usermodel.HorizontalAlignment; import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment; import org.apache.poi.ss.usermodel.VerticalAlignment;
@ -24,28 +26,169 @@ import java.time.LocalDate;
@ContentStyle(verticalAlignment = VerticalAlignment.CENTER) @ContentStyle(verticalAlignment = VerticalAlignment.CENTER)
public class ExcelPersonnel { public class ExcelPersonnel {
@ExcelProperty(value = "统一社会信用代码" )
@ApiModelProperty(value = "统一社会信用代码")
private String socialCreditCode;
@ExcelProperty("姓名") @ExcelProperty("姓名")
@ApiModelProperty(value = "姓名")
private String userName; private String userName;
@ColumnWidth(40)
@ExcelProperty("身份证号")
private String numberId;
@ExcelProperty("企业名称") @ExcelProperty("企业名称")
@ApiModelProperty(value = "企业名称")
private String companyName; 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("所在部门") @ExcelProperty("所在部门")
@ApiModelProperty(value = "所在部门")
private String presentDept; private String presentDept;
@ExcelProperty("岗位职务")
@ApiModelProperty(value = "岗位职务")
private String position;
@ExcelProperty("合同形式")
@ApiModelProperty(value = "合同形式")
private String contractModality;
@ExcelProperty("人员类型") @ExcelProperty("人员类型")
@ApiModelProperty(value = "人员类型")
private String personnelType; 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("变更标志") @ExcelProperty("变更标志")
@ApiModelProperty(value = "变更标志")
private String alterationSign; private String alterationSign;
@ExcelProperty("单据状态")
@ApiModelProperty(value = "单据状态")
private String receipts;
} }

View File

@ -14,7 +14,7 @@ import java.time.LocalDate;
@NoArgsConstructor @NoArgsConstructor
public class PersonnelExportVO { public class PersonnelExportVO {
@ExcelProperty("统一社会信用代码") @ExcelProperty(value = "统一社会信用代码" )
private String socialCreditCode; private String socialCreditCode;

View File

@ -60,8 +60,10 @@ public interface IPersonnelService {
*/ */
AjaxResult updateChange(ChangeRequest request); AjaxResult updateChange(ChangeRequest request);
void export(); void export(PersonnelDeleteRequest param,HttpServletResponse response);
AjaxResult importPersonnel(MultipartFile excl); AjaxResult importPersonnel(MultipartFile excl);
AjaxResult getEntityParam();
} }

View File

@ -10,6 +10,7 @@ import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil; import cn.hutool.core.util.URLUtil;
import cn.hutool.json.JSON; import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil; 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.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; 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.EasyExcelAlibabaUtil;
import com.ydool.common.utils.ExcelMergeRowByRowUtil; import com.ydool.common.utils.ExcelMergeRowByRowUtil;
import com.ydool.common.utils.ExcelUtil; import com.ydool.common.utils.ExcelUtil;
import com.ydool.common.utils.HttpServletUtil;
import com.ydool.staff.dto.AttachmentDto; import com.ydool.staff.dto.AttachmentDto;
import com.ydool.staff.dto.PersonnelByExchangeDto; import com.ydool.staff.dto.PersonnelByExchangeDto;
import com.ydool.staff.dto.PersonnelDto; import com.ydool.staff.dto.PersonnelDto;
@ -50,10 +52,15 @@ import org.springframework.web.multipart.MultipartFile;
import org.thymeleaf.util.StringUtils; import org.thymeleaf.util.StringUtils;
import javax.annotation.Resource; 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.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -361,9 +368,10 @@ public class PersonnelServiceImpl extends BaseService<PersonnelMapper, Personnel
return wrapper; return wrapper;
} }
public void export() { public void export(PersonnelDeleteRequest param ,HttpServletResponse response) {
List<Personnel> list = list(); List<Personnel> list = list();
List<ExcelPersonnel> personnelList = BeanUtil.copyToList(list, ExcelPersonnel.class); List<ExcelPersonnel> personnelList = BeanUtil.copyToList(list, ExcelPersonnel.class);
for (ExcelPersonnel excelPersonnel : personnelList) { for (ExcelPersonnel excelPersonnel : personnelList) {
CompanyName companyName = companyNameMapper.selectById(excelPersonnel.getCompanyName()); CompanyName companyName = companyNameMapper.selectById(excelPersonnel.getCompanyName());
Dept dept = deptMapper.selectById(excelPersonnel.getPresentDept()); Dept dept = deptMapper.selectById(excelPersonnel.getPresentDept());
@ -373,12 +381,18 @@ public class PersonnelServiceImpl extends BaseService<PersonnelMapper, Personnel
if (ObjectUtil.isNotEmpty(dept)){ if (ObjectUtil.isNotEmpty(dept)){
excelPersonnel.setPresentDept(dept.getName()); excelPersonnel.setPresentDept(dept.getName());
} }
excelPersonnel.setGender("1".equals(excelPersonnel.getGender())?"":"");
excelPersonnel.setFreshGraduate("1".equals(excelPersonnel.getFreshGraduate())?"":"");
} }
int[] mergeColumeIndex = {0};//需要合并的列
int mergeRowIndex = 100;// 从那一行开始合并 String fileName = "企业人员申报" + System.currentTimeMillis() + ".xlsx";
ExcelMergeRowByRowUtil excelMergeRowByRowStrategy = new ExcelMergeRowByRowUtil(mergeRowIndex,mergeColumeIndex,mergeColumeIndex[0],personnelList.size()); Set<String> includeColumnFiledNames = new HashSet<>();
ExcelUtil.export(URLUtil.encode(DateUtil.format(new Date(),"yyyyMMdd")+"人员表"), ExcelPersonnel.class,excelMergeRowByRowStrategy,"sheet1",personnelList); for (String s : param.getIds()) {
includeColumnFiledNames.add(s);
}
//导出
EasyExcelAlibabaUtil.exportDownload(response, ExcelPersonnel.class, personnelList, fileName,includeColumnFiledNames);
} }
@Override @Override
@ -413,13 +427,10 @@ public class PersonnelServiceImpl extends BaseService<PersonnelMapper, Personnel
PersonnelFailMsgVO failMsgVO = checkPersonnelExportVO(personnelExportVO); PersonnelFailMsgVO failMsgVO = checkPersonnelExportVO(personnelExportVO);
//错误数据 不保存 //错误数据 不保存
if (StrUtil.isNotBlank(failMsgVO.getFailMsg())) { if (StrUtil.isNotBlank(failMsgVO.getFailMsg())) {
System.out.println("2222222222222 = ");
failList.add(failMsgVO); failList.add(failMsgVO);
} else { } else {
System.out.println(" 111111111 ");
//转义 //转义
Personnel personnel = interpretPersonnelExportVO(personnelExportVO); Personnel personnel = interpretPersonnelExportVO(personnelExportVO);
System.out.println("personnel = " + personnel);
//保存 //保存
failMsgVO.setFailMsg(failMsgVO.getFailMsg() + (save(personnel) ? "导入成功" : "导入失败")); failMsgVO.setFailMsg(failMsgVO.getFailMsg() + (save(personnel) ? "导入成功" : "导入失败"));
failList.add(failMsgVO); failList.add(failMsgVO);
@ -444,6 +455,16 @@ public class PersonnelServiceImpl extends BaseService<PersonnelMapper, Personnel
return AjaxResult.ok().data(download).msg("导入完成"); return AjaxResult.ok().data(download).msg("导入完成");
} }
@Override
public AjaxResult getEntityParam() {
Field[] declaredFields = ExcelPersonnel.class.getDeclaredFields();
List<String> list = new ArrayList<>();
for (Field declaredField : declaredFields) {
list.add(declaredField.getName());
}
return AjaxResult.ok().data(list);
}
public PersonnelFailMsgVO checkPersonnelExportVO(PersonnelExportVO personnelExportVO) { public PersonnelFailMsgVO checkPersonnelExportVO(PersonnelExportVO personnelExportVO) {
PersonnelFailMsgVO failMsgVO = BeanUtil.copyProperties(personnelExportVO, PersonnelFailMsgVO.class); PersonnelFailMsgVO failMsgVO = BeanUtil.copyProperties(personnelExportVO, PersonnelFailMsgVO.class);
failMsgVO.setFailMsg(""); failMsgVO.setFailMsg("");