0

我正在尝试创建一个 dataValidation 对象,其中 List Constraint 是一个范围单个连续列的名称...。我将作为参数传递给 createFormulaListConstraint() 的列表与我想要的工作表不同显示数据验证。以下是我到目前为止的代码,当我尝试运行它时,会抛出一个FormulaParseException: Specified name range does not exist in the current workbook

    Name name = dctmWorkSheet.getWorkbook().createName();
    name.setSheetIndex(1);
    name.setNameName(cell.getStringCellValue());    
    name.setRefersToFormula(getRangeRefersToFormula(valueAssSheet, firstRow, rowIndex, insertCol));

    validationHelper = dctmWorkSheet.getDataValidationHelper();
    //------ THIS IS THE LINE RESPONSIBLE FOR THE EXCEPTION -------
    constraint = validationHelper.createFormulaListConstraint(name.getNameName());
    dataValidation = validationHelper.createValidation(constraint, cral);
    dataValidation.setEmptyCellAllowed(true);
    dataValidation.setShowErrorBox(true);
    dataValidation.setErrorStyle(DataValidation.ErrorStyle.STOP);
    dataValidation.createErrorBox("Error", "Please use Pick List Value");
    dctmWorkSheet.addValidationData(dataValidation);

感谢您的任何帮助或反馈......

4

1 回答 1

0

实际上设法通过为 sheetIndex(0) 创建一个附加名称并将其引用到 sheetIndex(1) 中的名称来解决我的问题。以下是按需要工作的代码:

    Name name = dctmWorkSheet.getWorkbook().createName();
    name.setSheetIndex(1);
    name.setNameName(cell.getStringCellValue());    
    name.setRefersToFormula(getRangeRefersToFormula(valueAssSheet, firstRow, rowIndex, insertCol));
    Name name2 = dctmWorkSheet.getWorkbook().createName();
    name2.setSheetIndex(0);
    name2.setNameName(name.getNameName());
    name2.setRefersToFormula(name.getRefersToFormula());


    validationHelper = dctmWorkSheet.getDataValidationHelper();
    constraint = validationHelper.createFormulaListConstraint(name2.getNameName());
    dataValidation = validationHelper.createValidation(constraint, cral);
    dataValidation.setEmptyCellAllowed(true);
    dataValidation.setShowErrorBox(true);
    dataValidation.setErrorStyle(DataValidation.ErrorStyle.STOP);
    dataValidation.createErrorBox("Error", "Please use Pick List Value");
    dctmWorkSheet.addValidationData(dataValidation);
于 2013-07-11T16:21:05.713 回答