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

C# 使用C语言用Excel进行范围验证#,c#,excel,validation,range,C#,Excel,Validation,Range,在C#Excel中,如何创建一个组合,它可以对项目进行排序,并且当单击其中一个项目时,该项目会单独出现?我无法真正帮助您解决Excel问题,但该控件很可能来自Windows窗体应用程序。请参阅有关MSDN的文章。 您可以通过创建一个空的Windows窗体应用程序,然后键入Form1.cs this.Controls来尝试它。我自己发现,正在尝试一些随机的东西: 范围m_Range=Sheet.get_Range(“A1”, “F9”);m_范围自动过滤器(1, 缺少.Value,XlAutoFi

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

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

我自己发现,正在尝试一些随机的东西:

范围m_Range=Sheet.get_Range(“A1”, “F9”);m_范围自动过滤器(1, 缺少.Value,XlAutoFilterOperator.xlAnd, 缺失。值,true)


我真的不知道为什么,但结果是完美的,顶行用作标题,所有其他单元格都被添加。。。酷

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

/// <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表格
///将包含验证的单元格的基于1的行索引
///将包含验证的单元格的基于1的列索引
///信息框的标题
///信息框中的消息
///用于选择单元格的可用值列表。除此列表外,不允许使用其他值。
///如果发生错误或工作表为空,则引发。
公共静态void AddDataValidation(工作表工作表、int-rowNr、int-columnr、字符串标题、字符串消息、列表验证值)
{
//如果消息字符串太长(超过255个字符,请将其删除)
如果(message.Length>255)
message=message.Substring(0,254);
尝试
{
//验证需要一个以“;”分隔的值列表,作为限制参数。
//折叠列表,以便将其添加为限制(结果为“Value1;Value2;Value3”)
//如果您使用另一个分隔字符(例如在美国),请适当地更改;(例如改为,)
字符串值=string.Join(“;”,validationValues);
//选择指定的单元格
范围单元格=工作表。单元格[rowNr,columnNr];
//删除以前的任何验证
cell.Validation.Delete();
//添加验证,该验证只允许选择提供的值。
cell.Validation.Add(XlDVType.xlValidateList,XlDVAlertStyle.xlValidAlertStop,XlFormatConditionOperator.xlBetween,value,Type.Missing);
cell.Validation.IgnoreBlank=true;
//你可以在那里留言
cell.Validation.InputTitle=标题;
cell.Validation.InputMessage=消息;
}
捕获(异常)
{
//不应触及该部分,但出于稳定性原因使用该部分
抛出新异常(String.Format(“向指定的单元格行{0},列{1},消息{2}”,行号,列号,消息添加带限制的验证时出错),异常);
}
}

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

刚刚复制了代码,结果显示下拉列表只包含一个项目,其中包含由“;”连接的字符串值。我们可以在哪里更改您在评论中提到的“全局分割字符”?我不能用这个词搜索任何结果。Excel根据您的本地化程度使用不同的字符分割列表。看见例如在德国,
在美国/英国使用,而
在美国/英国使用。我已经更新了我的帖子,让它更清晰。这是一个评论,而不是一个答案。