Regular Expression RegEx to validate Custom Excel formats
I am creating Excel spreadsheets programmaticly (in C#/.Net using the fabulous EPPlus library) and am passing in an array of cell ranges (string-based) and the custom format string to apply to these ranges.
// Struct:
public struct ExcelColumnFormat
{
public string CellRangeDefinition;
public string ExcelFormatString;
}
-
// Usage
ExcelHelper.ExcelColumnFormat[] formats = new ExcelHelper.ExcelColumnFormat[1];
formats[0].ExcelFormatString = "$#,##0.00";
formats[0].CellRangeDefinition = "I:I";
开发者_如何学运维
-
// Consumption / application of those formats
foreach (ExcelColumnFormat format in formats)
{
// Make sure that there are non-zero or null strings passed in
if (!String.IsNullOrEmpty(format.ExcelFormatString) && !String.IsNullOrEmpty(format.CellRangeDefinition))
ws.Cells[format.CellRangeDefinition].Style.Numberformat.Format = format.ExcelFormatString;
}
I am already checking the strings for IsNullOrEmpty, but I wanted to put another level of validation to test if the custom format strings are, in fact, valid Excel formats. Here is a link to the Office help file that describes the rules around Excel Formats.
The question is: given the many permutations available in custom Excel formats, is this a worthwhile endeavor and how might the RegEx look?
Thanks in advance!
Or you could let VBA figure it out:
On Error GoTo complain
junk = Format(dummyData,formatString)
...
The dummyData could be a reference to a cell in the given range if the type of data isn't known.
精彩评论