【问题标题】:Range Validation with Excel using C#使用 C# 使用 Excel 进行范围验证
【发布时间】:2021-03-12 01:40:03
【问题描述】:

在 C# 中的 Excel 中,如何创建一个可以对项目进行排序的组合,当单击其中一个项目时,该项目单独出现?

【问题讨论】:

    标签: c# excel validation range


    【解决方案1】:

    如果您想为此目的使用验证,我编写了以下方法来添加验证和用户单击单元格时出现的小信息框:

    /// <summary>
    /// Adds a small Infobox and a Validation with restriction (only these values will be selectable) to the specified cell.
    /// </summary>
    /// <param name="worksheet">The excel-sheet</param>
    /// <param name="rowNr">1-based row index of the cell that will contain the validation</param>
    /// <param name="columnNr">1-based column index of the cell that will contain the validation</param>
    /// <param name="title">Title of the Infobox</param>
    /// <param name="message">Message in the Infobox</param>
    /// <param name="validationValues">List of available values for selection of the cell. No other value, than this list is allowed to be used.</param>
    /// <exception cref="Exception">Thrown, if an error occurs, or the worksheet was null.</exception>
    public static void AddDataValidation(Worksheet worksheet, int rowNr, int columnNr, string title, string message, List<string> validationValues)
    {
        //If the message-string is too long (more than 255 characters, prune it)
        if (message.Length > 255)
            message = message.Substring(0, 254);
    
        try
        {
            //The validation requires a ';'-separated list of values, that goes as the restrictions-parameter.
            //Fold the list, so you can add it as restriction. (Result is "Value1;Value2;Value3")
            //If you use another separation-character (e.g in US) change the ; appropriately (e.g. to the ,)
            string values = string.Join(";", validationValues);
            //Select the specified cell
            Range cell = worksheet.Cells[rowNr, columnNr];
            //Delete any previous validation
            cell.Validation.Delete();
            //Add the validation, that only allowes selection of provided values.
            cell.Validation.Add(XlDVType.xlValidateList, XlDVAlertStyle.xlValidAlertStop, XlFormatConditionOperator.xlBetween, values, Type.Missing);
            cell.Validation.IgnoreBlank = true;
            //Optional put a message there
            cell.Validation.InputTitle = title;
            cell.Validation.InputMessage = message;
    
       }
       catch (Exception exception)
        {
             //This part should not be reached, but is used for stability-reasons
             throw new Exception(String.Format("Error when adding a Validation with restriction to the specified cell Row:{0}, Column:{1}, Message: {2}", rowNr, columnNr, message), exception);
    
        }
    }
    

    如果您不需要信息框,只需省略出现变量标题或消息的部分即可。

    【讨论】:

    • 刚刚复制了你的代码,结果发现下拉列表只包含一个项目,其中包含由“;”连接的字符串值。正如您在评论中提到的,我们在哪里可以更改“全局拆分字符”?我无法用该词搜索任何结果。
    • Excel 根据您的本地化使用不同的字符来拆分列表。见en.wikipedia.org/wiki/Comma-separated_values。例如在德国,使用;,而在美国/英国使用,。我已经更新了我的帖子以更加清晰。
    【解决方案2】:

    我自己找到的,尝试随机的东西:

    范围 m_range = Sheet.get_Range("A1", “F9”); m_range.AutoFilter(1, Missing.Value,XlAutoFilterOperator.xlAnd, Missing.Value, true);

    我也不知道为什么,但是结果很完美,最上面一行作为表头,其他所有单元格都加进去了……酷

    【讨论】:

      【解决方案3】:

      我无法真正帮助您解决 Excel 问题,但 this.Controls 很可能来自 Windows 窗体应用程序。请参阅 MSDN 上的 this 文章。 您可以通过创建一个空的 Windows 窗体应用程序然后输入 Form1.cs this.Controls 来尝试它。

      【讨论】:

      • 这是评论而不是答案。
      猜你喜欢
      • 2014-03-06
      • 1970-01-01
      • 1970-01-01
      • 2014-06-14
      • 1970-01-01
      • 2020-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多