java excel导出时固定列为下拉框
excel导出时固定某列为下拉框,比如对性别列希望固定为男或者女,这样导出的excel就会有如下效果

这种效果我们通常用在导入模板的下载功能中,这样下载下来的模板中的列变为只能选择的枚举选项,就能大大降低用户的输入错误,提高后期导入的正确率。
那么这种怎么做呢?直接上方法:
public class ExcelExportUtils { /** * 给列加下拉选项 * @param sheet sheet页 * @param textList 具体下拉框内容,如 String[] textList = {"男","女}; * @param firstRow 起始行(0起算第一行) * @param endRow 结束行(0起算第一行) * @param firstCol 起始列(0起算第一列) * @param endCol 结束列(0起算第一列) **/ public static void setFewDataValidation(Sheet sheet, String[] textList, int firstRow, int endRow, int firstCol, int endCol) { CellRangeAddressList cellRangeAddressList = new CellRangeAddressList(firstRow, endRow, firstCol, endCol); DataValidationHelper helper = sheet.getDataValidationHelper(); DataValidationConstraint constraint = helper.createExplicitListConstraint(textList); DataValidation dataValidation = helper.createValidation(constraint, cellRangeAddressList); //处理Excel兼容性问题 if (dataValidation instanceof XSSFDataValidation) { dataValidation.setSuppressDropDownArrow(true); dataValidation.setShowErrorBox(true); } else { dataValidation.setSuppressDropDownArrow(false); } dataValidation.setEmptyCellAllowed(true); dataValidation.setShowPromptBox(true); dataValidation.createPromptBox("提示", "只能选择下拉框里面的数据"); sheet.addValidationData(dataValidation); } }

浙公网安备 33010602011771号