如题,excel导出带下拉框的表格
有这么个需求,人员导入,但用户是不知道也不方便选择部门信息的,需要导出excel的时候带上部门信息,部门信息是个下拉框。
先引入pom,要3.*的版本才行
<!-- 阿里excel 解析包 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.3.2</version>
</dependency>
定义一个 SheetWriteHandler 的实现
@Slf4j
public class UserImportFileFilter implements SheetWriteHandler {
/**
* 下拉框值
*/
private Map<Integer,String[]> dropDownMap;
/**
* 多少行有下拉
*/
private final static Integer rowSize = 200;
public UserImportFileFilter(Map<Integer,String[]> dropDownMap) {
this.dropDownMap = dropDownMap;
}
@Override
public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
}
@Override
public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
Sheet sheet = writeSheetHolder.getSheet();
DataValidationHelper helper = sheet.getDataValidationHelper();
dropDownMap.forEach((celIndex, strings) -> {
// 区间设置
CellRangeAddressList cellRangeAddressList = new CellRangeAddressList(1, rowSize, celIndex, celIndex);
// 下拉内容
DataValidationConstraint constraint = helper.createExplicitListConstraint(strings);
DataValidation dataValidation = helper.createValidation(constraint, cellRangeAddressList);
sheet.addValidationData(dataValidation);
});
}
}
增加一个工具类,用于导出的
/**
* 写出数据到excel
*
* @param list 要写出的数据
* @param clazz 写出数据对应的类
* 如果要指定excel的宽高,需要使用 @ColumnWidth(25) @HeadRowHeight(20) @ContentRowHeight(10) 等相关注解
* 多表头导出则需要使用到 @ExcelProperty({"主标题", "字符串标题"}) 类似这种写法
* 需要导出excel有背景颜色,则需要使用:
* 头背景设置成红色 IndexedColors.RED.getIndex() @HeadStyle(fillPatternType = FillPatternType.SOLID_FOREGROUND, fillForegroundColor = 10)
* 头字体设置成20 @HeadFontStyle(fontHeightInPoints = 20)
* 内容的背景设置成绿色 @ContentStyle(fillPatternType = FillPatternType.SOLID_FOREGROUND, fillForegroundColor = 17)
* 内容字体设置成20 @ContentFontStyle(fontHeightInPoints = 20)
*
* @param fileName 写出excel的名称,例如:1.xls ,不填则默认 MongoDB ID生成策略实现.xls ,注意,一定要带后缀
* @param sheetName sheet的名称,不填默认sheet1
* @param <T>
* @return 返回的绝对路径,如:/temps/excel/out/2020/3/文件导出测试.xlsx
*/
public static <T, E extends SheetWriteHandler> String writeSelect(List<T> list, Class<T> clazz, String fileName, String sheetName, E select){
if (CheckTool.checkNull(fileName)){
fileName = IdUtil.objectId() + ".xls";
}
if (CheckTool.checkNull(sheetName)){
sheetName = "sheet1";
}
//返回存储在本地的绝对路径
String path = getOutPath(fileName, false);
try {
HorizontalCellStyleStrategy horizontalCellStyleStrategy = styleDefault();
//如果不需要加样式,直接将 registerWriteHandler 去掉即可。
ExcelWriterBuilder write = EasyExcel.write(path, clazz);
if (select != null){
write.registerWriteHandler(select);
}
write.registerWriteHandler(horizontalCellStyleStrategy).sheet(sheetName).doWrite(list);
} catch (Exception e) {
log.error("excel 生成失败!", e);
throw new ServiceException("excel生成失败");
}
return path;
}
此时,就可以自定义要导出的下拉框了
@GetMapping("getImportExcelFile")
public Result getImportExcelFile(){
HashMap<Integer, String[]> dropDownMap = new HashMap<>();
// 指定下拉框
String[] type = {"全部员工类型","在职员工","退休员工"};
// 从0开始
dropDownMap.put(2, type);
UserImportFileFilter userImportFileFilter = new UserImportFileFilter(dropDownMap);
String s = ExcelTool.writeSelect(null, UserImportFile.class, null, null, userImportFileFilter);
System.out.println(s);
return Result.success(s);
}
注意上面的map,从0开始,序号在那个位置就在哪个列加对应的下拉框。
注意,如果你们已经引入了poi相关的jar,很可能出现依赖冲突。
此时排除掉poi的依赖一般可以解决问题
<!-- 阿里excel 解析包 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>${easyexcel-version}</version>
<exclusions>
<exclusion>
<artifactId>poi-ooxml-schemas</artifactId>
<groupId>com.test.b2c.base</groupId>
</exclusion>
<exclusion>
<artifactId>poi-ooxml</artifactId>
<groupId>com.test.b2c.base</groupId>
</exclusion>
<exclusion>
<artifactId>poi</artifactId>
<groupId>com.test.b2c.base</groupId>
</exclusion>
</exclusions>
</dependency>