easyExcel导出模板的时候,固定的某个列我们有固定的是选项值的时候,我们需要将excel的单元格做成下拉选的情况;满足实际的业务需求。
public class CustomSheetWriteHandler implements SheetWriteHandler {
@Override
public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
}
@Override
public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
//定义一个map key是需要添加下拉框的列的index value是下拉框数据
Map mapDropDown = new HashMap<>(3);
//设置单位身份 值写死
String[] unitIdentity = {"职教集团成员单位","拟合作单位","合作单位"};
//地区
String[] area = {"国内","国外"};
//等级下拉选
String[] level = {"国家级示范校","国家级骨干校","省级示范校","省级骨干校","其他"};
//年份下拉选
String[] joinYear = {"1990年","1991年","1992年","1993年","1994年","1995年","1996年","1997年","1998年",
"1999年","2000年","2001年","2002年","2003年","2004年","2005年","2006年","2007年","2008年","2009年","2010年","2011年","2012年",
"2013年","2014年","2015年","2016年","2017年","2018年","2019年","2020年","2021年","2022年"};
//下拉选在Excel中对应的列
mapDropDown.put(0,unitIdentity);
mapDropDown.put(1,joinYear);
mapDropDown.put(4,area);
mapDropDown.put(10,level);
//获取工作簿
Sheet sheet = writeSheetHolder.getSheet();
///开始设置下拉框
DataValidationHelper helper = sheet.getDataValidationHelper();
//设置下拉框
for (Map.Entry entry : mapDropDown.entrySet()) {
/*起始行、终止行、起始列、终止列 起始行为1即表示表头不设置**/
CellRangeAddressList addressList = new CellRangeAddressList(1, 65535, entry.getKey(), entry.getKey());
/*设置下拉框数据**/
DataValidationConstraint constraint = helper.createExplicitListConstraint(entry.getValue());
DataValidation dataValidation = helper.createValidation(constraint, addressList);
sheet.addValidationData(dataValidation);
}
}
}
@Data
@ContentRowHeight(25)
@HeadRowHeight(25)
@ColumnWidth(50)
public class UnitExcelDownVoOne {
@ExcelIgnore
private Integer unitType;
@ExcelProperty(value = "单位身份", index = 0)
private String unitIdentity;
@ExcelProperty(value = "加入年份", index = 1)
private String joinYears;
@ExcelIgnore
private Integer joinYear;
@ExcelProperty(value = "您想加入,成为集团成员吗?", index = 2)
private String joinIdea;
@ExcelProperty(value = "单位名称", index = 3)
private String unitName;
@ExcelProperty(value = "地区", index = 4)
private String area;
@ExcelProperty(value = "地区-省", index = 5)
private String province;
@ExcelProperty(value = "地区-市", index = 6)
private String city;
@ExcelProperty(value = "地区-县区", index = 7)
private String county;
@ExcelProperty(value = "统一社会信用代码", index = 8)
private String code;
@ExcelProperty(value = "学校标识码", index = 9)
private String schoolCode;
@ExcelProperty(value = "级别", index = 10)
private String level;
@ExcelProperty(value = "单位代表人姓名", index = 11)
private String contact;
@ExcelProperty(value = "单位代表人联系电话", index = 12)
private String phone;
@ExcelProperty(value = "单位代表人职务", index = 13)
private String representPost;
@ExcelProperty(value = "单位联系人姓名", index = 14)
private String companyContactName;
@ExcelProperty(value = "单位联系人电话", index = 15)
private String companyContactPhone;
@ExcelProperty(value = "单位联系人职务", index = 16)
private String companyContactPost;
@ExcelProperty(value = "单位联系人部门", index = 17)
private String workUnit;
}
String fileName = URLEncoder.encode("XXX模板导出", "UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
EasyExcel.write(response.getOutputStream(), UnitExcelDownVoOne.class).sheet("中职院校").registerWriteHandler(new CustomSheetWriteHandler()).doWrite(null);