使用 C# 使用 Excel 进行范围验证

Range Validation with Excel using C#(使用 C# 使用 Excel 进行范围验证)
本文介绍了使用 C# 使用 Excel 进行范围验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In Excel in C# how does one create a combo where it can sort the items and when one of them is clicked, the item appears alone?

推荐答案

如果你想为此目的使用验证,我编写了以下方法来添加验证和一个小的信息框,当用户点击细胞:

if you want to use a validation for that purpose the following method was written by me to add a Validation and a small Infobox that appears when the user clicks on the cell:

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

    }
}

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

If you don't need the Infobox, just leave out the parts where the variables title or message appear.

这篇关于使用 C# 使用 Excel 进行范围验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)