这里用的是easypoi
首先引入jar包
<!-- excel -->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>${easypoi.version}</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>${easypoi.version}</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>${easypoi.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.1</version>
</dependency>
2.业务代码
(1)service接口层代码
/**
* 获取导出列表
* @param jsonParam
* @return
* @throws Exception
*/
Map<String,Object> getDownloadAllStaffWorkers(JSONObject jsonParam )throws Exception;
(2)serviceImpl实现类代码
红色部分是因为要先把文件上传到服务器,然后后端获取,然后再返给前端一个URL。一般不用谢
@Override
public Map<String, Object> getDownloadAllStaffWorkers(JSONObject jsonParam) throws Exception {
String workerName = jsonParam.getString("workerName");
String workerCardNum = jsonParam.getString("workerCardNum");
String workerPhone = jsonParam.getString("workerPhone");
Long allStaffWorkerTypeId = jsonParam.getLong("allStaffWorkerTypeId");
String workerCode = jsonParam.getString("workerCode");
Long sysId = jsonParam.getLong("sysId");
String clzName = "员工表";
String sessionToken = jsonParam.getString("sessionToken");
/*
查询导出员工列表
*/
Example example = new Example(AllStaffWorkers.class);
Example.Criteria criteria = example.createCriteria();
//是否有效:1.有效 2.无效
criteria.andEqualTo("isEnabled", ConstantsConfig.IS_ENABLED_YES);
//是否删除:1未删除 2.已删除
criteria.andEqualTo("isDeleted",ConstantsConfig.IS_DELETED_NO);
criteria.andEqualTo("sysId",sysId);
if (!Strings.isNullOrEmpty("workerName")){
criteria.andLike("workerName",workerName);
}
if (!Strings.isNullOrEmpty(workerCardNum)){
criteria.andEqualTo("workerCardNum",workerCardNum);
}
if (!Strings.isNullOrEmpty(workerPhone)){
criteria.andEqualTo("workerPhone",workerPhone);
}
if (!Strings.isNullOrEmpty(String.valueOf(allStaffWorkerTypeId))){
criteria.andEqualTo("allStaffWorkerTypeId",allStaffWorkerTypeId);
}
if (!Strings.isNullOrEmpty(workerCode)){
criteria.andEqualTo("workerCode",workerCode);
}
List<AllStaffWorkers> allStaffWorkersList = allStaffWorkersMapper.selectByExample(example);
//导出数据
HSSFWorkbook workbook = new HSSFWorkbook();
//声明一个表格
HSSFSheet sheet = workbook.createSheet("员工信息表");
//设置表格的默认宽度
sheet.setDefaultColumnWidth((short) 30);
//需要导出的文件名字
String fileName = clzName + ".xls";
int rowNum = 1;
//导出Excel 表格头部
String [] headers = {"姓名","手机号","身份证号","性别","部门代码","归属部门","上级部门","人员类型","岗位/工种","状态"};
HSSFRow row = sheet.createRow(0);
//在Excel中添加表头
for (int i = 0;i<headers.length;i++){
HSSFCell cell = row.createCell(i);
HSSFRichTextString text = new HSSFRichTextString(headers[i]);
cell.setCellValue(text);
}
HSSFRow row1 = null;
for (AllStaffWorkers allStaffWorkers : allStaffWorkersList){
String workerSex = null;
if (allStaffWorkers.getWorkerSex() == 1){
workerSex = "男";
}else if (allStaffWorkers.getWorkerSex() ==2){
workerSex ="女";
}else{
workerSex = "";
}
row1 = sheet.createRow(rowNum);
row1.createCell(0).setCellValue(allStaffWorkers.getWorkerName());
row1.createCell(1).setCellValue(allStaffWorkers.getWorkerPhone());
row1.createCell(2).setCellValue(allStaffWorkers.getWorkerCardNum());
row1.createCell(3).setCellValue(workerSex);
//组织管理表
Long allStaffOrgId = allStaffWorkers.getAllStaffOrgId();
AllStaffOrg allStaffOrg = new AllStaffOrg();
allStaffOrg.setAllStaffOrgId(allStaffOrgId);
allStaffOrg.setIsDeleted(ConstantsConfig.IS_DELETED_NO);
allStaffOrg.setIsEnabled(ConstantsConfig.IS_ENABLED_YES);
AllStaffOrg allStaffOrg1 = allStaffOrgMapper.selectOne(allStaffOrg);
row1.createCell(4).setCellValue(allStaffOrg1.getOrgCode());
row1.createCell(5).setCellValue(allStaffOrg1.getOrgName());
row1.createCell(6).setCellValue(allStaffOrg1.getParentId());
//工种表
allStaffWorkerTypeId = allStaffWorkers.getAllStaffWorkerTypeId();
if (allStaffWorkerTypeId != null){
AllStaffWorkerType allStaffWorkerType = new AllStaffWorkerType();
allStaffWorkerType.setAllStaffWorkerTypeId(allStaffWorkerTypeId);
allStaffWorkerType.setIsDeleted(ConstantsConfig.IS_DELETED_NO);
allStaffWorkerType.setIsEnabled(ConstantsConfig.IS_ENABLED_YES);
AllStaffWorkerType allStaffWorkerType1 = allStaffWorkerTypeMapper.selectOne(allStaffWorkerType);
row1.createCell(7).setCellValue(allStaffWorkerType1.getWorkerTypeName());
row1.createCell(8).setCellValue(allStaffWorkerType1.getWorkerTypeName());
}else {
continue;
}
//字典表
Long dictId = allStaffWorkers.getDictCommId();
if (dictId != null){
DictComm dictComm = new DictComm();
dictComm.setDictId(dictId);
dictComm.setIsDeleted(ConstantsConfig.IS_DELETED_NO);
dictComm.setIsEnabled(ConstantsConfig.IS_ENABLED_YES);
DictComm dictComm1 = dictCommMapper.selectOne(dictComm);
row1.createCell(9).setCellValue(dictComm1.getDictName());
rowNum++;
}else {
continue;
}
}
/*byte[] fileByte = workbook.getBytes();*/
ByteArrayOutputStream os = new ByteArrayOutputStream();
workbook.write(os);
os.toByteArray();
workbook.close();
String httpUrl = null;
String filePath = null;
String url = null;
try {
url = FastDFSUtils.getDoUpload(sessionToken,fileName,os.toByteArray());
}catch (Exception e){
errLog("getDownloadAllStaffWorkers",ErrorCodeMsg.UNKNOWN_ERROR.getMessage(),"获取文件服务下载地址为空");
throw new AttemptException(ErrorCodeMsg.UNKNOWN_ERROR,"获取文件服务下载地址为空");
}
if (StringUtils.isNotEmpty(url)) {
JSONObject jsonObject = JSONObject.parseObject(url);
String data = jsonObject.getString("data");
JSONObject dataObject = JSONObject.parseObject(data);
httpUrl = dataObject.getString("httpUrl");
filePath = dataObject.getString("filePath");
if (StringUtils.isEmpty(filePath)) {
errLog("getDownloadTrainStuEnroll", ErrorCodeMsg.UNKNOWN_ERROR.getMessage(), "获取文件服务下载地址为空2");
throw new AttemptException(ErrorCodeMsg.UNKNOWN_ERROR, "获取文件服务下载地址为空2");
}
}
System.out.println(url);
Map<String,Object> map = new HashMap<>();
map.put("url",fileServer + "/" + filePath);
map.put("attname",fileName);
return map;
}
(3)Controller层代码
/**
* 下载员工
* @param request
* @param jsonParam
* @param requestUser
* @return
* @throws Exception
*/
@PostMapping("/getDownloadAllStaffWorkers")
public ResponseStruct getDownloadAllStaffWorkers(HttpServletRequest request,@RequestBody JSONObject jsonParam,@ModelAttribute RequestUser requestUser)throws Exception{
Long sysId = requestUser.getSysid();
String sessionToken = request.getHeader(ConstantsConfig.ACCESS_TOKEN_KEY);
jsonParam.put("sysId",sysId);
jsonParam.put("sessionToken",sessionToken);
Map<String,Object> map = allStaffWorkersInterface.getDownloadAllStaffWorkers(jsonParam);
return ResponseStruct.success(map);
}
注:因为前端框架是vue架构师考虑到整体文件的管理,所以这里面都要先把文件上传到fastdns服务器然后再进行处理。之前写过简单的。喜欢的点个赞谢谢!