tags:

views:

3334

answers:

1

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.

+3  A: 

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.
BradC
If so, why can I get a sheet name with a colon in it?
Patrick Cuff
When I pasted one in, I got the included message, then when I clicked out on the sheet, the colon had changed to a space. Check yours carefully.
BradC
Weird, if you ignore the message and hit <enter> again, Excel will take !@#$%,.| as the name. But you're right, Excel doens't let you manually type a colon, though you can copy one in.
Patrick Cuff
I didn't notice that; can they make the default font for the sheet names any smaller? From a quick glance the ; in the name looked like a :, but you're right (again) :)
Patrick Cuff
Try it with 1:2\3/4?5*6[7]8. It is a bit more obvious.
BradC
This might be useful for someone, <br> setSheetName(wb.getNumberOfSheets()-1, cName, HSSFCell.ENCODING_UTF_16); <br> I was unable to enter chinese charectors as sheetnames but filenames were accepting chinese charectors. After setting the encoding for the sheetname , now it accepts those charectors.
Vimal Raj