如何使用户仅在下拉列表中选择一个值,而不在excel表中输入自己的值。我正在使用Java库Apache-POI

最佳答案

如果您使用的是HSSFWorkbook,请在下面使用

        DVConstraint dvConstraint =


DVConstraint.createExplicitListConstraint(new String [] {“ ABC”});
        DataValidation dataValidation =新的HSSFDataValidation(programList,dvConstraint);
        dataValidation.setSuppressDropDownArrow(true);
        dataValidation.setShowErrorBox(true);
        dataValidation.setShowPromptBox(true);

    targetSheet.addValidationData(dataValidation);

For XSSFWorkbook, use as below

        DataValidationHelper validationHelper=new XSSFDataValidationHelper(targetSheet);
        DataValidationConstraint constraint =validationHelper.createExplicitListConstraint(new String[]{"ABC"});
        DataValidation dataValidation = validationHelper.createValidation(constraint, programList);
        dataValidation.setSuppressDropDownArrow(true);
        dataValidation.setShowErrorBox(true);
        dataValidation.setShowPromptBox(true);
        targetSheet.addValidationData(dataValidation);

09-10 07:38