Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create cell in excel file having multiple selection dropdown using OpenXml?

As I am working on functionality to write an excel file using DocumentFormat.OpenXML NuGet package.

I can create the dropdown list on a specific cell in excel but my requirement is, that specific cell should be allowed the user to select multiple items from dropdown list.

Using the below code I am able to create dropdown on cell but that cell is not allowing the multiple selections.

            DataValidation dataValidation = new DataValidation
            {
                Type = DataValidationValues.List,
                AllowBlank = true,
                SequenceOfReferences = new ListValue<StringValue>() { InnerText = "B1" },
                Formula1 = new Formula1("'Cricket Team'!$A$1:$A$3")

            };

            DataValidations dataValidations = worksheet1.GetFirstChild<DataValidations>();
            if (dataValidations != null)
            {
                dataValidations.Count = dataValidations.Count + 1;
                dataValidations.Append(dataValidation);
            }
            else
            {
                DataValidations newdataValidations = new DataValidations();
                newdataValidations.Append(dataValidation);
                newdataValidations.Count = 1;
                worksheet1.Append(newdataValidations);
            }

Sample Output of this code is:

enter image description here

My requirement is, the user can select multiple items from dropdown list.

like image 923
Dipak Delvadiya Avatar asked May 17 '26 15:05

Dipak Delvadiya


1 Answers

my requirement is, that specific cell should be allowed the user to select multiple items from dropdown list.

You can try to set and apply the filter to the range via AutoFilter, like below.

Worksheet sheet1 = new Worksheet();
sheet1.Append(sheetData);

// set the AutoFilter
// and set the range based on your requirement and data items
AutoFilter autoFilterForName = new AutoFilter(){ Reference = "B1:B" + 3 }; 

sheet1.Append(autoFilterForName);

Test Result

enter image description here

like image 200
Fei Han Avatar answered May 19 '26 05:05

Fei Han



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!