diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index f9609297c..735eedfba 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 52348 - Avoid exception when creating cell style in a workbook that has an empty xf table 52219 - fixed XSSFSimpleShape to set rich text attributes from XSSFRichtextString 52314 - enhanced SheetUtil.getColumnWidth diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java index 51ffe6e06..d633300ae 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java @@ -68,15 +68,15 @@ public class XSSFCellStyle implements CellStyle { /** * Creates a Cell Style from the supplied parts - * @param cellXfId The main XF for the cell - * @param cellStyleXfId Optional, style xf + * @param cellXfId The main XF for the cell. Must be a valid 0-based index into the XF table + * @param cellStyleXfId Optional, style xf. A value of -1 means no xf. * @param stylesSource Styles Source to work off */ public XSSFCellStyle(int cellXfId, int cellStyleXfId, StylesTable stylesSource, ThemesTable theme) { _cellXfId = cellXfId; _stylesSource = stylesSource; _cellXf = stylesSource.getCellXfAt(this._cellXfId); - _cellStyleXf = stylesSource.getCellStyleXfAt(cellStyleXfId); + _cellStyleXf = cellStyleXfId == -1 ? null : stylesSource.getCellStyleXfAt(cellStyleXfId); _theme = theme; } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java index a9c968c7e..11b724f4a 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java @@ -691,4 +691,19 @@ public class TestXSSFCellStyle extends TestCase { assertEquals(fmtClone.getFormat("Test##"), reload.getDataFormat()); assertFalse(fmtClone.getFormat("Test##") == fmt.getFormat("Test##")); } + + /** + * Avoid ArrayIndexOutOfBoundsException when creating cell style + * in a workbook that has an empty xf table. + */ + public void testBug52348() { + XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("52348.xlsx"); + StylesTable st = workbook.getStylesSource(); + assertEquals(0, st._getStyleXfsSize()); + + + XSSFCellStyle style = workbook.createCellStyle(); // no exception at this point + assertNull(style.getStyleXf()); + } + } diff --git a/test-data/spreadsheet/52348.xlsx b/test-data/spreadsheet/52348.xlsx new file mode 100644 index 000000000..356490d8d Binary files /dev/null and b/test-data/spreadsheet/52348.xlsx differ