Range Validation with Excel using C#

2019-01-28 22:56发布

I've been looking on the internet for 4 hours and I just can't do it.

My objectives : create a combo where I can sort my items and when I click on one of them, the item appears alone.

In Excel, easy to do, but I can't do it in C#.

I found this answer : Other topic, but I can't understand where the "this.Controls" comes from.

Thanks for your help

3条回答
beautiful°
2楼-- · 2019-01-28 23:39

I found by myself, trying random stuffs :

Range m_range = Sheet.get_Range("A1", "F9"); m_range.AutoFilter(1, Missing.Value,XlAutoFilterOperator.xlAnd, Missing.Value, true);

I don't really know why, but the result is perfect, the top row is used as the header, and all the other cells are added... Cool

查看更多
迷人小祖宗
3楼-- · 2019-01-28 23:41

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.

查看更多
来,给爷笑一个
4楼-- · 2019-01-28 23:55

I can't really help you with the Excel question but the this.Controls comes most probably from a Windows Forms application. See this article on MSDN. You can try it by creating an empty Windows Forms application and then typing in the Form1.cs this.Controls.

查看更多
登录 后发表回答