赞
踩
@GetMapping("/download")
public void download(String name, HttpServletResponse response) {
fileManager.download(name, response);
}
public void download(String name, HttpServletResponse response) {
try {
String template_path = "E://template//模板文件.xlsx";
if (!new File(template_path).exists()) {
String message = "模板文件不存在,路径:" + template_path;
log.error(message);
throw new XException(message);
}
String strFileName = String.format("%s_导出文件.xlsx", name);
//获取数据字段数据【这个根据自己需要获取到对应的数据,自己实现此方法】
List<SysColDto> sysColDtos = getData();
//构建填充excel单元格数据【需要将获取到的转换成SysColExcelDto(代码见后面),这个实体类见后面】
List<SysColExcelDto> list = new ArrayList<>();
for (SysColDto sysColDto : sysColDtos) {
SysColExcelDto sysColExcelDto = new SysColExcelDto();
sysColExcelDto.setModname(sysColDto.getModname());
sysColExcelDto.setLname(sysColDto.getLname());
sysColExcelDto.setCname(sysColDto.getCname());
list.add(sysColExcelDto);
}
//自定义行风格handler【笔者这边用了一个自定义行风格handler(代码见后面),是为了实现框线,看每个人的需求决定使不使用】
CustomRowStyleHandler customRowStyleHandler = new CustomRowStyleHandler(1, 9);
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
// 这里URLEncoder.encode可以防止中文乱码
response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", new String(strFileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1)));
//填充excel【easyExcel提供了多种生成excel的方式,这边使用的是根据模板来填充模板中的数据doFill,模板中数据填充位置需要参考:https://easyexcel.opensource.alibaba.com/docs/current/quickstart/fill】
EasyExcel.write(response.getOutputStream())
.registerWriteHandler(customRowStyleHandler)
.withTemplate(template_path)
.sheet()
.doFill(list);
} catch (IOException ex) {
throw new XException(ex.getMessage());
}
}
/**
* 书签导入excel对象
*/
@Data
public class SysColExcelDto {
@ExcelProperty("对应信息采集组")
private String modname;
@ExcelProperty("对应采集栏目名称")
private String lname;
@ExcelProperty("对应系统中文字段")
private String cname;
}
/**
* excel自定义行风格处理器
*/
public class CustomRowStyleHandler implements RowWriteHandler {
//单元格起始下标
private Integer cellIndexStart;
//单元格结束下标
private Integer cellIndexEnd;
public CustomRowStyleHandler(Integer cellIndexStart, Integer cellIndexEnd) {
this.cellIndexStart = cellIndexStart;
this.cellIndexEnd = cellIndexEnd;
}
public CustomRowStyleHandler() {
}
@Override
public void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Integer relativeRowIndex, Boolean isHead) {
Sheet sheet = writeSheetHolder.getSheet();
Workbook workbook = sheet.getWorkbook();
CellStyle cellStyle = workbook.createCellStyle();
//设置边框样式
cellStyle.setBorderLeft(BorderStyle.THIN);//细实线
cellStyle.setBorderTop(BorderStyle.THIN);
cellStyle.setBorderRight(BorderStyle.THIN);
cellStyle.setBorderBottom(BorderStyle.THIN);
for (Integer i = cellIndexStart; i <= cellIndexEnd; i++) {
Cell cell = row.getCell(i);
cell.setCellStyle(cellStyle);
}
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。