-1

I have a excel which has some fields as a dropdown, need to get entire list of the dropdown has. I tried to use the ClosedXML for this. I can able to find whether it is list or not. But unable to read the entire list?

     using(var workbook = new XLWorkbook("C:\Data\Test.xlsx"))
    {
        var workSheetTable = workbook.Worksheets.Worksheet("Template1");

        var dropdownList = workSheetTable.Range("A5:F5");
        var datavalid = workSheetTable.DataValidations.
            GetAllInRange(dropdownList.RangeAddress).ToList();

        foreach (var dataValidation in workSheetTable.DataValidations)
        {
            if (dataValidation.AllowedValues == XLAllowedValues.List)
            {
                //How to get values of the list
            }
        }
    }
Ram
  • 1

1 Answers1

0

IXLDataValidation.Value is holding the selected range for validation.

using (XLWorkbook workbook = new XLWorkbook(@"C:\Data\Test.xlsx"))
{
    IXLWorksheet workSheetTable = workbook.Worksheets.Worksheet("Template1");

    IXLRange dropdownList = workSheetTable.Range("A5:F5");
    foreach (IXLDataValidation dataValidation in workSheetTable.DataValidations.GetAllInRange(dropdownList.RangeAddress))
    {
        if (dataValidation.AllowedValues == XLAllowedValues.List)
        {
            foreach(IXLCell cell in workSheetTable.Range(dataValidation.Value).Cells())
            {
                System.Diagnostics.Debug.WriteLine(string.Format("validation item for cel(s) {0}: {1}",dropdownList.RangeAddress,cell.Value));
            }
        }
    }
}
DerSchnitz
  • 457
  • 3
  • 8