2017-03-09 20 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