Here is a solution that gives you unique and valid sheet names in Excel files
that you are generating.
The problem
Generating Excel files in code could be fun. But when you do you need to make
sure that the sheet names follow these rules:
The name cannot be more than 31 characters.
The name must be unique.
The name cannot be history.
The following characters cannot be used:
:
\
/
?
*
[
]
The solution
I am using ClosedXML that is a great
library when you are generating Excel files in code. This is used in this code
that solves the problem:
CSharp
The output from this code is:
Sheet A
Sheet A-1
Sheet with a name that is longe
Sheet B
Sheet B-1
SheetB
History.
Summary
This is not a complicated problem, but I hope someone find this useful anyway.
The largest problem I think is that there is no official documentation on the
limitations of Excel sheet names. There is a page listing some specifications
and limitations of
Excel,
but this is not mentioned.