Skip to content

Workbook::getSheetName() returning null due to sheet name truncation to 31 characters. #966

@KrisPatAU

Description

@KrisPatAU

In 5.x versions, while creating a sheet, it was decided to truncate its name:

String sn = (sheetname.length() > 31) ? sheetname.substring(0, 31) : sheetname;

But as a consequence, trying to get the sheet using the same original name results in null.

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class ApachePOISheetNameTest {
	private static final String LONG_SHEET_NAME = "ThisSheetNameIsMoreThan31Characters";

	public static void main(String[] args) throws Exception {
		try (Workbook workbook = new HSSFWorkbook()) {
			workbook.createSheet(LONG_SHEET_NAME);
			Sheet sheet = workbook.getSheet(LONG_SHEET_NAME);
			System.out.println(sheet);	// null
			sheet = workbook.getSheet(LONG_SHEET_NAME.substring(0, 31));
			System.out.println(sheet);	// works
		}
	}

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions