1.Controller
/**
* 自定义导出
*
* @param
* @return
*/
@GetMapping("/export")
public void exportExcel(HttpServletRequest request, HttpServletResponse response, @RequestParam("userIdListStr") String userIdListStr, @RequestParam("fieldNameListStr") String fieldNameListStr) throws Exception {
List<String> userIdList = Arrays.asList(userIdListStr.split(","));
List<String> fieldNameList = Arrays.asList(fieldNameListStr.split(","));
dingRosterService.myExport(request, response, userIdList, fieldNameList);
}
2.service
/**
* 自定义导出
*
* @param userIdList
* @param request
* @param response
* @param fieldNameList
* @return
*/
void myExport(HttpServletRequest request, HttpServletResponse response,List<String> userIdList, List<String> fieldNameList) throws UnsupportedEncodingException;
3.serviceImpl
@Override
public void myExport(HttpServletRequest request, HttpServletResponse response, List<String> userIdList, List<String> fieldNameList) throws UnsupportedEncodingException {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("UTF-8");
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
//在设置response.setHeader时,如果含有中文字符,一定要转换成ISO8859-1格式,否则设置的中文会出现错误。
String excelName = "员工花名册信息自定义导出 " + dateFormat.format(date)+".xls";
response.setHeader("content-disposition", "attachment;filename=" + new String(excelName.getBytes("gb2312"), "ISO8859-1"));
try {
// 这里需要设置不关闭流
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
//设置背景颜色
headWriteCellStyle.setFillForegroundColor(IndexedColors.BLUE_GREY.getIndex());
//内容策略
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
//设置 水平居中
contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
HorizontalCellStyleStrategy horizontalCellStyleStrategy =
new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
EasyExcel.write(response.getOutputStream()).autoCloseStream(Boolean.FALSE)
.registerWriteHandler(new CustomizeColumnWidth()).registerWriteHandler(horizontalCellStyleStrategy)
.head(myriadPeopleHead(fieldNameList)).sheet("模板")
//获取数据填充,按顺序匹配
.doWrite(getMyriadPeopleReportData(userIdList, fieldNameList));
} catch (Exception e) {
// 重置response
response.reset();
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
Map<String, String> map = new HashMap<String, String>();
map.put("status", "failure");
map.put("message", "下载文件失败" + e.getMessage());
try {
response.getWriter().println(JSON.toJSONString(map));
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
private List getMyriadPeopleReportData(List<String> userIdList, List<String> fieldNameList) {
List<List<String>> listList = new ArrayList<>();
for (String userId : userIdList) {
List<String> list = new ArrayList<>();
for (String fieldName : fieldNameList) {
String fieldList = baseDao.selectExport(fieldName, userId);
if (fieldList != null) {
List<FieldListDTO> fieldListDTOS = JSON.parseArray(fieldList, FieldListDTO.class);
if (fieldListDTOS.size() > 1) {
String label = "";
int i = 0;
for (FieldListDTO fieldListDTO : fieldListDTOS) {
i++;
label = label + "第" + i + fieldName + "为" + fieldListDTO.getLabel() + ";";
fieldList = label;
}
} else {
for (FieldListDTO fieldListDTO : fieldListDTOS) {
fieldList = fieldListDTO.getLabel();
}
}
list.add(fieldList);
}
}
listList.add(list);
}
return listList;
}
/**
* 创建自定义表头
*
* @param fieldNameList
* @return
*/
private List<List<String>> myriadPeopleHead(List<String> fieldNameList) {
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
List<List<String>> list = new ArrayList<List<String>>(10);
for (String fieldName : fieldNameList) {
List<String> head = new ArrayList<String>(2);
head.add("自定义导出花名册数据信息 " + dateFormat.format(date));
head.add(fieldName);
list.add(head);
}
return list;
}