1.接口层
@ApiOperation("其他费用配置-模版下载")@GetMapping("/downloadTemplate")public void downloadTemplate(HttpServletResponse response) {try {List<String> list = Arrays.asList("集团", "平台", "部门", "店铺", "年月", "币别", "费用项目", "金额","分摊类型", "分摊依据", "订单号", "备注");ExcelUtils.exportTemplate(response, ExcelUtils.getSimpleHead(list), new SheetWriteHandler() {@Overridepublic void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {String[] expenseItem = new String[]{"退款", "库存损益", "库存清理", "商品成本", "平台费", "收款费用", "物流成本", "包材费", "资金成本", "营销费","刊登费", "vat费", "炒分", "云主机及vps", "vps及其他", "固定服务费分摊", "GBC罚款", "销毁成本", "其他款项", "罚款或暂扣货款金额", "收款费及其他", "服务费"};String[] shareType = new String[]{"店铺承担", "部门承担", "集团承担", "平台承担", "公司承担"};String[] shareGist = new String[]{"总收入", "商品成本", "计算重量", "订单系数", "订单量"};Map<Integer, String[]> mapDropDown = new HashMap<>();mapDropDown.put(6, expenseItem);mapDropDown.put(8, shareType);mapDropDown.put(9, shareGist);ExcelUtils.sheetConfig(writeSheetHolder, mapDropDown);}}, "其他费用配置", "其他费用配置");} catch (IOException e) {e.printStackTrace();}}
2.ExcelUtils
public static <T> void exportTemplate(HttpServletResponse response, List<List<String>> head, SheetWriteHandler handler, String fileName, String sheetName) throws IOException {try {setDownloadResponse(response, fileName);EasyExcel.write(response.getOutputStream()).head(head).autoCloseStream(Boolean.FALSE).sheet(sheetName).registerWriteHandler(handler).doWrite(new ArrayList<>());} catch (Exception e) {returnErrorMessage(response, e);}}public static void setDownloadResponse(HttpServletResponse response, String fileName) throws Exception {response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");response.setCharacterEncoding(StandardCharsets.UTF_8.name());response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + URLEncoder.encode(fileName + ExcelTypeEnum.XLSX.getValue(), StandardCharsets.UTF_8.name()));}private static void returnErrorMessage(HttpServletResponse response, Exception exception) throws IOException {response.reset();response.setContentType("application/json");response.setCharacterEncoding(StandardCharsets.UTF_8.name());Map<String, String> map = new HashMap<>();map.put("status", "failure");map.put("message", "下载文件失败" + exception.getMessage());response.getWriter().println(JSON.toJSONString(map));}public static void sheetConfig(WriteSheetHolder writeSheetHolder, Map<Integer, String[]> mapDropDown) {Sheet sheet = writeSheetHolder.getSheet();DataValidationHelper helper = sheet.getDataValidationHelper();for (Map.Entry<Integer, String[]> entry : mapDropDown.entrySet()) {CellRangeAddressList addressList = new CellRangeAddressList(1, 1000, entry.getKey(), entry.getKey());DataValidationConstraint constraint = helper.createExplicitListConstraint(entry.getValue());DataValidation dataValidation = helper.createValidation(constraint, addressList);if (dataValidation instanceof XSSFDataValidation) {dataValidation.setSuppressDropDownArrow(true);dataValidation.setShowErrorBox(true);} else {dataValidation.setSuppressDropDownArrow(false);}sheet.addValidationData(dataValidation);}}
实现效果


