从不同工作表中的命名列表创建dataValidation

时间:2013-07-11 14:09:35

标签: java apache-poi

我正在尝试创建一个dataValidation对象,其中List Constraint是一个范围单个连续列的名称....我将作为参数传递给createFormulaListConstraint()的列表位于不同的表单中我想显示dataValidation。 以下是我到目前为止的代码,当我尝试运行它时 FormulaParseException:当前工作簿中不存在指定的名称范围

    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);

感谢您提供任何帮助或反馈......

1 个答案:

答案 0 :(得分: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);