Search code examples
excel.net-coreeppluslibreoffice-calc

Can we use Cell Range Validation to populate a dropdown list in excel from another sheet in EPPLUS


I am new to using EPplus. I actually wanted to populate a dropdown list in Sheet1 from a list of values in Sheet2. This can be achieved by Cell Range Validation in Excel.But I am not sure whether EPPlus support this programmatically. It will be really helpful if anyone could help me out. Populate a dropdown in this Designation Column from Designation Column in Sheet 2. Sheet 2


Solution

  • Here's how to do it:

    var departmentSheet = excel.Workbook.Worksheets.Add("department");
    departmentSheet.Cells["A1"].Value = "Management";
    departmentSheet.Cells["A2"].Value = "Administrator";
    departmentSheet.Cells["A3"].Value = "Quality Engineering";
    departmentSheet.Cells["A4"].Value = "Enablers";
    
    var designationSheet = excel.Workbook.Worksheets.Add("designations");
    designationSheet.Cells["A1"].Value = "Developer 1";
    designationSheet.Cells["A2"].Value = "Developer 2";
    designationSheet.Cells["A3"].Value = "Developer 3";
    designationSheet.Cells["A4"].Value = "Developer 4";
    
    // add a validation and set values
    // the range of cells that will contain the validation
    var validation = departmentSheet.DataValidations.AddListValidation("B1:B4");
    
    // set validation rules as required
    validation.ShowErrorMessage = true;
    validation.ErrorStyle = ExcelDataValidationWarningStyle.warning;
    validation.ErrorTitle = "An invalid value was entered";
    validation.Error = "Select a value from the list";
    
    // set the range that contains the validation list
    validation.Formula.ExcelFormula = $"={ designationSheet.Name }!$A$1:$A$4";