Home > Net >  Programmatically obtain characters not acceptable in an Excel worksheet name using OpenXML or other
Programmatically obtain characters not acceptable in an Excel worksheet name using OpenXML or other

Time:10-12

Is there a way to programmatically obtain the characters Microsoft Excel will not accept in worksheet names in C#?

I know that, for example, File.GetInvalidFileNameChars() will return a character array containing the characters not acceptable in a Windows filename.

I checked the documentation for OpenXML but could not find anything to help me with this.

Edit

It seems strange that OpenXML will actually allow you to create an invalid sheet name as follows:

Sheet sheet = new Sheet { ..., Name="?/" }
sheets.Add(sheet)

The program will run and the sheets will be generated in the underlying XML files. But if you try to open with excel it will fail...

CodePudding user response:

The OpenXML specification only says that the worksheet name must be unique (printed page number 1568, in part1). The specification is not quite excel, and the .net openxml library will happily allow you to create invalid excel files in any number of ways.

Excel imposes its own restrictions, there's not a programmatic way to retrieve that information.

So the answer is a duplicate of this answer, just don't do these things:

  • The name that you type does not exceed 31 characters.
  • The name does not contain any of the following characters: : \ / ? * [ ]
  • You did not leave the name blank.

Incidentally, non printable ASCII is a valid worksheet name. ¯\_(ツ)_/¯

  • Related