Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Valid characters for Excel sheet names

Tags:

java

excel

In Java, we're using the following package to programmatically create excel documents:

org.apache.poi.hssf 

If you attempt to set a sheet's name (NOT file, but internal Excel sheet), you will get an error if:

  • The name is over 31 characters
  • The name contains any of the following characters: / \ * ? [ ]

However, after creating a document with a sheet name of:

@#$%&()+~`"':;,.|

No error is output, and everything seems fine in Java. When you open the excel file in Office 2003, it will give you an error saying that the sheet name was invalid and that it renamed it to something generic like "Sheet 1".

I don't know much about the previously stated package that we're using, but it looks like it's not correctly filtering invalid Excel sheet names. Any idea of how I can filter out all known invalid characters? I'm hesitant to simply filter out all non-word characters.

like image 359
Ross Avatar asked Jan 16 '09 18:01

Ross


People also ask

How many characters can an Excel sheet name have?

Microsoft Excel does not allow worksheet name longer than 31 characters.

How do you name a sheet in Excel?

Double-click the sheet tab, and type the new name. Right-click the sheet tab, click Rename, and type the new name.

Are spaces allowed in Excel worksheet names?

Spaces are not allowed in worksheet names. A worksheet's tab color can be changed, but the text color of the tab cannot be changed. It is not possible to hide all of the worksheets in a workbook. The Watch Window can be used to monitor the value in one or more cells.


2 Answers

I think the problem is the colon, not the exclamation point.

If you open Excel and try to manually edit a sheet name, the only characters it doesn't let you type are [ ] * / \ ? :

If you paste one of those characters in, you get the following error: (Excel 2003)

While renaming a sheet or chart, you entered an invalid name. Try one of the following:

  • Make sure the name you entered does not exceed 31 characters.
  • Make sure the name does not contain any of the following characters: : \ / ? * [ or ]
  • Make sure you did not leave the name blank.
like image 148
BradC Avatar answered Oct 01 '22 01:10

BradC


You can use the Apache POI's WorkbookUtil.createSafeSheetName(String s) to safely create your Sheet Name.

http://poi.apache.org/apidocs/org/apache/poi/ss/util/WorkbookUtil.html

like image 33
Tago Fabic Avatar answered Oct 01 '22 02:10

Tago Fabic