diff --git a/src/java/org/apache/poi/hssf/model/Sheet.java b/src/java/org/apache/poi/hssf/model/Sheet.java index 5e528f659..80adc8457 100644 --- a/src/java/org/apache/poi/hssf/model/Sheet.java +++ b/src/java/org/apache/poi/hssf/model/Sheet.java @@ -1091,6 +1091,8 @@ public final class Sheet implements Model { * (in units of 1/256th of a character width) */ public void setColumnWidth(int column, int width) { + if(width > 255*256) throw new IllegalArgumentException("The maximum column width for an individual cell is 255 characters."); + setColumn(column, null, new Integer(width), null, null, null); } diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java index af51ce654..5e7609db0 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java @@ -429,9 +429,16 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet { } /** - * set the width (in units of 1/256th of a character width) + * Set the width (in units of 1/256th of a character width) + *
+ * The maximum column width for an individual cell is 255 characters. + * This value represents the number of characters that can be displayed + * in a cell that is formatted with the standard font. + *
+ * * @param columnIndex - the column to set (0-based) * @param width - the width in units of 1/256th of a character width + * @throws IllegalArgumentException if width > 65536 (the maximum column width in Excel) */ public void setColumnWidth(int columnIndex, int width) { sheet.setColumnWidth(columnIndex, width); diff --git a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Sheet.java b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Sheet.java index a6a1662c5..ad3e50b09 100644 --- a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Sheet.java +++ b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Sheet.java @@ -118,6 +118,11 @@ public interface Sheet extends Iterable+ * The maximum column width for an individual cell is 255 characters. + * This value represents the number of characters that can be displayed + * in a cell that is formatted with the standard font. + *
* * @param columnIndex - the column to set (0-based) * @param width - the width in units of 1/256th of a character width diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java index aa35007ff..b1ed00935 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java @@ -1266,11 +1266,19 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { /** * Set the width (in units of 1/256th of a character width) + *+ * The maximum column width for an individual cell is 255 characters. + * This value represents the number of characters that can be displayed + * in a cell that is formatted with the standard font. + *
* * @param columnIndex - the column to set (0-based) * @param width - the width in units of 1/256th of a character width + * @throws IllegalArgumentException if width > 65536 (the maximum column width in Excel) */ public void setColumnWidth(int columnIndex, int width) { + if(width > 255*256) throw new IllegalArgumentException("The maximum column width for an individual cell is 255 characters."); + columnHelper.setColWidth(columnIndex, (double)width/256); } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java index dd0446c9d..61a0b5e94 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java @@ -184,6 +184,18 @@ public class TestXSSFSheet extends TestCase { assertEquals(1, sheet.getRowBreaks().length); } + public void testMaxColumnWidth() { + XSSFWorkbook workbook = new XSSFWorkbook(); + Sheet sheet = workbook.createSheet("Sheet 1"); + sheet.setColumnWidth(0, 255*256); //the limit + try { + sheet.setColumnWidth(0, 256*256); //the limit + fail("expected exception"); + } catch (Exception e){ + ; + } + } + public void testGetSetColumnBreaks() { XSSFWorkbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("Sheet 1"); diff --git a/src/testcases/org/apache/poi/hssf/model/TestSheetAdditional.java b/src/testcases/org/apache/poi/hssf/model/TestSheetAdditional.java index 477908dc7..9afae28b1 100644 --- a/src/testcases/org/apache/poi/hssf/model/TestSheetAdditional.java +++ b/src/testcases/org/apache/poi/hssf/model/TestSheetAdditional.java @@ -54,4 +54,15 @@ public final class TestSheetAdditional extends TestCase { assertEquals((short)100,sheet.getColumnWidth((short)9)); assertEquals((short)100,sheet.getColumnWidth((short)10)); } + + public void testMaxColumnWidth() { + Sheet sheet = Sheet.createSheet(); + sheet.setColumnWidth(0, 255*256); //the limit + try { + sheet.setColumnWidth(0, 256*256); //the limit + fail("expected exception"); + } catch (Exception e){ + ; + } + } }