package org.apache.poi.xssf.streaming;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
-import org.apache.poi.ss.usermodel.Cell;
-import org.apache.poi.ss.usermodel.CellStyle;
-import org.apache.poi.ss.usermodel.Sheet;
-import org.apache.poi.ss.usermodel.Row;
-import org.apache.poi.ss.usermodel.RichTextString;
-import org.apache.poi.ss.usermodel.Comment;
-import org.apache.poi.ss.usermodel.Hyperlink;
-import org.apache.poi.ss.usermodel.DateUtil;
+import org.apache.poi.ss.formula.eval.ErrorEval;
+import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.formula.FormulaParseException;
import org.apache.poi.ss.util.CellRangeAddress;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellType;
/**
* Streaming version of XSSFRow implementing the "BigGridDemo" strategy.
*/
public int getCachedFormulaResultType()
{
-//TODO: Implement this correctly
- assert false;
- return CELL_TYPE_NUMERIC;
+ if (_value.getType() != CELL_TYPE_FORMULA) {
+ throw new IllegalStateException("Only formula cells have cached results");
+ }
+
+ return ((FormulaValue)_value).getFormulaType();
}
/**
*/
public void setCellValue(double value)
{
- ensureTypeOrFormulaType(CELL_TYPE_NUMERIC);
- if(_value.getType()==CELL_TYPE_FORMULA)
- ((NumericFormulaValue)_value).setPreEvaluatedValue(value);
- else
- ((NumericValue)_value).setValue(value);
+ if(Double.isInfinite(value)) {
+ // Excel does not support positive/negative infinities,
+ // rather, it gives a #DIV/0! error in these cases.
+ setCellErrorValue(FormulaError.DIV0.getCode());
+ } else if (Double.isNaN(value)){
+ setCellErrorValue(FormulaError.NUM.getCode());
+ } else {
+ ensureTypeOrFormulaType(CELL_TYPE_NUMERIC);
+ if(_value.getType()==CELL_TYPE_FORMULA)
+ ((NumericFormulaValue)_value).setPreEvaluatedValue(value);
+ else
+ ((NumericValue)_value).setValue(value);
+ }
}
/**
*/
public void setCellFormula(String formula) throws FormulaParseException
{
+ if(formula == null) {
+ setType(Cell.CELL_TYPE_BLANK);
+ return;
+ }
+
ensureFormulaType(computeTypeFromFormula(formula));
((FormulaValue)_value).setValue(formula);
}
public RichTextString getRichStringCellValue()
{
int cellType = getCellType();
- if(!(getCellType()==CELL_TYPE_STRING&&((StringValue)_value).isRichText()))
+ if(getCellType() != CELL_TYPE_STRING)
throw typeMismatch(CELL_TYPE_STRING, cellType, false);
- return ((RichTextValue)_value).getValue();
+
+ StringValue sval = (StringValue)_value;
+ if(sval.isRichText())
+ return ((RichTextValue)_value).getValue();
+ else {
+ String plainText = getStringCellValue();
+ return getSheet().getWorkbook().getCreationHelper().createRichTextString(plainText);
+ }
}
*/
public CellStyle getCellStyle()
{
- return _style;
+ if(_style == null){
+ SXSSFWorkbook wb = (SXSSFWorkbook)getRow().getSheet().getWorkbook();
+ return wb.getCellStyleAt((short)0);
+ } else {
+ return _style;
+ }
}
/**
}
//end of interface implementation
+ /**
+ * Returns a string representation of the cell
+ * <p>
+ * Formula cells return the formula string, rather than the formula result.
+ * Dates are displayed in dd-MMM-yyyy format
+ * Errors are displayed as #ERR<errIdx>
+ * </p>
+ */
+ public String toString() {
+ switch (getCellType()) {
+ case CELL_TYPE_BLANK:
+ return "";
+ case CELL_TYPE_BOOLEAN:
+ return getBooleanCellValue() ? "TRUE" : "FALSE";
+ case CELL_TYPE_ERROR:
+ return ErrorEval.getText(getErrorCellValue());
+ case CELL_TYPE_FORMULA:
+ return getCellFormula();
+ case CELL_TYPE_NUMERIC:
+ if (DateUtil.isCellDateFormatted(this)) {
+ DateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
+ return sdf.format(getDateCellValue());
+ }
+ return getNumericCellValue() + "";
+ case CELL_TYPE_STRING:
+ return getRichStringCellValue().toString();
+ default:
+ return "Unknown Cell Type: " + getCellType();
+ }
+ }
+
void removeProperty(int type)
{
Property current=_firstProperty;
}
case CELL_TYPE_STRING:
{
- _value=new PlainStringValue();
+ PlainStringValue sval = new PlainStringValue();
+ if(_value != null){
+ // if a cell is not blank then convert the old value to string
+ String str = convertCellValueToString();
+ sval.setValue(str);
+ }
+ _value = sval;
break;
}
case CELL_TYPE_FORMULA:
}
case CELL_TYPE_BOOLEAN:
{
- _value=new BooleanValue();
+ BooleanValue bval = new BooleanValue();
+ if(_value != null){
+ // if a cell is not blank then convert the old value to string
+ boolean val = convertCellValueToBoolean();
+ bval.setValue(val);
+ }
+ _value = bval;
break;
}
case CELL_TYPE_ERROR:
}
return "#unknown cell type (" + cellTypeCode + ")#";
}
+ private boolean convertCellValueToBoolean() {
+ int cellType = getCellType();
+
+ if (cellType == CELL_TYPE_FORMULA) {
+ cellType = getCachedFormulaResultType();
+ }
+
+ switch (cellType) {
+ case CELL_TYPE_BOOLEAN:
+ return getBooleanCellValue();
+ case CELL_TYPE_STRING:
+
+ String text = getStringCellValue();
+ return Boolean.parseBoolean(text);
+ case CELL_TYPE_NUMERIC:
+ return getNumericCellValue() != 0;
+ case CELL_TYPE_ERROR:
+ case CELL_TYPE_BLANK:
+ return false;
+ }
+ throw new RuntimeException("Unexpected cell type (" + cellType + ")");
+ }
+ private String convertCellValueToString() {
+ int cellType = getCellType();
+
+ switch (cellType) {
+ case CELL_TYPE_BLANK:
+ return "";
+ case CELL_TYPE_BOOLEAN:
+ return getBooleanCellValue() ? "TRUE" : "FALSE";
+ case CELL_TYPE_STRING:
+ return getStringCellValue();
+ case CELL_TYPE_NUMERIC:
+ case CELL_TYPE_ERROR:
+ byte errVal = getErrorCellValue();
+ return FormulaError.forInt(errVal).getString();
+ case CELL_TYPE_FORMULA:
+ return "";
+ default:
+ throw new IllegalStateException("Unexpected cell type (" + cellType + ")");
+ }
+ }
+
//END OF COPIED CODE
static abstract class Property
return false;
}
}
- static class RichTextValue implements Value
+ static class RichTextValue extends StringValue
{
RichTextString _value;
public int getType()
SXSSFCell[] _cells;
int _maxColumn=-1;
short _height=-1;
-//TODO: Need to set the correct default value for _zHeight
- boolean _zHeight;
+ boolean _zHeight = false;
public SXSSFRow(SXSSFSheet sheet, int initialSize)
{
* @return Cell representing that column or null if undefined.
* @see #getCell(int, org.apache.poi.ss.usermodel.Row.MissingCellPolicy)
*/
- public Cell getCell(int cellnum)
- {
- return cellnum>_maxColumn?null:_cells[cellnum];
+ public Cell getCell(int cellnum) {
+ if(cellnum < 0) throw new IllegalArgumentException("Cell index must be >= 0");
+
+ Cell cell = cellnum > _maxColumn ? null : _cells[cellnum];
+
+ MissingCellPolicy policy = _sheet.getWorkbook().getMissingCellPolicy();
+ if(policy == RETURN_NULL_AND_BLANK) {
+ return cell;
+ }
+ if (policy == RETURN_BLANK_AS_NULL) {
+ if (cell == null) return cell;
+ if (cell.getCellType() == Cell.CELL_TYPE_BLANK) {
+ return null;
+ }
+ return cell;
+ }
+ if (policy == CREATE_NULL_AS_BLANK) {
+ if (cell == null) {
+ return createCell((short) cellnum, Cell.CELL_TYPE_BLANK);
+ }
+ return cell;
+ }
+ throw new IllegalArgumentException("Illegal policy " + policy + " (" + policy.id + ")");
}
/**
*/
public short getLastCellNum()
{
- return (short)(_maxColumn+1);
+ return _maxColumn == -1 ? -1 : (short)(_maxColumn+1);
}
/**
public class FilledCellIterator implements Iterator<Cell>
{
int pos=0;
+
+ FilledCellIterator(){
+ for (int i = 0; i <= _maxColumn; i++) {
+ if (_cells[i] != null) {
+ pos = i;
+ break;
+ }
+ }
+ }
+
public boolean hasNext()
{
return pos <= _maxColumn;
import java.io.InputStream;
import java.io.FileInputStream;
-import org.apache.poi.ss.usermodel.Sheet;
-import org.apache.poi.ss.usermodel.Row;
-import org.apache.poi.ss.usermodel.Cell;
-import org.apache.poi.ss.usermodel.CellStyle;
-import org.apache.poi.ss.usermodel.PrintSetup;
-import org.apache.poi.ss.usermodel.Header;
-import org.apache.poi.ss.usermodel.Footer;
-import org.apache.poi.ss.usermodel.Comment;
-import org.apache.poi.ss.usermodel.Drawing;
-import org.apache.poi.ss.usermodel.Workbook;
-import org.apache.poi.ss.usermodel.DataValidation;
-import org.apache.poi.ss.usermodel.CellRange;
-import org.apache.poi.ss.usermodel.DataValidationHelper;
-import org.apache.poi.ss.usermodel.AutoFilter;
+import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.usermodel.XSSFSheet;
*/
public void removeRow(Row row)
{
+ if (row.getSheet() != this) {
+ throw new IllegalArgumentException("Specified row does not belong to this sheet");
+ }
+
for(Iterator<Map.Entry<Integer,SXSSFRow>> iter=_rows.entrySet().iterator();iter.hasNext();)
{
Map.Entry<Integer,SXSSFRow> entry=iter.next();
*/
public int getFirstRowNum()
{
- if(_writer.getNumberOfFlushedRows()>0)
+ if(_writer.getNumberOfFlushedRows() > 0)
return _writer.getLowestIndexOfFlushedRows();
- Integer firstKey=_rows.firstKey();
- return firstKey==null?-1:firstKey.intValue();
+ return _rows.size() == 0 ? 0 : _rows.firstKey();
}
/**
*/
public int getLastRowNum()
{
- Integer lastKey=_rows.lastKey();
- return lastKey==null?-1:lastKey.intValue();
+ return _rows.size() == 0 ? 0 : _rows.lastKey();
}
/**
_out.write("<row r=\""+(rownum+1)+"\"");
if(row.hasCustomHeight())
_out.write(" customHeight=\"true\" ht=\""+row.getHeightInPoints()+"\"");
+ if(row.getZeroHeight())
+ _out.write(" hidden=\"true\"");
_out.write(">\n");
this._rownum = rownum;
_rowContainedNullCells=false;
String ref = new CellReference(_rownum, columnIndex).formatAsString();
_out.write("<c r=\""+ref+"\"");
CellStyle cellStyle=cell.getCellStyle();
- if(cellStyle!=null) _out.write(" s=\""+cellStyle.getIndex()+"\"");
+ if(cellStyle.getIndex() != 0) _out.write(" s=\""+cellStyle.getIndex()+"\"");
int cellType=cell.getCellType();
switch(cellType)
{
case Cell.CELL_TYPE_BLANK:
{
-//TODO: What needs to be done here?
+ _out.write(">");
+ break;
}
case Cell.CELL_TYPE_FORMULA:
{
-//TODO: What needs to be done here?
+ _out.write(">");
+ _out.write("<f>");
+ outputQuotedString(cell.getCellFormula());
+ _out.write("</f>");
+ switch (cell.getCachedFormulaResultType()){
+ case Cell.CELL_TYPE_NUMERIC:
+ double nval = cell.getNumericCellValue();
+ if(!Double.isNaN(nval)){
+ _out.write("<v>"+nval+"</v>");
+ }
+ break;
+ }
+ break;
}
case Cell.CELL_TYPE_STRING:
{
}
case Cell.CELL_TYPE_BOOLEAN:
{
- _out.write(" t=\"n\">");
+ _out.write(" t=\"b\">");
_out.write("<v>"+(cell.getBooleanCellValue()?"1":"0")+"</v>");
break;
}
case Cell.CELL_TYPE_ERROR:
{
-//TODO: What needs to be done here?
- _out.write(" t=\"inlineStr\">");
- _out.write("<is><t></t></is>");
+ FormulaError error = FormulaError.forInt(cell.getErrorCellValue());
+
+ _out.write(" t=\"e\">");
+ _out.write("<v>" + error.getString() +"</v>");
break;
}
default:
*/
public Sheet cloneSheet(int sheetNum)
{
- return createAndRegisterSXSSFSheet(_wb.cloneSheet(sheetNum));
+ throw new RuntimeException("NotImplemented");
}
* Returns a CellValue wrapper around the supplied ValueEval instance.
*/
private CellValue evaluateFormulaCellValue(Cell cell) {
+ if(!(cell instanceof XSSFCell)){
+ throw new IllegalArgumentException("Unexpected type of cell: " + cell.getClass() + "." +
+ " Only XSSFCells can be evaluated.");
+ }
+
ValueEval eval = _bookEvaluator.evaluate(new XSSFEvaluationCell((XSSFCell) cell));
if (eval instanceof NumberEval) {
NumberEval ne = (NumberEval) eval;
super(SXSSFITestDataProvider.instance);
}
- @Override
- public void testSetValues() {
- // TODO fix me
- }
-
- @Override
- public void testBoolErr() {
- // TODO fix me
- }
-
- @Override
- public void testFormulaStyle() {
- // TODO fix me
- }
-
- @Override
- public void testToString() {
- // TODO fix me
- }
-
- @Override
- public void testSetFormulaValue() {
- // TODO fix me
- }
-
- @Override
- public void testChangeTypeStringToBool() {
- // TODO fix me
- }
-
- @Override
- public void testChangeTypeBoolToString() {
- // TODO fix me
- }
-
+ /**
+ * this test involves evaluation of formulas which isn't supported for SXSSF
+ */
@Override
public void testConvertStringFormulaCell() {
- // TODO fix me
+ try {
+ super.testConvertStringFormulaCell();
+ fail("expected exception");
+ } catch (IllegalArgumentException e){
+ assertEquals(
+ "Unexpected type of cell: class org.apache.poi.xssf.streaming.SXSSFCell. " +
+ "Only XSSFCells can be evaluated.", e.getMessage());
+ }
}
+ /**
+ * this test involves evaluation of formulas which isn't supported for SXSSF
+ */
@Override
public void testSetTypeStringOnFormulaCell() {
- // TODO fix me
- }
-
- @Override
- public void testChangeTypeFormulaToBoolean() {
- // TODO fix me
- }
-
- @Override
- public void test40296() {
- // TODO fix me
- }
-
- @Override
- public void testNanAndInfinity() {
- // TODO fix me
+ try {
+ super.testSetTypeStringOnFormulaCell();
+ fail("expected exception");
+ } catch (IllegalArgumentException e){
+ assertEquals(
+ "Unexpected type of cell: class org.apache.poi.xssf.streaming.SXSSFCell. " +
+ "Only XSSFCells can be evaluated.", e.getMessage());
+ }
}
}
package org.apache.poi.xssf.usermodel.streaming;
-import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.usermodel.BaseTestRow;
import org.apache.poi.xssf.SXSSFITestDataProvider;
-import org.apache.poi.xssf.XSSFITestDataProvider;
/**
* Tests for XSSFRow
public TestSXSSFRow() {
super(SXSSFITestDataProvider.instance);
}
-
- public void testRowBounds() {
- //TODO fix me
- //baseTestRowBounds(SpreadsheetVersion.EXCEL2007.getLastRowIndex());
- }
-
- public void testCellBounds() {
- //TODO fix me
- //baseTestCellBounds(SpreadsheetVersion.EXCEL2007.getLastColumnIndex());
- }
-
- @Override
- public void testLastAndFirstColumns() {
- //TODO fix me
- }
-
- @Override
- public void testRemoveCell() {
- //TODO fix me
- }
-
- @Override
- public void testLastCellNumIsCorrectAfterAddCell_bug43901() {
- //TODO fix me
- }
-
- @Override
- public void testGetCellPolicy() {
- //TODO fix me
- }
-
- @Override
- public void testRowHeight() {
- //TODO fix me
- }
-
- @Override
- public void testCellIterator() {
- //TODO fix me
- }
}
super(SXSSFITestDataProvider.instance);
}
+ /**
+ * cloning of sheets is not supported in SXSSF
+ */
@Override
- public void testRemoveRow(){
- // TODO fix me
+ public void testCloneSheet() {
+ try {
+ super.testCloneSheet();
+ fail("expected exception");
+ } catch (RuntimeException e){
+ assertEquals("NotImplemented", e.getMessage());
+ }
}
@Override
- public void testCloneSheet(){
- // TODO fix me
+ public void testCloneSheetMultipleTimes() {
+ try {
+ super.testCloneSheetMultipleTimes();
+ fail("expected exception");
+ } catch (RuntimeException e){
+ assertEquals("NotImplemented", e.getMessage());
+ }
}
-
+ /**
+ * shifting rows is not supported in SXSSF
+ */
@Override
public void testShiftMerged(){
- // TODO fix me
+ try {
+ super.testShiftMerged();
+ fail("expected exception");
+ } catch (RuntimeException e){
+ assertEquals("NotImplemented", e.getMessage());
+ }
}
+ /**
+ * Bug 35084: cloning cells with formula
+ *
+ * The test is disabled because cloning of sheets is not supported in SXSSF
+ */
@Override
public void test35084(){
- // TODO fix me
+ try {
+ super.test35084();
+ fail("expected exception");
+ } catch (RuntimeException e){
+ assertEquals("NotImplemented", e.getMessage());
+ }
}
@Override
public void testDefaultColumnStyle() {
- // TODO fix me
+ //TODO column styles are not yet supported by XSSF
}
}
super(SXSSFITestDataProvider.instance);
}
+ /**
+ * cloning of sheets is not supported in SXSSF
+ */
@Override
public void testCloneSheet() {
- // TODO figure out why the base class failes and remove me
- }
-
- @Override
- public void testUnicodeInAll() {
- // TODO figure out why the base class failes and remove me
+ try {
+ super.testCloneSheet();
+ fail("expected exception");
+ } catch (RuntimeException e){
+ assertEquals("NotImplemented", e.getMessage());
+ }
}
+ /**
+ * this test involves evaluation of formulas which isn't supported for SXSSF
+ */
@Override
public void testSetSheetName() {
- // this test involves formula evaluation which isn't supportd by SXSSF
+ try {
+ super.testSetSheetName();
+ fail("expected exception");
+ } catch (Exception e){
+ assertEquals(
+ "Unexpected type of cell: class org.apache.poi.xssf.streaming.SXSSFCell. " +
+ "Only XSSFCells can be evaluated.", e.getMessage());
+ }
}
+
}