我一直在網上查找4個小時,我無法做到。使用C#Excel11進行範圍驗證
我的目標:創建一個組合,在那裏我可以對物品進行分類,當我點擊其中一個物品時,該物品將單獨出現。
在Excel中,很容易做到,但我無法在C#中完成。
我找到了這個答案:Other topic,但我不明白「this.Controls」來自哪裏。
感謝您的幫助
我一直在網上查找4個小時,我無法做到。使用C#Excel11進行範圍驗證
我的目標:創建一個組合,在那裏我可以對物品進行分類,當我點擊其中一個物品時,該物品將單獨出現。
在Excel中,很容易做到,但我無法在C#中完成。
我找到了這個答案:Other topic,但我不明白「this.Controls」來自哪裏。
感謝您的幫助
我真的不能幫你的Excel的問題,但this.Controls談到最有可能從Windows窗體應用程序。請參閱MSDN上的this文章。 您可以通過創建一個空的Windows窗體應用程序然後在Form1.cs中輸入this.Controls來嘗試它。
我發現我自己,試圖隨機的東西:
範圍m_範圍= Sheet.get_Range( 「A1」, 「F9」); m_range.AutoFilter(1, Missing.Value,XlAutoFilterOperator.xlAnd, Missing.Value,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);
}
}
如果您不需要的信息框,只留下了部分變量標題或消息出現在哪裏。
剛剛複製了你的代碼,事實證明,下拉列表只包含一個項目,其中包含由「;」加入的字符串值。如您在評論中提到的那樣,我們可以在哪裏更改「全球分割字符」?我無法谷歌任何結果與該術語。 – lowatt 2012-10-16 03:17:42
Excel根據您的本地化使用不同的字符來拆分列表。請參閱http://en.wikipedia.org/wiki/Comma-separated_values。例如在德國,使用';',而在美國/英國使用','。我已更新我的帖子,以便更清楚。 – 2013-08-20 04:24:07