依赖 <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.4.0</version> </dependency>
<!-- easypoi --> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-base</artifactId> <version>3.2.0</version> <exclusions> <exclusion> <artifactId>guava</artifactId> <groupId>com.google.guava</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-web</artifactId> <version>3.2.0</version> </dependency> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-annotation</artifactId> <version>3.2.0</version> </dependency>
工具 /** * excel 导出-带下拉类表 * * @param response * @param fileName 文件名 * @param projects 对象集合 * @param columnNames 导出的excel中的列名 * @param keys 对应的是对象中的字段名字 * @param dvArray 下拉类表信息key--> 列;value--> 下拉数据 * @throws IOException */ public static void exportExcelDv(HttpServletResponse response, String fileName, List<?> projects, Object[] keys, Object[] columnNames, Map<Integer,String[]> dvArray) throws IOException { ExcelWriter bigWriter = ExcelUtil.getBigWriter(); Sheet sheet = bigWriter.getSheet(); for (int i = 0; i < keys.length; i++) { bigWriter.addHeaderAlias(keys[i]+"",columnNames[i]+""); bigWriter.setColumnWidth(i, 20); String[] arr = dvArray.get(i); if(null != arr && arr.length > 0){ DataValidationHelper dataValidationHelper = sheet.getDataValidationHelper(); DataValidationConstraint constraint = dataValidationHelper.createExplicitListConstraint(arr); DataValidation validation = dataValidationHelper.createValidation(constraint, new CellRangeAddressList(1, 65536,i , i)); validation.createErrorBox("提示","输入有误,请选择"); if (validation instanceof XSSFDataValidation) { validation.setShowErrorBox(true); validation.setSuppressDropDownArrow(true); } else { validation.setSuppressDropDownArrow(false); } bigWriter.addValidationData(validation); } } // 一次性写出内容,使用默认样式,强制输出标题 bigWriter.write(projects, true); //response为HttpServletResponse对象 response.setContentType("application/vnd.ms-excel;charset=utf-8"); //test.xls是弹出下载对话框的文件名,不能为中文,中文请自行编码 response.setHeader("Content-Disposition", "attachment;filename=" + new String((fileName + ".xlsx").getBytes(), "iso-8859-1")); ServletOutputStream out = response.getOutputStream(); bigWriter.flush(out, true); // 关闭writer,释放内存 bigWriter.close(); //此处记得关闭输出Servlet流 IoUtil.close(out); }