From: Yegor Kozlov Date: Fri, 11 Feb 2011 10:03:25 +0000 (+0000) Subject: fixed HSSFWorkbook.createCellStyle to throw exception if the maximum number of cell... X-Git-Tag: REL_3_8_BETA1~22 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=20062200df5d7684a67674ecf6783275b4dcdc6a;p=poi.git fixed HSSFWorkbook.createCellStyle to throw exception if the maximum number of cell styles was exceeded git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1069730 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 55a3de8c7b..6bcbea14e8 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + fixed HSSFWorkbook.createCellStyle to throw exception if the maximum number of cell styles was exceeded 50539 - Better fix for html-style br tags (invalid XML) inside XSSF documents 49928 - allow overridden built-in formats in HSSFCellStyle 50607 - Added implementation for CLEAN(), CHAR() and ADDRESS() diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java index f5b4423b35..20b5712f18 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java @@ -57,7 +57,6 @@ import org.apache.poi.hssf.record.aggregates.RecordAggregate.RecordVisitor; import org.apache.poi.hssf.record.common.UnicodeString; import org.apache.poi.ss.formula.ptg.Area3DPtg; import org.apache.poi.ss.formula.ptg.MemFuncPtg; -import org.apache.poi.ss.formula.ptg.NameXPtg; import org.apache.poi.ss.formula.ptg.OperandPtg; import org.apache.poi.ss.formula.ptg.Ptg; import org.apache.poi.ss.formula.ptg.Ref3DPtg; @@ -68,7 +67,6 @@ import org.apache.poi.poifs.filesystem.DirectoryNode; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.ss.formula.udf.AggregatingUDFFinder; import org.apache.poi.ss.formula.udf.UDFFinder; -import org.apache.poi.ss.usermodel.CreationHelper; import org.apache.poi.ss.usermodel.Row.MissingCellPolicy; import org.apache.poi.ss.formula.FormulaType; import org.apache.poi.ss.util.WorkbookUtil; @@ -92,6 +90,16 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss private static final int MAX_ROW = 0xFFFF; private static final short MAX_COLUMN = (short)0x00FF; + /** + * The maximum number of cell styles in a .xls workbook. + * The 'official' limit is 4,000, but POI allows a slightly larger number. + * This extra delta takes into account built-in styles that are automatically + * created for new workbooks + * + * See http://office.microsoft.com/en-us/excel-help/excel-specifications-and-limits-HP005199291.aspx + */ + private static final int MAX_STYLES = 4030; + private static final int DEBUG = POILogger.DEBUG; /** @@ -1123,12 +1131,19 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss } /** - * create a new Cell style and add it to the workbook's style table + * Create a new Cell style and add it to the workbook's style table. + * You can define up to 4000 unique styles in a .xls workbook. + * * @return the new Cell Style object + * @throws IllegalStateException if the maximum number of cell styles exceeded the limit */ public HSSFCellStyle createCellStyle() { + if(workbook.getNumExFormats() == MAX_STYLES) { + throw new IllegalStateException("The maximum number of cell styles was exceeded. " + + "You can define up to 4000 styles in a .xls workbook"); + } ExtendedFormatRecord xfr = workbook.createCellXF(); short index = (short) (getNumCellStyles() - 1); HSSFCellStyle style = new HSSFCellStyle(index, xfr, this); diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java index 81da9971bd..182ac6aef6 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java @@ -559,4 +559,23 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook { assertEquals("replaceMe", cell .getRichStringCellValue().getString()); } } + public void testCellStylesLimit() { + HSSFWorkbook wb = new HSSFWorkbook(); + int numBuiltInStyles = wb.getNumCellStyles(); + int MAX_STYLES = 4030; + int limit = MAX_STYLES - numBuiltInStyles; + for(int i=0; i < limit; i++){ + HSSFCellStyle style = wb.createCellStyle(); + } + + assertEquals(MAX_STYLES, wb.getNumCellStyles()); + try { + HSSFCellStyle style = wb.createCellStyle(); + fail("expected exception"); + } catch (IllegalStateException e){ + assertEquals("The maximum number of cell styles was exceeded. " + + "You can define up to 4000 styles in a .xls workbook", e.getMessage()); + } + assertEquals(MAX_STYLES, wb.getNumCellStyles()); + } }