Conteoller层:

代码:
/**
* 创建元数据模板
* 导出模板
*
* @param response 响应
* @throws Exception 异常
*/
@GetMapping("/createTemplate")
@ApiOperation(value = "模板下载")
@WhiteApiPerms
public void createMetadataTemplate(HttpServletResponse response) throws Exception {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
String fileName = URLEncoder.encode("服务器上传模板", "UTF-8").replaceAll("\\+", "%20");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
EasyExcel.write(response.getOutputStream(), FwqServerExcelDto.class)
.inMemory(Boolean.TRUE)
.registerWriteHandler(new CustomSheetWriteHandler())
.sheet("服务器上传模板")
.doWrite(new ArrayList());
}
CustomSheetWriteHandler:

代码:
public class CustomSheetWriteHandler extends AbstractCellWriteHandler {
@Override
public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
List<CellData> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
Sheet sheet = writeSheetHolder.getSheet();
// 数据可从数据库查询
Map<Integer,String[]> map = new HashMap<>();
map.put(0, new String[]{"移动", "电信", "联通", "索思", "其他"});
map.put(3, new String[]{"使用中", "已停用", "已收回"});
map.put(5, new String[]{"Centos", "Windows", "Ubuntu", "Debian", "Kali"});
map.put(8, new String[]{"开启", "关闭"});
map.put(10, new String[]{"22", "3389"});
map.put(13, new String[]{"vpn", "堡垒机", "直接访问"});
map.put(14, new String[]{"政务内网", "政务外网", "云服务器"});
// 设置单元格下拉框
for (int i = 0; i <= 17; i++){
if (i == 0 || i == 3 || i == 5 || i == 8 || i == 10 || i == 13 || i == 14){
CellRangeAddressList cellRangeAddressList = new CellRangeAddressList(1, 100, i, i);
DataValidationHelper helper = sheet.getDataValidationHelper();
DataValidationConstraint constraint = helper.createExplicitListConstraint(map.get(i));
DataValidation dataValidation = helper.createValidation(constraint, cellRangeAddressList);
sheet.addValidationData(dataValidation);
}
}
}
}