2017-03-09 21 views
1

我實現,增加了數據驗證在細胞從指定範圍內的代碼,但包含,值被分塊成片......C#互操作的格式確認列表

這是我的代碼

var listFormats = new List<string>(); 
listFormats.Add("US punctuation + alphanumeric lowercase:[a-z0-9,.?;:!&()_'" + '"' + "]+"); 
listFormats.Add("US punctuation + alphanumeric uppercase:[A-Z0-9,.?;:!&()_'" + '"' + "]+"); 
listFormats.Add("US punctuation + alphanumeric mixedcase:[A-Za-z0-9,.?;:!&()_'" + '"' + "]+"); 
var flatListFormats = string.Join(",", listFormats.ToArray()); 

rng.Validation.Add(XlDVType.xlValidateList, XlDVAlertStyle.xlValidAlertInformation, XlFormatConditionOperator.xlBetween, flatListDelimiters, Type.Missing); 

而這就是我得到的確認列表:
enter image description here

而不是

US punctuation + alphanumeric lowercase:[a-z0-9,.?;:!&()_'"]+ 
US punctuation + alphanumeric uppercase:[A-Z0-9,.?;:!&()_'" 
US punctuation + alphanumeric mixedcase:[A-Za-z0-9,.?;:!&()_'" 
+0

'XlDVType.xlValidateList'基本上使單元格下拉框。你可以做的最好的辦法是使用'xlValidateCustom'和一個狡猾的公式,因爲excel不支持公式中的正則表達式,但是vba會。 – Xiaoy312

+0

這裏有兩個不錯的答案:http://stackoverflow.com/questions/30724178/create-data-validation-list-when-some-of-the-values-have-commas – reasra

+0

我不知道如何轉換vba代碼從那個答案到c#... – Valip

回答

1

將列表獲取到一個範圍內並引用數據驗證的範圍。這裏有一些僞代碼讓你開始:

// Get the list you want into a cell range 
worksheet.Range("A1:A3").Value = listFormats; 

// Reference the range when applying the validation 
rng.Validation.Delete(); 
rng.Validation.Add(... xlBetween, "='" + worksheet.Name + "'!" + worksheet.Range("A1:A3").Address); 
+0

這是我實現它後在驗證列表中得到的:'$ K $ 2:$ K $ 11' – Valip

+0

@Valip好點。你需要添加「=」+ ...我更新了我的答案。 – reasra

+0

如果我從其他工作表中引用一個範圍而不是應該包含驗證列表的範圍,這也應該工作嗎? – Valip