Excel导出功能参考

发布时间 2023-03-28 16:33:09作者: 勇敢-的心
@PostMapping("/export")
@ApiOperation(value = "字典导出")
public void export(@RequestBody AcceptDictVO acceptDictVO, HttpServletResponse response){
try {
dictService.export(acceptDictVO,response);
}catch (Exception e){
CommonResult.fail("导出失败!"+e);
}
}


@Override
public void export(AcceptDictVO acceptDictVO, HttpServletResponse response) {
List<AcceptDict> acceptDicts = new ArrayList<>();
List<AcceptDictExportVO> exportVoList = new ArrayList<>();
//查询结果
if (!CollectionUtils.isEmpty(acceptDictVO.getIds())){
//选中记录查询
QueryWrapper<AcceptDict> queryWrapper = new QueryWrapper<>();
queryWrapper.in("id",acceptDictVO.getIds());
acceptDicts = dictMapper.selectList(queryWrapper);
}else {
acceptDicts = findListByParams(acceptDictVO);
}
acceptDicts.stream().forEach(s -> {
//导出数据处理,设置顺序及excel样式
AcceptDictExportVO exportVo = new AcceptDictExportVO();
BeanUtils.copyProperties(s, exportVo);
exportVoList.add(exportVo);
});
SsoUserModel userInfo = TokenUtils.getUserInfo();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String fileName = "数据字典配置列表+" + sdf.format(new Date()) + "+" + userInfo.getName();
String sheetName = "Sheet1";
try {
//easyexcel
ExcelUtil.writeExcel(response, exportVoList, fileName, sheetName, InspectDictExportVO.class);
} catch (Exception e) {
log.error("export: {}", e);
}
}


@Override
public void export(AcceptDictVO acceptDictVO, HttpServletResponse response) {
List<AcceptDict> acceptDicts = new ArrayList<>();
List<AcceptDictExportVO> exportVoList = new ArrayList<>();
//查询结果
if (!CollectionUtils.isEmpty(acceptDictVO.getIds())){
//选中记录查询
QueryWrapper<AcceptDict> queryWrapper = new QueryWrapper<>();
queryWrapper.in("id",acceptDictVO.getIds());
acceptDicts = dictMapper.selectList(queryWrapper);
}else {
acceptDicts = findListByParams(acceptDictVO);
}
acceptDicts.stream().forEach(s -> {
//导出数据处理,设置顺序及excel样式
AcceptDictExportVO exportVo = new AcceptDictExportVO();
BeanUtils.copyProperties(s, exportVo);
exportVoList.add(exportVo);
});
SsoUserModel userInfo = TokenUtils.getUserInfo();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String fileName = "数据字典配置列表+" + sdf.format(new Date()) + "+" + userInfo.getName();
String sheetName = "Sheet1";
try {
//easyexcel
ExcelUtil.writeExcel(response, exportVoList, fileName, sheetName, InspectDictExportVO.class);
} catch (Exception e) {
log.error("export: {}", e);
}
}



public static void writeExcel(HttpServletResponse response, List<? extends Object> data, String fileName, String sheetName, Class clazz) throws Exception {
// 表头样式
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
// 设置表头居中对齐
headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
// 内容样式
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
// 设置内容靠左对齐
contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.LEFT);
HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
EasyExcel.write(getOutputStream(fileName, response), clazz).excelType(ExcelTypeEnum.XLSX).sheet(sheetName).registerWriteHandler(horizontalCellStyleStrategy).doWrite(data);
}