### Eclipse Workspace Patch 1.0 #P apache-poi Index: src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java =================================================================== --- src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java (revision 981695) +++ src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java (working copy) @@ -725,7 +725,8 @@ * * @param sheetname the name for the new sheet. Note - certain length limits * apply. See {@link #setSheetName(int, String)}. - * + * @see {@link org.apache.poi.hssf.util.WorkbookUtil#createSafeSheetName(String nameProposal)} + * for a safe way to create valid names * @return HSSFSheet representing the new sheet. * @throws IllegalArgumentException * if there is already a sheet present with a case-insensitive Index: src/examples/src/org/apache/poi/hssf/usermodel/examples/NewSheet.java =================================================================== --- src/examples/src/org/apache/poi/hssf/usermodel/examples/NewSheet.java (revision 981695) +++ src/examples/src/org/apache/poi/hssf/usermodel/examples/NewSheet.java (working copy) @@ -19,6 +19,7 @@ import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFSheet; +import org.apache.poi.hssf.util.WorkbookUtil; import java.io.IOException; import java.io.FileOutputStream; @@ -33,7 +34,8 @@ HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet1 = wb.createSheet("new sheet"); HSSFSheet sheet2 = wb.createSheet(); // create with default name - wb.setSheetName(1, "second sheet"); // setting sheet name later + final String name = "second sheet"; + wb.setSheetName(1, WorkbookUtil.createSafeSheetName(name)); // setting sheet name later FileOutputStream fileOut = new FileOutputStream("workbook.xls"); wb.write(fileOut); fileOut.close(); Index: src/java/org/apache/poi/hssf/util/WorkbookUtil.java =================================================================== --- src/java/org/apache/poi/hssf/util/WorkbookUtil.java (revision 0) +++ src/java/org/apache/poi/hssf/util/WorkbookUtil.java (revision 0) @@ -0,0 +1,78 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ + +package org.apache.poi.hssf.util; + + +/** + * @author Martin W. Kirst (maki at bitkings de) + */ +public class WorkbookUtil { + + /** + * Creates a valid sheet name, which is conform to the rules. + * In any case, the result safely can be used for + * {@link org.apache.poi.hssf.usermodel.HSSFWorkbook#setSheetName(int, String)}. + *
+ * Rules: + * + * Invalid characters are replaced by one space character ' '. + * + * @param nameProposal can be any string, will be truncated if necessary, + * allowed to be null + * @return a valid string, "empty" if to short, "null" if null + */ + public final static String createSafeSheetName(final String nameProposal) { + if (nameProposal == null) { + return "null"; + } + if (nameProposal.length() < 1) { + return "empty"; + } + final int length = Math.min(31, nameProposal.length()); + final String shortenname = nameProposal.substring(0, length); + final StringBuilder result = new StringBuilder(shortenname); + for (int i=0; i