C# Interop format Validation List

2019-08-02 09:04发布

I implemented a code that adds Data Validation on cells from a specified range, but the values that contain , are chunked into pieces...

This is my code

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);

And this is what I get in the Validation List:
enter image description here

Instead of

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

1条回答
闹够了就滚
2楼-- · 2019-08-02 09:35

Get the list into a range and reference the range for the data validation. Here's some pseudo code to get you started:

// 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);
查看更多
登录 后发表回答