@AllArgsConstructor
public class ExcelDropdownHandler implements SheetWriteHandler{
private final int columnNo;
private final int maxValidRow;
private final List<String> dropdownData;
@Override
public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
}
@Override
public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
if (CollectionUtils.isEmpty(dropdownData))return;
//起始行、终止行、起始列、终止列
CellRangeAddressList addressList = new CellRangeAddressList(1, maxValidRow, columnNo, columnNo);
DataValidationHelper helper = writeSheetHolder.getSheet().getDataValidationHelper();
//设置下拉框数据
DataValidationConstraint constraint = helper.createExplicitListConstraint(dropdownData.toArray(new String[0]));
DataValidation validation = helper.createValidation(constraint, addressList);
//处理Excel兼容性问题
if (validation instanceof XSSFDataValidation) {
validation.setSuppressDropDownArrow(true);
validation.setShowErrorBox(true);
} else {
validation.setSuppressDropDownArrow(false);
}
validation.setErrorStyle(DataValidation.ErrorStyle.STOP);
writeSheetHolder.getSheet().addValidationData(validation);
}
}
异常提示 代码运行本身没有异常,就是当下拉框数据是["人民币[CNY]"],这样的数组时,打开Excel就报错了,但是数组里面的值换成["人民币-CNY"]就没问题了,很奇怪。
建议描述