si vous souhaitez utiliser un validation à cette fin la méthode suivante a été écrit par moi d'ajouter une validation et une petite Infobox qui apparaît lorsque l'utilisateur clique sur la cellule:
/// <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);
}
}
Si vous n'avez pas besoin Infobox, il suffit de laisser les parties où le titre ou le message des variables apparaît.
Il suffit de copier vos codes et il s'avère que la liste déroulante contient un seul élément contenant les valeurs de chaîne jointes par ";". Et où pouvons-nous changer le "caractère diviseur global" comme vous l'avez mentionné dans le commentaire? Je ne peux pas google n'importe quel résultat avec ce terme. – lowatt
Excel utilise différents caractères pour diviser les listes en fonction de votre localisation. Voir http://en.wikipedia.org/wiki/Comma-separated_values. Par exemple en Allemagne, le ';' est utilisé, tandis que ',' est utilisé aux États-Unis/Royaume-Uni. J'ai mis à jour mon message pour être plus clair. –