Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

validate excel worksheet name

Tags:

c#

regex

excel

I'm getting the below error when setting the worksheet name dynamically. Does anyone has regexp to validate the name before setting it ?

  • The name that you type does not exceed 31 characters. The name does
  • not contain any of the following characters: : \ / ? * [ or ]
  • You did not leave the name blank.
like image 275
UshaP Avatar asked Jun 09 '26 10:06

UshaP


1 Answers

You can use the method to check if the sheet name is valid

private bool IsSheetNameValid(string sheetName)
{
    if (string.IsNullOrEmpty(sheetName))
    {
        return false;
    }

    if (sheetName.Length > 31)
    {
        return false;
    }

    char[] invalidChars = new char[] {':', '\\',  '/',  '?',  '*', '[', ']'};
    if (invalidChars.Any(sheetName.Contains))
    {
        return false;
    }

    return true;
}
like image 144
HatSoft Avatar answered Jun 11 '26 00:06

HatSoft



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!