POI设置单元格为下拉框数据格式

2022-08-18 20:31:43 浏览数 (2)

HSSF版本
代码语言:javascript复制
public void dropDownBox(Workbook wb, int size) {
        Sheet oneSheet = wb.getSheetAt(0);
        //为指定范围内的单元格添加下拉框样式
        CellRangeAddressList cas = new CellRangeAddressList(1, size, 4, 4);
        //创建下拉数据列
        String[] str = new String[]{"技术管理","专业管理","生产技能","其他"};
        DVConstraint dvConstraint = DVConstraint.createExplicitListConstraint(str);
        //将下拉数据放入下拉框
        HSSFDataValidation dataValidation = new HSSFDataValidation(cas, dvConstraint);
        dataValidation.createErrorBox("错误", "请选择下拉框的值");
        dataValidation.setShowErrorBox(true);
        oneSheet.addValidationData(dataValidation);
    }

XSSH版本
代码语言:javascript复制
public void dropDownBox(Workbook wb, int size) {
        XSSFSheet oneSheet = (XSSFSheet)wb.getSheetAt(0);
        //创建下拉数据列
        String[] str = new String[]{"统一转账","现场缴费","统一转账 现场缴费","其他"};

        XSSFDataValidationHelper dvHelper = new XSSFDataValidationHelper(oneSheet);
        XSSFDataValidationConstraint dvConstraint = (XSSFDataValidationConstraint) dvHelper
                .createExplicitListConstraint(str);
        CellRangeAddressList addressList = null;
        XSSFDataValidation validation = null;

        addressList = new CellRangeAddressList(3, 2   size, 8, 8);
        validation = (XSSFDataValidation) dvHelper.createValidation(
                dvConstraint, addressList);
        validation.createErrorBox("错误", "请选择下拉框的值");
        validation.setShowErrorBox(true);
        oneSheet.addValidationData(validation);
    }
poi

0 人点赞