diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java
index 29a46c5d3..999fa287d 100644
--- a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java
+++ b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java
@@ -540,6 +540,11 @@ public class HSSFCell implements Cell {
setCellType(CELL_TYPE_BLANK, false, row, col, styleIndex);
return;
}
+
+ if(hvalue.length() > SpreadsheetVersion.EXCEL97.getMaxTextLength()){
+ throw new IllegalArgumentException("The maximum length of cell contents (text) is 32,767 characters");
+ }
+
if (_cellType == CELL_TYPE_FORMULA) {
// Set the 'pre-evaluated result' for the formula
// note - formulas do not preserve text formatting.
diff --git a/src/java/org/apache/poi/ss/SpreadsheetVersion.java b/src/java/org/apache/poi/ss/SpreadsheetVersion.java
index 316e5af06..d4edaec53 100755
--- a/src/java/org/apache/poi/ss/SpreadsheetVersion.java
+++ b/src/java/org/apache/poi/ss/SpreadsheetVersion.java
@@ -36,9 +36,11 @@ public enum SpreadsheetVersion {
*
The total number of available rows is 64k (2^16)
* The maximum number of arguments to a function is 30
* Number of conditional format conditions on a cell is 3
+ * Length of text cell contents is unlimited
+ * Length of text cell contents is 32767
*
*/
- EXCEL97(0x10000, 0x0100, 30, 3),
+ EXCEL97(0x10000, 0x0100, 30, 3, 32767),
/**
* Excel2007
@@ -49,21 +51,24 @@ public enum SpreadsheetVersion {
* The maximum number of arguments to a function is 255
* Number of conditional format conditions on a cell is unlimited
* (actually limited by available memory in Excel)
+ * Length of text cell contents is unlimited
*
*/
- EXCEL2007(0x100000, 0x4000, 255, Integer.MAX_VALUE);
+ EXCEL2007(0x100000, 0x4000, 255, Integer.MAX_VALUE, Integer.MAX_VALUE);
private final int _maxRows;
private final int _maxColumns;
private final int _maxFunctionArgs;
private final int _maxCondFormats;
+ private final int _maxTextLength;
- private SpreadsheetVersion(int maxRows, int maxColumns, int maxFunctionArgs, int maxCondFormats) {
+ private SpreadsheetVersion(int maxRows, int maxColumns, int maxFunctionArgs, int maxCondFormats, int maxText) {
_maxRows = maxRows;
_maxColumns = maxColumns;
_maxFunctionArgs = maxFunctionArgs;
_maxCondFormats = maxCondFormats;
- }
+ _maxTextLength = maxText;
+ }
/**
* @return the maximum number of usable rows in each spreadsheet
@@ -116,4 +121,12 @@ public enum SpreadsheetVersion {
public String getLastColumnName() {
return CellReference.convertNumToColString(getLastColumnIndex());
}
+
+ /**
+ * @return the maximum length of a text cell
+ */
+ public int getMaxTextLength() {
+ return _maxTextLength;
+ }
+
}
diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java b/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java
index 771dd838b..e55dd2d05 100644
--- a/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java
+++ b/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java
@@ -38,7 +38,7 @@ import org.apache.poi.hssf.record.NameRecord;
import org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate;
import org.apache.poi.hssf.record.formula.DeletedArea3DPtg;
import org.apache.poi.hssf.record.formula.Ptg;
-import org.apache.poi.ss.usermodel.BaseTestBugzillaIssues;
+import org.apache.poi.ss.usermodel.*;
import org.apache.poi.util.TempFile;
/**
@@ -1494,4 +1494,19 @@ public final class TestBugs extends BaseTestBugzillaIssues {
assertEquals(893, wb.getNumberOfNames());
assertEquals("Matthew\\Matthew11_1\\Matthew2331_1\\Matthew2351_1\\Matthew2361_1___lab", wb.getNameName(300));
}
+
+ /**
+ * HSSFRichTextString.length() returns negative for really long strings.
+ * The test file was created in OpenOffice 3.0 as Excel does not allow cell text longer than 32,767 characters
+ */
+ public void test46368() {
+ HSSFWorkbook wb = openSample("46368.xls");
+ HSSFSheet s = wb.getSheetAt(0);
+ HSSFCell cell1 = s.getRow(0).getCell(0);
+ assertEquals(32770, cell1.getStringCellValue().length());
+
+ HSSFCell cell2 = s.getRow(2).getCell(0);
+ assertEquals(32766, cell2.getStringCellValue().length());
+ }
+
}
diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java
index fdad33fec..540e71038 100644
--- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java
+++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java
@@ -19,6 +19,7 @@ package org.apache.poi.hssf.usermodel;
import java.util.Date;
import java.util.GregorianCalendar;
+import java.io.FileOutputStream;
import junit.framework.AssertionFailedError;
@@ -30,6 +31,8 @@ import org.apache.poi.hssf.record.Record;
import org.apache.poi.hssf.record.StringRecord;
import org.apache.poi.ss.usermodel.BaseTestCell;
import org.apache.poi.ss.usermodel.ErrorConstants;
+import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.ss.SpreadsheetVersion;
/**
* Tests various functionality having to do with {@link HSSFCell}. For instance support for
@@ -291,5 +294,34 @@ public final class TestHSSFCell extends BaseTestCell {
Record dbcr = recs[index++];
assertEquals(DBCellRecord.class, dbcr.getClass());
}
+
+ /**
+ * The maximum length of cell contents (text) is 32,767 characters.
+ */
+ public void testMaxTextLength(){
+ HSSFSheet sheet = new HSSFWorkbook().createSheet();
+ HSSFCell cell = sheet.createRow(0).createCell(0);
+
+ int maxlen = SpreadsheetVersion.EXCEL97.getMaxTextLength();
+ assertEquals(32767, maxlen);
+
+ StringBuffer b = new StringBuffer() ;
+
+ // 32767 is okay
+ for( int i = 0 ; i < maxlen ; i++ )
+ {
+ b.append( "X" ) ;
+ }
+ cell.setCellValue(b.toString());
+
+ b.append("X");
+ // 32768 produces an invalid XLS file
+ try {
+ cell.setCellValue(b.toString());
+ fail("Expected exception");
+ } catch (IllegalArgumentException e){
+ assertEquals("The maximum length of cell contents (text) is 32,767 characters", e.getMessage());
+ }
+ }
}
diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java
index 7f8edf040..b81be5b40 100755
--- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java
+++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java
@@ -218,38 +218,6 @@ public abstract class BaseTestBugzillaIssues extends TestCase {
assertTrue("no errors parsing formula", true);
}
- /**
- * HSSFRichTextString.length() returns negative for really
- * long strings
- */
- public void test46368() {
- Workbook wb = getTestDataProvider().createWorkbook();
- Sheet s = wb.createSheet();
- Row r = s.createRow(0);
- for(int i=0; i<15; i++) {
- int len = 32760 + i;
- Cell c = r.createCell(i);
-
- StringBuffer sb = new StringBuffer();
- for(int j=0; j