public void setCellValue(RichTextString value)
{
- HSSFRichTextString hvalue = (HSSFRichTextString) value;
int row=_record.getRow();
short col=_record.getColumn();
short styleIndex=_record.getXFIndex();
- if (hvalue == null)
+ if (value == null)
{
notifyFormulaChanging();
setCellType(CELL_TYPE_BLANK, false, row, col, styleIndex);
return;
}
- if(hvalue.length() > SpreadsheetVersion.EXCEL97.getMaxTextLength()){
+ if(value.length() > SpreadsheetVersion.EXCEL97.getMaxTextLength()){
throw new IllegalArgumentException("The maximum length of cell contents (text) is 32,767 characters");
}
// Set the 'pre-evaluated result' for the formula
// note - formulas do not preserve text formatting.
FormulaRecordAggregate fr = (FormulaRecordAggregate) _record;
- fr.setCachedStringResult(hvalue.getString());
+ fr.setCachedStringResult(value.getString());
// Update our local cache to the un-formatted version
_stringValue = new HSSFRichTextString(value.getString());
}
int index = 0;
+ HSSFRichTextString hvalue = (HSSFRichTextString) value;
UnicodeString str = hvalue.getUnicodeString();
index = _book.getWorkbook().addSSTString(str);
(( LabelSSTRecord ) _record).setSSTIndex(index);
import java.util.Calendar;
import java.util.Date;
+import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.formula.FormulaParseException;
import org.apache.poi.ss.formula.eval.ErrorEval;
import org.apache.poi.ss.usermodel.Cell;
public void setCellValue(RichTextString value)
{
ensureRichTextStringType();
+
+ if(value.length() > SpreadsheetVersion.EXCEL2007.getMaxTextLength()){
+ throw new IllegalArgumentException("The maximum length of cell contents (text) is 32,767 characters");
+ }
+
((RichTextValue)_value).setValue(value);
}
public void setCellValue(String value)
{
ensureTypeOrFormulaType(CELL_TYPE_STRING);
+
+ if(value.length() > SpreadsheetVersion.EXCEL2007.getMaxTextLength()){
+ throw new IllegalArgumentException("The maximum length of cell contents (text) is 32,767 characters");
+ }
+
if(_value.getType()==CELL_TYPE_FORMULA)
((StringFormulaValue)_value).setPreEvaluatedValue(value);
else
setCellType(Cell.CELL_TYPE_BLANK);
return;
}
+
+ if(str.length() > SpreadsheetVersion.EXCEL2007.getMaxTextLength()){
+ throw new IllegalArgumentException("The maximum length of cell contents (text) is 32,767 characters");
+ }
+
int cellType = getCellType();
switch(cellType){
case Cell.CELL_TYPE_FORMULA:
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.ss.ITestDataProvider;
+import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.xssf.SXSSFITestDataProvider;
import org.apache.poi.xssf.XSSFITestDataProvider;
import org.apache.poi.xssf.streaming.SXSSFCell;
// invalid characters are replaced with question marks
assertEquals("???<>\t\n\u00a0 &\"POI\'\u2122", wb.getSheetAt(0).getRow(0).getCell(0).getStringCellValue());
-
- }
-
- public void testEncodingbeloAscii(){
- Workbook xwb = XSSFITestDataProvider.instance.createWorkbook();
- Cell xCell = xwb.createSheet().createRow(0).createCell(0);
-
- Workbook swb = SXSSFITestDataProvider.instance.createWorkbook();
- Cell sCell = swb.createSheet().createRow(0).createCell(0);
-
- StringBuffer sb = new StringBuffer();
- // test all possible characters
- for(int i = 0; i < Character.MAX_VALUE; i++) sb.append((char)i) ;
-
- String str = sb.toString();
-
- xCell.setCellValue(str);
- assertEquals(str, xCell.getStringCellValue());
- sCell.setCellValue(str);
- assertEquals(str, sCell.getStringCellValue());
-
- xwb = XSSFITestDataProvider.instance.writeOutAndReadBack(xwb);
- swb = SXSSFITestDataProvider.instance.writeOutAndReadBack(swb);
- xCell = xwb.getSheetAt(0).createRow(0).createCell(0);
- sCell = swb.getSheetAt(0).createRow(0).createCell(0);
-
- assertEquals(xCell.getStringCellValue(), sCell.getStringCellValue());
}
}
public XSSFWorkbook writeOutAndReadBack(Workbook original) {
if(!(original instanceof XSSFWorkbook)) {
- throw new IllegalArgumentException("Expected an instance of XSSFWorkbook");
+ throw new IllegalArgumentException("Expected an instance of XSSFWorkbook, but had " + original.getClass());
}
return XSSFTestDataSamples.writeOutAndReadBack((XSSFWorkbook)original);
}
import java.io.IOException;
+import org.apache.poi.hssf.HSSFITestDataProvider;
+import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.usermodel.BaseTestXCell;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellReference;
+import org.apache.poi.xssf.SXSSFITestDataProvider;
import org.apache.poi.xssf.XSSFITestDataProvider;
import org.apache.poi.xssf.XSSFTestDataSamples;
import org.apache.poi.xssf.model.SharedStringsTable;
wb.close();
}
}
+
+ public void testEncodingbeloAscii(){
+ StringBuffer sb = new StringBuffer();
+ // test all possible characters
+ for(int i = 0; i < Character.MAX_VALUE; i++) {
+ sb.append((char)i);
+ }
+
+ String strAll = sb.toString();
+
+ // process in chunks as we have a limit on size of column now
+ int pos = 0;
+ while(pos < strAll.length()) {
+ String str = strAll.substring(pos, Math.min(strAll.length(), pos+SpreadsheetVersion.EXCEL2007.getMaxTextLength()));
+
+ Workbook wb = HSSFITestDataProvider.instance.createWorkbook();
+ Cell cell = wb.createSheet().createRow(0).createCell(0);
+
+ Workbook xwb = XSSFITestDataProvider.instance.createWorkbook();
+ Cell xCell = xwb.createSheet().createRow(0).createCell(0);
+
+ Workbook swb = SXSSFITestDataProvider.instance.createWorkbook();
+ Cell sCell = swb.createSheet().createRow(0).createCell(0);
+
+ cell.setCellValue(str);
+ assertEquals(str, cell.getStringCellValue());
+ xCell.setCellValue(str);
+ assertEquals(str, xCell.getStringCellValue());
+ sCell.setCellValue(str);
+ assertEquals(str, sCell.getStringCellValue());
+
+ Workbook wbBack = HSSFITestDataProvider.instance.writeOutAndReadBack(wb);
+ Workbook xwbBack = XSSFITestDataProvider.instance.writeOutAndReadBack(xwb);
+ Workbook swbBack = SXSSFITestDataProvider.instance.writeOutAndReadBack(swb);
+ cell = wbBack.getSheetAt(0).createRow(0).createCell(0);
+ xCell = xwbBack.getSheetAt(0).createRow(0).createCell(0);
+ sCell = swbBack.getSheetAt(0).createRow(0).createCell(0);
+
+ assertEquals(cell.getStringCellValue(), xCell.getStringCellValue());
+ assertEquals(cell.getStringCellValue(), sCell.getStringCellValue());
+
+ pos += SpreadsheetVersion.EXCEL97.getMaxTextLength();
+ }
+ }
}
import org.apache.poi.hssf.record.FormulaRecord;
import org.apache.poi.hssf.record.Record;
import org.apache.poi.hssf.record.StringRecord;
-import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.usermodel.BaseTestCell;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.FormulaError;
assertEquals(DBCellRecord.class, dbcr.getClass());
}
- /**
- * The maximum length of cell contents (text) is 32,767 characters.
- * @throws IOException
- */
- public void testMaxTextLength() throws IOException{
- HSSFWorkbook wb = new HSSFWorkbook();
- HSSFSheet sheet = wb.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());
- }
- wb.close();
- }
-
/**
* HSSF prior to version 3.7 had a bug: it could write a NaN but could not read such a file back.
*/
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.ITestDataProvider;
+import org.apache.poi.ss.SpreadsheetVersion;
/**
* Common superclass for testing implementations of
wb.close();
}
+
+ public void test57008() throws IOException {
+ Workbook wb = _testDataProvider.createWorkbook();
+ Sheet sheet = wb.createSheet();
+
+ Row row0 = sheet.createRow(0);
+ Cell cell0 = row0.createCell(0);
+ cell0.setCellValue("row 0, cell 0 _x0046_ without changes");
+
+ Cell cell1 = row0.createCell(1);
+ cell1.setCellValue("row 0, cell 1 _x005fx0046_ with changes");
+
+ Cell cell2 = row0.createCell(2);
+ cell2.setCellValue("hgh_x0041_**_x0100_*_x0101_*_x0190_*_x0200_*_x0300_*_x0427_*");
+
+ checkUnicodeValues(wb);
+
+// String fname = "/tmp/Test_xNNNN_inCell" + (wb instanceof HSSFWorkbook ? ".xls" : ".xlsx");
+// FileOutputStream out = new FileOutputStream(fname);
+// try {
+// wb.write(out);
+// } finally {
+// out.close();
+// }
+
+ Workbook wbBack = _testDataProvider.writeOutAndReadBack(wb);
+ checkUnicodeValues(wbBack);
+ }
+
+ private void checkUnicodeValues(Workbook wb) {
+ assertEquals((wb instanceof HSSFWorkbook ? "row 0, cell 0 _x0046_ without changes" : "row 0, cell 0 F without changes"),
+ wb.getSheetAt(0).getRow(0).getCell(0).toString());
+ assertEquals((wb instanceof HSSFWorkbook ? "row 0, cell 1 _x005fx0046_ with changes" : "row 0, cell 1 _x005fx0046_ with changes"),
+ wb.getSheetAt(0).getRow(0).getCell(1).toString());
+ assertEquals((wb instanceof HSSFWorkbook ? "hgh_x0041_**_x0100_*_x0101_*_x0190_*_x0200_*_x0300_*_x0427_*" : "hghA**\u0100*\u0101*\u0190*\u0200*\u0300*\u0427*"),
+ wb.getSheetAt(0).getRow(0).getCell(2).toString());
+ }
+
+ /**
+ * The maximum length of cell contents (text) is 32,767 characters.
+ * @throws IOException
+ */
+ public void testMaxTextLength() throws IOException{
+ Workbook wb = _testDataProvider.createWorkbook();
+ Sheet sheet = wb.createSheet();
+ Cell cell = sheet.createRow(0).createCell(0);
+
+ int maxlen = wb instanceof HSSFWorkbook ?
+ SpreadsheetVersion.EXCEL97.getMaxTextLength()
+ : SpreadsheetVersion.EXCEL2007.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());
+ }
+ wb.close();
+ }
}