如何使用OpenXml在具有多个选择下拉列表的excel文件中创建单元格?

问题描述 投票:2回答:1

由于我正在使用DocumentFormat.OpenXML NuGet包来编写Excel文件的功能。

我可以在excel中的特定单元格上创建下拉列表,但我的要求是,应允许用户从下拉列表中选择多个项目

使用下面的代码,我可以在单元格上创建下拉菜单,但是该单元格不允许多项选择。

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

此代码的示例输出是:

enter image description here

我的要求是,用户可以从下拉列表中选择多个项目

c# excel asp.net-core openxml
1个回答
0
投票

我的要求是,应允许用户从下拉列表中选择多个项目。

您可以尝试通过AutoFilter设置过滤器并将其应用于范围,如下所示。

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

测试结果

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.