【问题标题】:How to create cell in excel file having multiple selection dropdown using OpenXml?如何使用 OpenXml 在具有多项选择下拉列表的 Excel 文件中创建单元格?
【发布时间】:2020-04-01 07:19:20
【问题描述】:

我正在研究使用 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);
            }

此代码的示例输出为:

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

【问题讨论】:

  • 你找到解决方案了吗?

标签: c# excel asp.net-core openxml


【解决方案1】:

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

您可以尝试通过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);

测试结果

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-27
    • 1970-01-01
    • 1970-01-01
    • 2020-03-02
    相关资源
    最近更新 更多