diff options
Diffstat (limited to 'src/ooxml/java/org/apache')
10 files changed, 435 insertions, 373 deletions
diff --git a/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExcelExtractor.java b/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExcelExtractor.java index 923c5aed84..719dd636e5 100644 --- a/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExcelExtractor.java +++ b/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExcelExtractor.java @@ -26,6 +26,7 @@ import org.apache.poi.openxml4j.exceptions.OpenXML4JException; import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.Comment; import org.apache.poi.ss.usermodel.DataFormatter; import org.apache.poi.ss.usermodel.HeaderFooter; @@ -161,19 +162,19 @@ public class XSSFExcelExtractor extends POIXMLTextExtractor Cell cell = ri.next(); // Is it a formula one? - if(cell.getCellType() == Cell.CELL_TYPE_FORMULA) { + if(cell.getCellType() == CellType.FORMULA) { if (formulasNotResults) { String contents = cell.getCellFormula(); checkMaxTextSize(text, contents); text.append(contents); } else { - if (cell.getCachedFormulaResultType() == Cell.CELL_TYPE_STRING) { + if (cell.getCachedFormulaResultType() == CellType.STRING) { handleStringCell(text, cell); } else { handleNonStringCell(text, cell, formatter); } } - } else if(cell.getCellType() == Cell.CELL_TYPE_STRING) { + } else if(cell.getCellType() == CellType.STRING) { handleStringCell(text, cell); } else { handleNonStringCell(text, cell, formatter); @@ -235,12 +236,12 @@ public class XSSFExcelExtractor extends POIXMLTextExtractor } private void handleNonStringCell(StringBuffer text, Cell cell, DataFormatter formatter) { - int type = cell.getCellType(); - if (type == Cell.CELL_TYPE_FORMULA) { + CellType type = cell.getCellType(); + if (type == CellType.FORMULA) { type = cell.getCachedFormulaResultType(); } - if (type == Cell.CELL_TYPE_NUMERIC) { + if (type == CellType.NUMERIC) { CellStyle cs = cell.getCellStyle(); if (cs != null && cs.getDataFormatString() != null) { diff --git a/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExportToXml.java b/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExportToXml.java index d187b66ca1..f9d3c876f1 100644 --- a/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExportToXml.java +++ b/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExportToXml.java @@ -42,7 +42,7 @@ import javax.xml.validation.SchemaFactory; import javax.xml.validation.Validator; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; -import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.util.DocumentHelper; import org.apache.poi.util.LocaleUtil; @@ -280,11 +280,11 @@ public class XSSFExportToXml implements Comparator<String>{ String value =""; switch (cell.getCellType()) { - case XSSFCell.CELL_TYPE_STRING: value = cell.getStringCellValue(); break; - case XSSFCell.CELL_TYPE_BOOLEAN: value += cell.getBooleanCellValue(); break; - case XSSFCell.CELL_TYPE_ERROR: value = cell.getErrorCellString(); break; - case XSSFCell.CELL_TYPE_FORMULA: - if (cell.getCachedFormulaResultType() == Cell.CELL_TYPE_STRING) { + case STRING: value = cell.getStringCellValue(); break; + case BOOLEAN: value += cell.getBooleanCellValue(); break; + case ERROR: value = cell.getErrorCellString(); break; + case FORMULA: + if (cell.getCachedFormulaResultType() == CellType.STRING) { value = cell.getStringCellValue(); } else { if (DateUtil.isCellDateFormatted(cell)) { @@ -295,7 +295,7 @@ public class XSSFExportToXml implements Comparator<String>{ } break; - case XSSFCell.CELL_TYPE_NUMERIC: + case NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { value = getFormattedDate(cell); } else { diff --git a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCell.java b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCell.java index cf1b419902..5a6ac555e0 100644 --- a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCell.java +++ b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCell.java @@ -28,6 +28,7 @@ import org.apache.poi.ss.formula.FormulaParseException; import org.apache.poi.ss.formula.eval.ErrorEval; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.Comment; import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.FormulaError; @@ -55,7 +56,7 @@ public class SXSSFCell implements Cell { private CellStyle _style; private Property _firstProperty; - public SXSSFCell(SXSSFRow row,int cellType) + public SXSSFCell(SXSSFRow row,CellType cellType) { _row=row; setType(cellType); @@ -119,16 +120,27 @@ public class SXSSFCell implements Cell { * Set the cells type (numeric, formula or string) * * @throws IllegalArgumentException if the specified cell type is invalid - * @see #CELL_TYPE_NUMERIC - * @see #CELL_TYPE_STRING - * @see #CELL_TYPE_FORMULA - * @see #CELL_TYPE_BLANK - * @see #CELL_TYPE_BOOLEAN - * @see #CELL_TYPE_ERROR + * @see CellType#NUMERIC + * @see CellType#STRING + * @see CellType#FORMULA + * @see CellType#BLANK + * @see CellType#BOOLEAN + * @see CellType#ERROR + * @deprecated POI 3.15 beta 3. Use {@link #setCellType(CellType)} instead. */ @Override public void setCellType(int cellType) { + ensureType(CellType.forInt(cellType)); + } + /** + * Set the cells type (numeric, formula or string) + * + * @throws IllegalArgumentException if the specified cell type is invalid + */ + @Override + public void setCellType(CellType cellType) + { ensureType(cellType); } @@ -136,29 +148,23 @@ public class SXSSFCell implements Cell { * Return the cell type. * * @return the cell type - * @see Cell#CELL_TYPE_BLANK - * @see Cell#CELL_TYPE_NUMERIC - * @see Cell#CELL_TYPE_STRING - * @see Cell#CELL_TYPE_FORMULA - * @see Cell#CELL_TYPE_BOOLEAN - * @see Cell#CELL_TYPE_ERROR */ @Override - public int getCellType() + public CellType getCellType() { return _value.getType(); } /** * Only valid for formula cells - * @return one of ({@link #CELL_TYPE_NUMERIC}, {@link #CELL_TYPE_STRING}, - * {@link #CELL_TYPE_BOOLEAN}, {@link #CELL_TYPE_ERROR}) depending + * @return one of ({@link CellType#NUMERIC}, {@link CellType#STRING}, + * {@link CellType#BOOLEAN}, {@link CellType#ERROR}) depending * on the cached value of the formula */ @Override - public int getCachedFormulaResultType() + public CellType getCachedFormulaResultType() { - if (_value.getType() != CELL_TYPE_FORMULA) { + if (_value.getType() != CellType.FORMULA) { throw new IllegalStateException("Only formula cells have cached results"); } @@ -182,8 +188,8 @@ public class SXSSFCell implements Cell { } else if (Double.isNaN(value)){ setCellErrorValue(FormulaError.NUM.getCode()); } else { - ensureTypeOrFormulaType(CELL_TYPE_NUMERIC); - if(_value.getType()==CELL_TYPE_FORMULA) + ensureTypeOrFormulaType(CellType.NUMERIC); + if(_value.getType()==CellType.FORMULA) ((NumericFormulaValue)_value).setPreEvaluatedValue(value); else ((NumericValue)_value).setValue(value); @@ -197,7 +203,7 @@ public class SXSSFCell implements Cell { * <b>Note</b> - There is actually no 'DATE' cell type in Excel. In many * cases (when entering date values), Excel automatically adjusts the * <i>cell style</i> to some date format, creating the illusion that the cell - * data type is now something besides {@link Cell#CELL_TYPE_NUMERIC}. POI + * data type is now something besides {@link CellType#NUMERIC}. POI * does not attempt to replicate this behaviour. To make a numeric cell * display as a date, use {@link #setCellStyle(CellStyle)} etc. * @@ -208,7 +214,7 @@ public class SXSSFCell implements Cell { @Override public void setCellValue(Date value) { if(value == null) { - setCellType(Cell.CELL_TYPE_BLANK); + setCellType(CellType.BLANK); return; } @@ -235,7 +241,7 @@ public class SXSSFCell implements Cell { @Override public void setCellValue(Calendar value) { if(value == null) { - setCellType(Cell.CELL_TYPE_BLANK); + setCellType(CellType.BLANK); return; } @@ -267,7 +273,7 @@ public class SXSSFCell implements Cell { ((RichTextValue)_value).setValue(xvalue); } else { - setCellType(CELL_TYPE_BLANK); + setCellType(CellType.BLANK); } } @@ -283,18 +289,18 @@ public class SXSSFCell implements Cell { public void setCellValue(String value) { if (value != null) { - ensureTypeOrFormulaType(CELL_TYPE_STRING); + ensureTypeOrFormulaType(CellType.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) + if(_value.getType()==CellType.FORMULA) ((StringFormulaValue)_value).setPreEvaluatedValue(value); else ((PlainStringValue)_value).setValue(value); } else { - setCellType(CELL_TYPE_BLANK); + setCellType(CellType.BLANK); } } @@ -313,7 +319,7 @@ public class SXSSFCell implements Cell { public void setCellFormula(String formula) throws FormulaParseException { if(formula == null) { - setType(Cell.CELL_TYPE_BLANK); + setType(CellType.BLANK); return; } @@ -324,13 +330,13 @@ public class SXSSFCell implements Cell { * Return a formula for the cell, for example, <code>SUM(C4:E4)</code> * * @return a formula for the cell - * @throws IllegalStateException if the cell type returned by {@link #getCellType()} is not CELL_TYPE_FORMULA + * @throws IllegalStateException if the cell type returned by {@link #getCellType()} is not CellType.FORMULA */ @Override public String getCellFormula() { - if(_value.getType()!=CELL_TYPE_FORMULA) - throw typeMismatch(CELL_TYPE_FORMULA,_value.getType(),false); + if(_value.getType()!=CellType.FORMULA) + throw typeMismatch(CellType.FORMULA,_value.getType(),false); return ((FormulaValue)_value).getValue(); } @@ -341,29 +347,29 @@ public class SXSSFCell implements Cell { * For formulas or error cells we return the precalculated value; * </p> * @return the value of the cell as a number - * @throws IllegalStateException if the cell type returned by {@link #getCellType()} is CELL_TYPE_STRING + * @throws IllegalStateException if the cell type returned by {@link #getCellType()} is CellType.STRING * @exception NumberFormatException if the cell value isn't a parsable <code>double</code>. * @see org.apache.poi.ss.usermodel.DataFormatter for turning this number into a string similar to that which Excel would render this number as. */ @Override public double getNumericCellValue() { - int cellType = getCellType(); + CellType cellType = getCellType(); switch(cellType) { - case CELL_TYPE_BLANK: + case BLANK: return 0.0; - case CELL_TYPE_FORMULA: + case FORMULA: { FormulaValue fv=(FormulaValue)_value; - if(fv.getFormulaType()!=CELL_TYPE_NUMERIC) - throw typeMismatch(CELL_TYPE_NUMERIC, CELL_TYPE_FORMULA, false); + if(fv.getFormulaType()!=CellType.NUMERIC) + throw typeMismatch(CellType.NUMERIC, CellType.FORMULA, false); return ((NumericFormulaValue)_value).getPreEvaluatedValue(); } - case CELL_TYPE_NUMERIC: + case NUMERIC: return ((NumericValue)_value).getValue(); default: - throw typeMismatch(CELL_TYPE_NUMERIC, cellType, false); + throw typeMismatch(CellType.NUMERIC, cellType, false); } } @@ -373,15 +379,15 @@ public class SXSSFCell implements Cell { * For strings we throw an exception. For blank cells we return a null. * </p> * @return the value of the cell as a date - * @throws IllegalStateException if the cell type returned by {@link #getCellType()} is CELL_TYPE_STRING + * @throws IllegalStateException if the cell type returned by {@link #getCellType()} is CellType.STRING * @exception NumberFormatException if the cell value isn't a parsable <code>double</code>. * @see org.apache.poi.ss.usermodel.DataFormatter for formatting this date into a string similar to how excel does. */ @Override public Date getDateCellValue() { - int cellType = getCellType(); - if (cellType == CELL_TYPE_BLANK) + CellType cellType = getCellType(); + if (cellType == CellType.BLANK) { return null; } @@ -402,9 +408,9 @@ public class SXSSFCell implements Cell { @Override public RichTextString getRichStringCellValue() { - int cellType = getCellType(); - if(getCellType() != CELL_TYPE_STRING) - throw typeMismatch(CELL_TYPE_STRING, cellType, false); + CellType cellType = getCellType(); + if(getCellType() != CellType.STRING) + throw typeMismatch(CellType.STRING, cellType, false); StringValue sval = (StringValue)_value; if(sval.isRichText()) @@ -427,19 +433,19 @@ public class SXSSFCell implements Cell { @Override public String getStringCellValue() { - int cellType = getCellType(); + CellType cellType = getCellType(); switch(cellType) { - case CELL_TYPE_BLANK: + case BLANK: return ""; - case CELL_TYPE_FORMULA: + case FORMULA: { FormulaValue fv=(FormulaValue)_value; - if(fv.getFormulaType()!=CELL_TYPE_STRING) - throw typeMismatch(CELL_TYPE_STRING, CELL_TYPE_FORMULA, false); + if(fv.getFormulaType()!=CellType.STRING) + throw typeMismatch(CellType.STRING, CellType.FORMULA, false); return ((StringFormulaValue)_value).getPreEvaluatedValue(); } - case CELL_TYPE_STRING: + case STRING: { if(((StringValue)_value).isRichText()) return ((RichTextValue)_value).getValue().getString(); @@ -447,7 +453,7 @@ public class SXSSFCell implements Cell { return ((PlainStringValue)_value).getValue(); } default: - throw typeMismatch(CELL_TYPE_STRING, cellType, false); + throw typeMismatch(CellType.STRING, cellType, false); } } @@ -461,8 +467,8 @@ public class SXSSFCell implements Cell { @Override public void setCellValue(boolean value) { - ensureTypeOrFormulaType(CELL_TYPE_BOOLEAN); - if(_value.getType()==CELL_TYPE_FORMULA) + ensureTypeOrFormulaType(CellType.BOOLEAN); + if(_value.getType()==CellType.FORMULA) ((BooleanFormulaValue)_value).setPreEvaluatedValue(value); else ((BooleanValue)_value).setValue(value); @@ -480,8 +486,8 @@ public class SXSSFCell implements Cell { @Override public void setCellErrorValue(byte value) { - ensureType(CELL_TYPE_ERROR); - if(_value.getType()==CELL_TYPE_FORMULA) + ensureType(CellType.ERROR); + if(_value.getType()==CellType.FORMULA) ((ErrorFormulaValue)_value).setPreEvaluatedValue(value); else ((ErrorValue)_value).setValue(value); @@ -494,29 +500,29 @@ public class SXSSFCell implements Cell { * </p> * @return the value of the cell as a boolean * @throws IllegalStateException if the cell type returned by {@link #getCellType()} - * is not CELL_TYPE_BOOLEAN, CELL_TYPE_BLANK or CELL_TYPE_FORMULA + * is not CellType.BOOLEAN, CellType.BLANK or CellType.FORMULA */ @Override public boolean getBooleanCellValue() { - int cellType = getCellType(); + CellType cellType = getCellType(); switch(cellType) { - case CELL_TYPE_BLANK: + case BLANK: return false; - case CELL_TYPE_FORMULA: + case FORMULA: { FormulaValue fv=(FormulaValue)_value; - if(fv.getFormulaType()!=CELL_TYPE_BOOLEAN) - throw typeMismatch(CELL_TYPE_BOOLEAN, CELL_TYPE_FORMULA, false); + if(fv.getFormulaType()!=CellType.BOOLEAN) + throw typeMismatch(CellType.BOOLEAN, CellType.FORMULA, false); return ((BooleanFormulaValue)_value).getPreEvaluatedValue(); } - case CELL_TYPE_BOOLEAN: + case BOOLEAN: { return ((BooleanValue)_value).getValue(); } default: - throw typeMismatch(CELL_TYPE_BOOLEAN, cellType, false); + throw typeMismatch(CellType.BOOLEAN, cellType, false); } } @@ -528,30 +534,30 @@ public class SXSSFCell implements Cell { * </p> * * @return the value of the cell as an error code - * @throws IllegalStateException if the cell type returned by {@link #getCellType()} isn't CELL_TYPE_ERROR + * @throws IllegalStateException if the cell type returned by {@link #getCellType()} isn't CellType.ERROR * @see org.apache.poi.ss.usermodel.FormulaError for error codes */ @Override public byte getErrorCellValue() { - int cellType = getCellType(); + CellType cellType = getCellType(); switch(cellType) { - case CELL_TYPE_BLANK: + case BLANK: return 0; - case CELL_TYPE_FORMULA: + case FORMULA: { FormulaValue fv=(FormulaValue)_value; - if(fv.getFormulaType()!=CELL_TYPE_ERROR) - throw typeMismatch(CELL_TYPE_ERROR, CELL_TYPE_FORMULA, false); + if(fv.getFormulaType()!=CellType.ERROR) + throw typeMismatch(CellType.ERROR, CellType.FORMULA, false); return ((ErrorFormulaValue)_value).getPreEvaluatedValue(); } - case CELL_TYPE_ERROR: + case ERROR: { return ((ErrorValue)_value).getValue(); } default: - throw typeMismatch(CELL_TYPE_ERROR, cellType, false); + throw typeMismatch(CellType.ERROR, cellType, false); } } @@ -709,22 +715,22 @@ public class SXSSFCell implements Cell { @Override public String toString() { switch (getCellType()) { - case CELL_TYPE_BLANK: + case BLANK: return ""; - case CELL_TYPE_BOOLEAN: + case BOOLEAN: return getBooleanCellValue() ? "TRUE" : "FALSE"; - case CELL_TYPE_ERROR: + case ERROR: return ErrorEval.getText(getErrorCellValue()); - case CELL_TYPE_FORMULA: + case FORMULA: return getCellFormula(); - case CELL_TYPE_NUMERIC: + case NUMERIC: if (DateUtil.isCellDateFormatted(this)) { DateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy", LocaleUtil.getUserLocale()); sdf.setTimeZone(LocaleUtil.getUserTimeZone()); return sdf.format(getDateCellValue()); } return getNumericCellValue() + ""; - case CELL_TYPE_STRING: + case STRING: return getRichStringCellValue().toString(); default: return "Unknown Cell Type: " + getCellType(); @@ -806,39 +812,39 @@ public class SXSSFCell implements Cell { } /*package*/ void ensurePlainStringType() { - if(_value.getType()!=CELL_TYPE_STRING + if(_value.getType()!=CellType.STRING ||((StringValue)_value).isRichText()) _value=new PlainStringValue(); } /*package*/ void ensureRichTextStringType() { - if(_value.getType()!=CELL_TYPE_STRING + if(_value.getType()!=CellType.STRING ||!((StringValue)_value).isRichText()) _value=new RichTextValue(); } - /*package*/ void ensureType(int type) + /*package*/ void ensureType(CellType type) { if(_value.getType()!=type) setType(type); } - /*package*/ void ensureFormulaType(int type) + /*package*/ void ensureFormulaType(CellType type) { - if(_value.getType()!=CELL_TYPE_FORMULA + if(_value.getType()!=CellType.FORMULA ||((FormulaValue)_value).getFormulaType()!=type) setFormulaType(type); } /* * Sets the cell type to type if it is different */ - /*package*/ void ensureTypeOrFormulaType(int type) + /*package*/ void ensureTypeOrFormulaType(CellType type) { if(_value.getType()==type) { - if(type==CELL_TYPE_STRING&&((StringValue)_value).isRichText()) - setType(CELL_TYPE_STRING); + if(type==CellType.STRING&&((StringValue)_value).isRichText()) + setType(CellType.STRING); return; } - if(_value.getType()==CELL_TYPE_FORMULA) + if(_value.getType()==CellType.FORMULA) { if(((FormulaValue)_value).getFormulaType()==type) return; @@ -854,16 +860,16 @@ public class SXSSFCell implements Cell { * @param type the cell type to set * @throws IllegalArgumentException if type is not a recognized type */ - /*package*/ void setType(int type) + /*package*/ void setType(CellType type) { switch(type) { - case CELL_TYPE_NUMERIC: + case NUMERIC: { _value=new NumericValue(); break; } - case CELL_TYPE_STRING: + case STRING: { PlainStringValue sval = new PlainStringValue(); if(_value != null){ @@ -874,17 +880,17 @@ public class SXSSFCell implements Cell { _value = sval; break; } - case CELL_TYPE_FORMULA: + case FORMULA: { _value=new NumericFormulaValue(); break; } - case CELL_TYPE_BLANK: + case BLANK: { _value=new BlankValue(); break; } - case CELL_TYPE_BOOLEAN: + case BOOLEAN: { BooleanValue bval = new BooleanValue(); if(_value != null){ @@ -895,7 +901,7 @@ public class SXSSFCell implements Cell { _value = bval; break; } - case CELL_TYPE_ERROR: + case ERROR: { _value=new ErrorValue(); break; @@ -906,26 +912,26 @@ public class SXSSFCell implements Cell { } } } - /*package*/ void setFormulaType(int type) + /*package*/ void setFormulaType(CellType type) { switch(type) { - case CELL_TYPE_NUMERIC: + case NUMERIC: { _value=new NumericFormulaValue(); break; } - case CELL_TYPE_STRING: + case STRING: { _value=new StringFormulaValue(); break; } - case CELL_TYPE_BOOLEAN: + case BOOLEAN: { _value=new BooleanFormulaValue(); break; } - case CELL_TYPE_ERROR: + case ERROR: { _value=new ErrorFormulaValue(); break; @@ -938,77 +944,64 @@ public class SXSSFCell implements Cell { } //TODO: implement this correctly @NotImplemented - /*package*/ int computeTypeFromFormula(String formula) + /*package*/ CellType computeTypeFromFormula(String formula) { - return CELL_TYPE_NUMERIC; + return CellType.NUMERIC; } //COPIED FROM https://svn.apache.org/repos/asf/poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java since the functions are declared private there /** * Used to help format error messages */ - private static RuntimeException typeMismatch(int expectedTypeCode, int actualTypeCode, boolean isFormulaCell) { - String msg = "Cannot get a " - + getCellTypeName(expectedTypeCode) + " value from a " - + getCellTypeName(actualTypeCode) + " " + (isFormulaCell ? "formula " : "") + "cell"; + private static RuntimeException typeMismatch(CellType expectedTypeCode, CellType actualTypeCode, boolean isFormulaCell) { + String msg = "Cannot get a " + expectedTypeCode + " value from a " + actualTypeCode + + " " + (isFormulaCell ? "formula " : "") + "cell"; return new IllegalStateException(msg); } -/** - * Used to help format error messages - */ - private static String getCellTypeName(int cellTypeCode) { - switch (cellTypeCode) { - case CELL_TYPE_BLANK: return "blank"; - case CELL_TYPE_STRING: return "text"; - case CELL_TYPE_BOOLEAN: return "boolean"; - case CELL_TYPE_ERROR: return "error"; - case CELL_TYPE_NUMERIC: return "numeric"; - case CELL_TYPE_FORMULA: return "formula"; - } - return "#unknown cell type (" + cellTypeCode + ")#"; - } + private boolean convertCellValueToBoolean() { - int cellType = getCellType(); + CellType cellType = getCellType(); - if (cellType == CELL_TYPE_FORMULA) { + if (cellType == CellType.FORMULA) { cellType = getCachedFormulaResultType(); } switch (cellType) { - case CELL_TYPE_BOOLEAN: + case BOOLEAN: return getBooleanCellValue(); - case CELL_TYPE_STRING: + case STRING: String text = getStringCellValue(); return Boolean.parseBoolean(text); - case CELL_TYPE_NUMERIC: + case NUMERIC: return getNumericCellValue() != 0; - case CELL_TYPE_ERROR: - case CELL_TYPE_BLANK: + case ERROR: + case BLANK: return false; + default: throw new RuntimeException("Unexpected cell type (" + cellType + ")"); } - throw new RuntimeException("Unexpected cell type (" + cellType + ")"); + } private String convertCellValueToString() { - int cellType = getCellType(); + CellType cellType = getCellType(); return convertCellValueToString(cellType); } - private String convertCellValueToString(int cellType) { + private String convertCellValueToString(CellType cellType) { switch (cellType) { - case CELL_TYPE_BLANK: + case BLANK: return ""; - case CELL_TYPE_BOOLEAN: + case BOOLEAN: return getBooleanCellValue() ? "TRUE" : "FALSE"; - case CELL_TYPE_STRING: + case STRING: return getStringCellValue(); - case CELL_TYPE_NUMERIC: - return Double.toString( getNumericCellValue() ); - case CELL_TYPE_ERROR: + case NUMERIC: + return Double.toString( getNumericCellValue() ); + case ERROR: byte errVal = getErrorCellValue(); return FormulaError.forInt(errVal).getString(); - case CELL_TYPE_FORMULA: + case FORMULA: if (_value != null) { FormulaValue fv = (FormulaValue)_value; - if (fv.getFormulaType() != CELL_TYPE_FORMULA) { + if (fv.getFormulaType() != CellType.FORMULA) { return convertCellValueToString(fv.getFormulaType()); } } @@ -1066,14 +1059,14 @@ public class SXSSFCell implements Cell { } interface Value { - int getType(); + CellType getType(); } static class NumericValue implements Value { double _value; - public int getType() + public CellType getType() { - return CELL_TYPE_NUMERIC; + return CellType.NUMERIC; } void setValue(double value) { @@ -1086,11 +1079,11 @@ public class SXSSFCell implements Cell { } static abstract class StringValue implements Value { - public int getType() + public CellType getType() { - return CELL_TYPE_STRING; + return CellType.STRING; } -//We cannot introduce a new type CELL_TYPE_RICH_TEXT because the types are public so we have to make rich text as a type of string +//We cannot introduce a new type CellType.RICH_TEXT because the types are public so we have to make rich text as a type of string abstract boolean isRichText(); // using the POI style which seems to avoid "instanceof". } static class PlainStringValue extends StringValue @@ -1114,9 +1107,9 @@ public class SXSSFCell implements Cell { { RichTextString _value; @Override - public int getType() + public CellType getType() { - return CELL_TYPE_STRING; + return CellType.STRING; } void setValue(RichTextString value) { @@ -1135,9 +1128,9 @@ public class SXSSFCell implements Cell { static abstract class FormulaValue implements Value { String _value; - public int getType() + public CellType getType() { - return CELL_TYPE_FORMULA; + return CellType.FORMULA; } void setValue(String value) { @@ -1147,15 +1140,15 @@ public class SXSSFCell implements Cell { { return _value; } - abstract int getFormulaType(); + abstract CellType getFormulaType(); } static class NumericFormulaValue extends FormulaValue { double _preEvaluatedValue; @Override - int getFormulaType() + CellType getFormulaType() { - return CELL_TYPE_NUMERIC; + return CellType.NUMERIC; } void setPreEvaluatedValue(double value) { @@ -1170,9 +1163,9 @@ public class SXSSFCell implements Cell { { String _preEvaluatedValue; @Override - int getFormulaType() + CellType getFormulaType() { - return CELL_TYPE_STRING; + return CellType.STRING; } void setPreEvaluatedValue(String value) { @@ -1187,9 +1180,9 @@ public class SXSSFCell implements Cell { { boolean _preEvaluatedValue; @Override - int getFormulaType() + CellType getFormulaType() { - return CELL_TYPE_BOOLEAN; + return CellType.BOOLEAN; } void setPreEvaluatedValue(boolean value) { @@ -1204,9 +1197,9 @@ public class SXSSFCell implements Cell { { byte _preEvaluatedValue; @Override - int getFormulaType() + CellType getFormulaType() { - return CELL_TYPE_ERROR; + return CellType.ERROR; } void setPreEvaluatedValue(byte value) { @@ -1219,17 +1212,17 @@ public class SXSSFCell implements Cell { } static class BlankValue implements Value { - public int getType() + public CellType getType() { - return CELL_TYPE_BLANK; + return CellType.BLANK; } } static class BooleanValue implements Value { boolean _value; - public int getType() + public CellType getType() { - return CELL_TYPE_BOOLEAN; + return CellType.BOOLEAN; } void setValue(boolean value) { @@ -1243,9 +1236,9 @@ public class SXSSFCell implements Cell { static class ErrorValue implements Value { byte _value; - public int getType() + public CellType getType() { - return CELL_TYPE_ERROR; + return CellType.ERROR; } void setValue(byte value) { diff --git a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFEvaluationCell.java b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFEvaluationCell.java index b7d3d830b4..e5e18ceac0 100644 --- a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFEvaluationCell.java +++ b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFEvaluationCell.java @@ -19,6 +19,7 @@ package org.apache.poi.xssf.streaming; import org.apache.poi.ss.formula.EvaluationCell; import org.apache.poi.ss.formula.EvaluationSheet; +import org.apache.poi.ss.usermodel.CellType; /** * SXSSF wrapper for a cell under evaluation @@ -36,6 +37,7 @@ final class SXSSFEvaluationCell implements EvaluationCell { this(cell, new SXSSFEvaluationSheet(cell.getSheet())); } + @Override public Object getIdentityKey() { // save memory by just using the cell itself as the identity key // Note - this assumes SXSSFCell has not overridden hashCode and equals @@ -45,31 +47,40 @@ final class SXSSFEvaluationCell implements EvaluationCell { public SXSSFCell getSXSSFCell() { return _cell; } + @Override public boolean getBooleanCellValue() { return _cell.getBooleanCellValue(); } - public int getCellType() { + @Override + public CellType getCellType() { return _cell.getCellType(); } + @Override public int getColumnIndex() { return _cell.getColumnIndex(); } + @Override public int getErrorCellValue() { return _cell.getErrorCellValue(); } + @Override public double getNumericCellValue() { return _cell.getNumericCellValue(); } + @Override public int getRowIndex() { return _cell.getRowIndex(); } + @Override public EvaluationSheet getSheet() { return _evalSheet; } + @Override public String getStringCellValue() {
return _cell.getRichStringCellValue().getString();
}
- public int getCachedFormulaResultType() {
+ @Override + public CellType getCachedFormulaResultType() {
return _cell.getCachedFormulaResultType();
}
}
diff --git a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFRow.java b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFRow.java index cec3136f61..a1eb965663 100644 --- a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFRow.java +++ b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFRow.java @@ -26,6 +26,7 @@ import java.util.TreeMap; import org.apache.poi.ss.SpreadsheetVersion; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.util.Internal; @@ -119,7 +120,7 @@ public class SXSSFRow implements Row, Comparable<SXSSFRow> /** * Use this to create new cells within the row and return it. * <p> - * The cell that is returned is a {@link Cell#CELL_TYPE_BLANK}. The type can be changed + * The cell that is returned is a {@link CellType#BLANK}. The type can be changed * either through calling <code>setCellValue</code> or <code>setCellType</code>. * * @param column - the column number this cell represents @@ -130,23 +131,40 @@ public class SXSSFRow implements Row, Comparable<SXSSFRow> @Override public SXSSFCell createCell(int column) { - return createCell(column, Cell.CELL_TYPE_BLANK); + return createCell(column, CellType.BLANK); } /** * Use this to create new cells within the row and return it. * <p> - * The cell that is returned is a {@link Cell#CELL_TYPE_BLANK}. The type can be changed + * The cell that is returned is a {@link CellType#BLANK}. The type can be changed * either through calling setCellValue or setCellType. * * @param column - the column number this cell represents * @return Cell a high level representation of the created cell. * @throws IllegalArgumentException if columnIndex < 0 or greate than a maximum number of supported columns * (255 for *.xls, 1048576 for *.xlsx) + * @deprecated POI 3.15 beta 3. Use {@link #createCell(int, CellType)} instead. */ @Override public SXSSFCell createCell(int column, int type) { + return createCell(column, CellType.forInt(type)); + } + /** + * Use this to create new cells within the row and return it. + * <p> + * The cell that is returned is a {@link CellType#BLANK}. The type can be changed + * either through calling setCellValue or setCellType. + * + * @param column - the column number this cell represents + * @return Cell a high level representation of the created cell. + * @throws IllegalArgumentException if columnIndex < 0 or greate than a maximum number of supported columns + * (255 for *.xls, 1048576 for *.xlsx) + */ + @Override + public SXSSFCell createCell(int column, CellType type) + { checkBounds(column); SXSSFCell cell = new SXSSFCell(this, type); _cells.put(column, cell); @@ -250,10 +268,10 @@ public class SXSSFRow implements Row, Comparable<SXSSFRow> case RETURN_NULL_AND_BLANK: return cell; case RETURN_BLANK_AS_NULL: - boolean isBlank = (cell != null && cell.getCellType() == Cell.CELL_TYPE_BLANK); + boolean isBlank = (cell != null && cell.getCellType() == CellType.BLANK); return (isBlank) ? null : cell; case CREATE_NULL_AS_BLANK: - return (cell == null) ? createCell(cellnum, Cell.CELL_TYPE_BLANK) : cell; + return (cell == null) ? createCell(cellnum, CellType.BLANK) : cell; default: throw new IllegalArgumentException("Illegal policy " + policy + " (" + policy.id + ")"); } diff --git a/src/ooxml/java/org/apache/poi/xssf/streaming/SheetDataWriter.java b/src/ooxml/java/org/apache/poi/xssf/streaming/SheetDataWriter.java index 4782ca2fd8..455254aa6b 100644 --- a/src/ooxml/java/org/apache/poi/xssf/streaming/SheetDataWriter.java +++ b/src/ooxml/java/org/apache/poi/xssf/streaming/SheetDataWriter.java @@ -31,6 +31,7 @@ import java.util.Iterator; import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
+import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.FormulaError;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.util.POILogFactory;
@@ -201,19 +202,19 @@ public class SheetDataWriter { // APIs
_out.write(" s=\"" + (cellStyle.getIndex() & 0xffff) + "\"");
}
- int cellType = cell.getCellType();
+ CellType cellType = cell.getCellType();
switch (cellType) {
- case Cell.CELL_TYPE_BLANK: {
+ case BLANK: {
_out.write(">");
break;
}
- case Cell.CELL_TYPE_FORMULA: {
+ case FORMULA: {
_out.write(">");
_out.write("<f>");
outputQuotedString(cell.getCellFormula());
_out.write("</f>");
switch (cell.getCachedFormulaResultType()) {
- case Cell.CELL_TYPE_NUMERIC:
+ case NUMERIC:
double nval = cell.getNumericCellValue();
if (!Double.isNaN(nval)) {
_out.write("<v>" + nval + "</v>");
@@ -224,7 +225,7 @@ public class SheetDataWriter { }
break;
}
- case Cell.CELL_TYPE_STRING: {
+ case STRING: {
if (_sharedStringSource != null) {
XSSFRichTextString rt = new XSSFRichTextString(cell.getStringCellValue());
int sRef = _sharedStringSource.addEntry(rt.getCTRst());
@@ -245,17 +246,17 @@ public class SheetDataWriter { }
break;
}
- case Cell.CELL_TYPE_NUMERIC: {
+ case NUMERIC: {
_out.write(" t=\"n\">");
_out.write("<v>" + cell.getNumericCellValue() + "</v>");
break;
}
- case Cell.CELL_TYPE_BOOLEAN: {
+ case BOOLEAN: {
_out.write(" t=\"b\">");
_out.write("<v>" + (cell.getBooleanCellValue() ? "1" : "0") + "</v>");
break;
}
- case Cell.CELL_TYPE_ERROR: {
+ case ERROR: {
FormulaError error = FormulaError.forInt(cell.getErrorCellValue());
_out.write(" t=\"e\">");
diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/BaseXSSFFormulaEvaluator.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/BaseXSSFFormulaEvaluator.java index 817a9b5a82..81359d3857 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/BaseXSSFFormulaEvaluator.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/BaseXSSFFormulaEvaluator.java @@ -29,6 +29,7 @@ import org.apache.poi.ss.formula.eval.NumberEval; import org.apache.poi.ss.formula.eval.StringEval; import org.apache.poi.ss.formula.eval.ValueEval; import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.CellValue; import org.apache.poi.ss.usermodel.FormulaEvaluator; @@ -75,20 +76,21 @@ public abstract class BaseXSSFFormulaEvaluator implements FormulaEvaluator, Work } switch (cell.getCellType()) { - case XSSFCell.CELL_TYPE_BOOLEAN: + case BOOLEAN: return CellValue.valueOf(cell.getBooleanCellValue()); - case XSSFCell.CELL_TYPE_ERROR: + case ERROR: return CellValue.getError(cell.getErrorCellValue()); - case XSSFCell.CELL_TYPE_FORMULA: + case FORMULA: return evaluateFormulaCellValue(cell); - case XSSFCell.CELL_TYPE_NUMERIC: + case NUMERIC: return new CellValue(cell.getNumericCellValue()); - case XSSFCell.CELL_TYPE_STRING: + case STRING: return new CellValue(cell.getRichStringCellValue().getString()); - case XSSFCell.CELL_TYPE_BLANK: + case BLANK: return null; + default: + throw new IllegalStateException("Bad cell type (" + cell.getCellType() + ")"); } - throw new IllegalStateException("Bad cell type (" + cell.getCellType() + ")"); } @@ -110,9 +112,9 @@ public abstract class BaseXSSFFormulaEvaluator implements FormulaEvaluator, Work * @param cell The cell to evaluate * @return The type of the formula result (the cell's type remains as HSSFCell.CELL_TYPE_FORMULA however) */ - public int evaluateFormulaCell(Cell cell) { - if (cell == null || cell.getCellType() != XSSFCell.CELL_TYPE_FORMULA) { - return -1; + public CellType evaluateFormulaCell(Cell cell) { + if (cell == null || cell.getCellType() != CellType.FORMULA) { + return CellType._UNINITIALIZED; } CellValue cv = evaluateFormulaCellValue(cell); // cell remains a formula cell, but the cached value is changed @@ -129,47 +131,52 @@ public abstract class BaseXSSFFormulaEvaluator implements FormulaEvaluator, Work */ protected void doEvaluateInCell(Cell cell) { if (cell == null) return; - if (cell.getCellType() == XSSFCell.CELL_TYPE_FORMULA) { + if (cell.getCellType() == CellType.FORMULA) { CellValue cv = evaluateFormulaCellValue(cell); setCellType(cell, cv); // cell will no longer be a formula cell setCellValue(cell, cv); } } private static void setCellType(Cell cell, CellValue cv) { - int cellType = cv.getCellType(); + CellType cellType = cv.getCellType(); switch (cellType) { - case XSSFCell.CELL_TYPE_BOOLEAN: - case XSSFCell.CELL_TYPE_ERROR: - case XSSFCell.CELL_TYPE_NUMERIC: - case XSSFCell.CELL_TYPE_STRING: + case BOOLEAN: + case ERROR: + case NUMERIC: + case STRING: cell.setCellType(cellType); return; - case XSSFCell.CELL_TYPE_BLANK: + case BLANK: // never happens - blanks eventually get translated to zero - case XSSFCell.CELL_TYPE_FORMULA: + throw new IllegalArgumentException("This should never happen. Blanks eventually get translated to zero."); + case FORMULA: // this will never happen, we have already evaluated the formula + throw new IllegalArgumentException("This should never happen. Formulas should have already been evaluated."); + default: + throw new IllegalStateException("Unexpected cell value type (" + cellType + ")"); + } - throw new IllegalStateException("Unexpected cell value type (" + cellType + ")"); + } private static void setCellValue(Cell cell, CellValue cv) { - int cellType = cv.getCellType(); + CellType cellType = cv.getCellType(); switch (cellType) { - case XSSFCell.CELL_TYPE_BOOLEAN: + case BOOLEAN: cell.setCellValue(cv.getBooleanValue()); break; - case XSSFCell.CELL_TYPE_ERROR: + case ERROR: cell.setCellErrorValue(cv.getErrorValue()); break; - case XSSFCell.CELL_TYPE_NUMERIC: + case NUMERIC: cell.setCellValue(cv.getNumberValue()); break; - case XSSFCell.CELL_TYPE_STRING: + case STRING: cell.setCellValue(new XSSFRichTextString(cv.getStringValue())); break; - case XSSFCell.CELL_TYPE_BLANK: + case BLANK: // never happens - blanks eventually get translated to zero - case XSSFCell.CELL_TYPE_FORMULA: + case FORMULA: // this will never happen, we have already evaluated the formula default: throw new IllegalStateException("Unexpected cell value type (" + cellType + ")"); diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java index 37adc7ead4..25d8cc8324 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java @@ -32,6 +32,7 @@ import org.apache.poi.ss.formula.ptg.Ptg; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellCopyPolicy; import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.Comment; import org.apache.poi.ss.usermodel.DataFormatter; import org.apache.poi.ss.usermodel.DateUtil; @@ -70,6 +71,8 @@ public final class XSSFCell implements Cell { private static final String FALSE_AS_STRING = "0"; private static final String TRUE_AS_STRING = "1"; + private static final String FALSE = "FALSE"; + private static final String TRUE = "TRUE"; /** * the xml bean containing information about the cell's location, value, @@ -135,23 +138,14 @@ public final class XSSFCell implements Cell { // Copy cell value (cell type is updated implicitly) if (policy.isCopyCellValue()) { if (srcCell != null) { - int copyCellType = srcCell.getCellType(); - if (copyCellType == Cell.CELL_TYPE_FORMULA && !policy.isCopyCellFormula()) { + CellType copyCellType = srcCell.getCellType(); + if (copyCellType == CellType.FORMULA && !policy.isCopyCellFormula()) { // Copy formula result as value // FIXME: Cached value may be stale copyCellType = srcCell.getCachedFormulaResultType(); } switch (copyCellType) { - case Cell.CELL_TYPE_BOOLEAN: - setCellValue(srcCell.getBooleanCellValue()); - break; - case Cell.CELL_TYPE_ERROR: - setCellErrorValue(srcCell.getErrorCellValue()); - break; - case Cell.CELL_TYPE_FORMULA: - setCellFormula(srcCell.getCellFormula()); - break; - case Cell.CELL_TYPE_NUMERIC: + case NUMERIC: // DataFormat is not copied unless policy.isCopyCellStyle is true if (DateUtil.isCellDateFormatted(srcCell)) { setCellValue(srcCell.getDateCellValue()); @@ -160,12 +154,22 @@ public final class XSSFCell implements Cell { setCellValue(srcCell.getNumericCellValue()); } break; - case Cell.CELL_TYPE_STRING: + case STRING: setCellValue(srcCell.getStringCellValue()); break; - case Cell.CELL_TYPE_BLANK: + case FORMULA: + setCellFormula(srcCell.getCellFormula()); + break; + case BLANK: setBlank(); break; + case BOOLEAN: + setCellValue(srcCell.getBooleanCellValue()); + break; + case ERROR: + setCellErrorValue(srcCell.getErrorCellValue()); + break; + default: throw new IllegalArgumentException("Invalid cell type " + srcCell.getCellType()); } @@ -246,21 +250,21 @@ public final class XSSFCell implements Cell { * </p> * @return the value of the cell as a boolean * @throws IllegalStateException if the cell type returned by {@link #getCellType()} - * is not CELL_TYPE_BOOLEAN, CELL_TYPE_BLANK or CELL_TYPE_FORMULA + * is not {@link CellType#BOOLEAN}, {@link CellType#BLANK} or {@link CellType#FORMULA} */ @Override public boolean getBooleanCellValue() { - int cellType = getCellType(); + CellType cellType = getCellType(); switch(cellType) { - case CELL_TYPE_BLANK: + case BLANK: return false; - case CELL_TYPE_BOOLEAN: + case BOOLEAN: return _cell.isSetV() && TRUE_AS_STRING.equals(_cell.getV()); - case CELL_TYPE_FORMULA: + case FORMULA: //YK: should throw an exception if requesting boolean value from a non-boolean formula return _cell.isSetV() && TRUE_AS_STRING.equals(_cell.getV()); default: - throw typeMismatch(CELL_TYPE_BOOLEAN, cellType, false); + throw typeMismatch(CellType.BOOLEAN, cellType, false); } } @@ -284,31 +288,32 @@ public final class XSSFCell implements Cell { * For formulas or error cells we return the precalculated value; * </p> * @return the value of the cell as a number - * @throws IllegalStateException if the cell type returned by {@link #getCellType()} is CELL_TYPE_STRING + * @throws IllegalStateException if the cell type returned by {@link #getCellType()} is {@link CellType#STRING} * @exception NumberFormatException if the cell value isn't a parsable <code>double</code>. * @see DataFormatter for turning this number into a string similar to that which Excel would render this number as. */ @Override public double getNumericCellValue() { - int cellType = getCellType(); + CellType cellType = getCellType(); switch(cellType) { - case CELL_TYPE_BLANK: + case BLANK: return 0.0; - case CELL_TYPE_FORMULA: - case CELL_TYPE_NUMERIC: + case FORMULA: + // fall-through + case NUMERIC: if(_cell.isSetV()) { String v = _cell.getV(); if (v.isEmpty()) return 0.0; try { return Double.parseDouble(v); } catch(NumberFormatException e) { - throw typeMismatch(CELL_TYPE_NUMERIC, CELL_TYPE_STRING, false); + throw typeMismatch(CellType.NUMERIC, CellType.STRING, false); } } else { return 0.0; } default: - throw typeMismatch(CELL_TYPE_NUMERIC, cellType, false); + throw typeMismatch(CellType.NUMERIC, cellType, false); } } @@ -361,13 +366,13 @@ public final class XSSFCell implements Cell { */ @Override public XSSFRichTextString getRichStringCellValue() { - int cellType = getCellType(); + CellType cellType = getCellType(); XSSFRichTextString rt; switch (cellType) { - case CELL_TYPE_BLANK: + case BLANK: rt = new XSSFRichTextString(""); break; - case CELL_TYPE_STRING: + case STRING: if (_cell.getT() == STCellType.INLINE_STR) { if(_cell.isSetIs()) { //string is expressed directly in the cell definition instead of implementing the shared string table. @@ -391,18 +396,18 @@ public final class XSSFCell implements Cell { } } break; - case CELL_TYPE_FORMULA: - checkFormulaCachedValueType(CELL_TYPE_STRING, getBaseCellType(false)); + case FORMULA: + checkFormulaCachedValueType(CellType.STRING, getBaseCellType(false)); rt = new XSSFRichTextString(_cell.isSetV() ? _cell.getV() : ""); break; default: - throw typeMismatch(CELL_TYPE_STRING, cellType, false); + throw typeMismatch(CellType.STRING, cellType, false); } rt.setStylesTableReference(_stylesSource); return rt; } - private static void checkFormulaCachedValueType(int expectedTypeCode, int cachedValueType) { + private static void checkFormulaCachedValueType(CellType expectedTypeCode, CellType cachedValueType) { if (cachedValueType != expectedTypeCode) { throw typeMismatch(expectedTypeCode, cachedValueType, true); } @@ -432,7 +437,7 @@ public final class XSSFCell implements Cell { @Override public void setCellValue(RichTextString str) { if(str == null || str.getString() == null){ - setCellType(Cell.CELL_TYPE_BLANK); + setCellType(CellType.BLANK); return; } @@ -440,9 +445,9 @@ public final class XSSFCell implements Cell { throw new IllegalArgumentException("The maximum length of cell contents (text) is 32,767 characters"); } - int cellType = getCellType(); - switch(cellType){ - case Cell.CELL_TYPE_FORMULA: + CellType cellType = getCellType(); + switch (cellType){ + case FORMULA: _cell.setV(str.getString()); _cell.setT(STCellType.STR); break; @@ -465,7 +470,7 @@ public final class XSSFCell implements Cell { * Return a formula for the cell, for example, <code>SUM(C4:E4)</code> * * @return a formula for the cell - * @throws IllegalStateException if the cell type returned by {@link #getCellType()} is not CELL_TYPE_FORMULA + * @throws IllegalStateException if the cell type returned by {@link #getCellType()} is not {@link CellType#FORMULA} */ @Override public String getCellFormula() { @@ -478,11 +483,11 @@ public final class XSSFCell implements Cell { * * @param fpb evaluation workbook for reuse, if available, or null to create a new one as needed * @return a formula for the cell - * @throws IllegalStateException if the cell type returned by {@link #getCellType()} is not CELL_TYPE_FORMULA + * @throws IllegalStateException if the cell type returned by {@link #getCellType()} is not {@link CellType#FORMULA} */ protected String getCellFormula(XSSFEvaluationWorkbook fpb) { - int cellType = getCellType(); - if(cellType != CELL_TYPE_FORMULA) throw typeMismatch(CELL_TYPE_FORMULA, cellType, false); + CellType cellType = getCellType(); + if(cellType != CellType.FORMULA) throw typeMismatch(CellType.FORMULA, cellType, false); CTCellFormula f = _cell.getF(); if (isPartOfArrayFormulaGroup() && f == null) { @@ -660,28 +665,22 @@ public final class XSSFCell implements Cell { * Return the cell type. * * @return the cell type - * @see Cell#CELL_TYPE_BLANK - * @see Cell#CELL_TYPE_NUMERIC - * @see Cell#CELL_TYPE_STRING - * @see Cell#CELL_TYPE_FORMULA - * @see Cell#CELL_TYPE_BOOLEAN - * @see Cell#CELL_TYPE_ERROR */ @Override - public int getCellType() { - if (isFormulaCell()) return CELL_TYPE_FORMULA; + public CellType getCellType() { + if (isFormulaCell()) return CellType.FORMULA; return getBaseCellType(true); } /** * Only valid for formula cells - * @return one of ({@link #CELL_TYPE_NUMERIC}, {@link #CELL_TYPE_STRING}, - * {@link #CELL_TYPE_BOOLEAN}, {@link #CELL_TYPE_ERROR}) depending + * @return one of ({@link CellType#NUMERIC}, {@link CellType#STRING}, + * {@link CellType#BOOLEAN}, {@link CellType#ERROR}) depending * on the cached value of the formula */ @Override - public int getCachedFormulaResultType() { + public CellType getCachedFormulaResultType() { if (! isFormulaCell()) { throw new IllegalStateException("Only formula cells have cached results"); } @@ -692,10 +691,10 @@ public final class XSSFCell implements Cell { /** * Detect cell type based on the "t" attribute of the CTCell bean */ - private int getBaseCellType(boolean blankCells) { + private CellType getBaseCellType(boolean blankCells) { switch (_cell.getT().intValue()) { case STCellType.INT_B: - return CELL_TYPE_BOOLEAN; + return CellType.BOOLEAN; case STCellType.INT_N: if (!_cell.isSetV() && blankCells) { // ooxml does have a separate cell type of 'blank'. A blank cell gets encoded as @@ -703,15 +702,15 @@ public final class XSSFCell implements Cell { // The formula evaluator (and perhaps other clients of this interface) needs to // distinguish blank values which sometimes get translated into zero and sometimes // empty string, depending on context - return CELL_TYPE_BLANK; + return CellType.BLANK; } - return CELL_TYPE_NUMERIC; + return CellType.NUMERIC; case STCellType.INT_E: - return CELL_TYPE_ERROR; + return CellType.ERROR; case STCellType.INT_S: // String is in shared strings case STCellType.INT_INLINE_STR: // String is inline in cell case STCellType.INT_STR: - return CELL_TYPE_STRING; + return CellType.STRING; default: throw new IllegalStateException("Illegal cell type: " + this._cell.getT()); } @@ -723,13 +722,13 @@ public final class XSSFCell implements Cell { * For strings we throw an exception. For blank cells we return a null. * </p> * @return the value of the cell as a date - * @throws IllegalStateException if the cell type returned by {@link #getCellType()} is CELL_TYPE_STRING + * @throws IllegalStateException if the cell type returned by {@link #getCellType()} is {@link CellType#STRING} * @exception NumberFormatException if the cell value isn't a parsable <code>double</code>. * @see DataFormatter for formatting this date into a string similar to how excel does. */ @Override public Date getDateCellValue() { - if (getCellType() == CELL_TYPE_BLANK) { + if (getCellType() == CellType.BLANK) { return null; } @@ -749,7 +748,7 @@ public final class XSSFCell implements Cell { @Override public void setCellValue(Date value) { if(value == null) { - setCellType(Cell.CELL_TYPE_BLANK); + setCellType(CellType.BLANK); return; } @@ -776,7 +775,7 @@ public final class XSSFCell implements Cell { @Override public void setCellValue(Calendar value) { if(value == null) { - setCellType(Cell.CELL_TYPE_BLANK); + setCellType(CellType.BLANK); return; } @@ -788,12 +787,12 @@ public final class XSSFCell implements Cell { * Returns the error message, such as #VALUE! * * @return the error message such as #VALUE! - * @throws IllegalStateException if the cell type returned by {@link #getCellType()} isn't CELL_TYPE_ERROR + * @throws IllegalStateException if the cell type returned by {@link #getCellType()} isn't {@link CellType#ERROR} * @see FormulaError */ public String getErrorCellString() { - int cellType = getBaseCellType(true); - if(cellType != CELL_TYPE_ERROR) throw typeMismatch(CELL_TYPE_ERROR, cellType, false); + CellType cellType = getBaseCellType(true); + if(cellType != CellType.ERROR) throw typeMismatch(CellType.ERROR, cellType, false); return _cell.getV(); } @@ -805,7 +804,7 @@ public final class XSSFCell implements Cell { * </p> * * @return the value of the cell as an error code - * @throws IllegalStateException if the cell type returned by {@link #getCellType()} isn't CELL_TYPE_ERROR + * @throws IllegalStateException if the cell type returned by {@link #getCellType()} isn't {@link CellType #ERROR} * @see FormulaError */ @Override @@ -881,41 +880,40 @@ public final class XSSFCell implements Cell { * Set the cells type (numeric, formula or string) * * @throws IllegalArgumentException if the specified cell type is invalid - * @see #CELL_TYPE_NUMERIC - * @see #CELL_TYPE_STRING - * @see #CELL_TYPE_FORMULA - * @see #CELL_TYPE_BLANK - * @see #CELL_TYPE_BOOLEAN - * @see #CELL_TYPE_ERROR + * @see CellType#NUMERIC + * @see CellType#STRING + * @see CellType#FORMULA + * @see CellType#BLANK + * @see CellType#BOOLEAN + * @see CellType#ERROR + * @deprecated POI 3.15 beta 3. Use {@link #setCellType(CellType)} instead. */ @Override public void setCellType(int cellType) { - int prevType = getCellType(); + setCellType(CellType.forInt(cellType)); + } + /** + * Set the cells type (numeric, formula or string) + * + * @throws IllegalArgumentException if the specified cell type is invalid + */ + @Override + public void setCellType(CellType cellType) { + CellType prevType = getCellType(); if(isPartOfArrayFormulaGroup()){ notifyArrayFormulaChanging(); } - if(prevType == CELL_TYPE_FORMULA && cellType != CELL_TYPE_FORMULA) { + if(prevType == CellType.FORMULA && cellType != CellType.FORMULA) { getSheet().getWorkbook().onDeleteFormula(this); } switch (cellType) { - case CELL_TYPE_BLANK: - setBlank(); - break; - case CELL_TYPE_BOOLEAN: - String newVal = convertCellValueToBoolean() ? TRUE_AS_STRING : FALSE_AS_STRING; - _cell.setT(STCellType.B); - _cell.setV(newVal); - break; - case CELL_TYPE_NUMERIC: + case NUMERIC: _cell.setT(STCellType.N); break; - case CELL_TYPE_ERROR: - _cell.setT(STCellType.E); - break; - case CELL_TYPE_STRING: - if(prevType != CELL_TYPE_STRING){ + case STRING: + if(prevType != CellType.STRING){ String str = convertCellValueToString(); XSSFRichTextString rt = new XSSFRichTextString(str); rt.setStylesTableReference(_stylesSource); @@ -924,7 +922,7 @@ public final class XSSFCell implements Cell { } _cell.setT(STCellType.S); break; - case CELL_TYPE_FORMULA: + case FORMULA: if(!_cell.isSetF()){ CTCellFormula f = CTCellFormula.Factory.newInstance(); f.setStringValue("0"); @@ -932,10 +930,24 @@ public final class XSSFCell implements Cell { if(_cell.isSetT()) _cell.unsetT(); } break; + case BLANK: + setBlank(); + break; + case BOOLEAN: + String newVal = convertCellValueToBoolean() ? TRUE_AS_STRING : FALSE_AS_STRING; + _cell.setT(STCellType.B); + _cell.setV(newVal); + break; + + case ERROR: + _cell.setT(STCellType.E); + break; + + default: throw new IllegalArgumentException("Illegal cell type: " + cellType); } - if (cellType != CELL_TYPE_FORMULA && _cell.isSetF()) { + if (cellType != CellType.FORMULA && _cell.isSetF()) { _cell.unsetF(); } } @@ -951,23 +963,23 @@ public final class XSSFCell implements Cell { @Override 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: + case NUMERIC: if (DateUtil.isCellDateFormatted(this)) { DateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy", LocaleUtil.getUserLocale()); sdf.setTimeZone(LocaleUtil.getUserTimeZone()); return sdf.format(getDateCellValue()); } return Double.toString(getNumericCellValue()); - case CELL_TYPE_STRING: + case STRING: return getRichStringCellValue().toString(); + case FORMULA: + return getCellFormula(); + case BLANK: + return ""; + case BOOLEAN: + return getBooleanCellValue() ? TRUE : FALSE; + case ERROR: + return ErrorEval.getText(getErrorCellValue()); default: return "Unknown Cell Type: " + getCellType(); } @@ -989,28 +1001,12 @@ public final class XSSFCell implements Cell { return _cell.getV(); } - /** - * Used to help format error messages - */ - private static String getCellTypeName(int cellTypeCode) { - switch (cellTypeCode) { - case CELL_TYPE_BLANK: return "blank"; - case CELL_TYPE_STRING: return "text"; - case CELL_TYPE_BOOLEAN: return "boolean"; - case CELL_TYPE_ERROR: return "error"; - case CELL_TYPE_NUMERIC: return "numeric"; - case CELL_TYPE_FORMULA: return "formula"; - } - return "#unknown cell type (" + cellTypeCode + ")#"; - } /** * Used to help format error messages */ - private static RuntimeException typeMismatch(int expectedTypeCode, int actualTypeCode, boolean isFormulaCell) { - String msg = "Cannot get a " - + getCellTypeName(expectedTypeCode) + " value from a " - + getCellTypeName(actualTypeCode) + " " + (isFormulaCell ? "formula " : "") + "cell"; + private static RuntimeException typeMismatch(CellType expectedType, CellType actualType, boolean isFormulaCell) { + String msg = "Cannot get a " + expectedType + " value from a " + actualType+ " " + (isFormulaCell ? "formula " : "") + "cell"; return new IllegalStateException(msg); } @@ -1137,46 +1133,49 @@ public final class XSSFCell implements Cell { * TODO - perhaps a method like setCellTypeAndValue(int, Object) should be introduced to avoid this */ private boolean convertCellValueToBoolean() { - int cellType = getCellType(); + CellType cellType = getCellType(); - if (cellType == CELL_TYPE_FORMULA) { + if (cellType == CellType.FORMULA) { cellType = getBaseCellType(false); } switch (cellType) { - case CELL_TYPE_BOOLEAN: + case BOOLEAN: return TRUE_AS_STRING.equals(_cell.getV()); - case CELL_TYPE_STRING: + case STRING: int sstIndex = Integer.parseInt(_cell.getV()); XSSFRichTextString rt = new XSSFRichTextString(_sharedStringSource.getEntryAt(sstIndex)); String text = rt.getString(); return Boolean.parseBoolean(text); - case CELL_TYPE_NUMERIC: + case NUMERIC: return Double.parseDouble(_cell.getV()) != 0; - case CELL_TYPE_ERROR: - case CELL_TYPE_BLANK: + case ERROR: + // fall-through + case BLANK: return false; + + default: + throw new RuntimeException("Unexpected cell type (" + cellType + ")"); } - throw new RuntimeException("Unexpected cell type (" + cellType + ")"); } private String convertCellValueToString() { - int cellType = getCellType(); + CellType cellType = getCellType(); switch (cellType) { - case CELL_TYPE_BLANK: + case BLANK: return ""; - case CELL_TYPE_BOOLEAN: - return TRUE_AS_STRING.equals(_cell.getV()) ? "TRUE" : "FALSE"; - case CELL_TYPE_STRING: + case BOOLEAN: + return TRUE_AS_STRING.equals(_cell.getV()) ? TRUE : FALSE; + case STRING: int sstIndex = Integer.parseInt(_cell.getV()); XSSFRichTextString rt = new XSSFRichTextString(_sharedStringSource.getEntryAt(sstIndex)); return rt.getString(); - case CELL_TYPE_NUMERIC: - case CELL_TYPE_ERROR: + case NUMERIC: + case ERROR: return _cell.getV(); - case CELL_TYPE_FORMULA: + case FORMULA: // should really evaluate, but HSSFCell can't call HSSFFormulaEvaluator // just use cached formula result instead break; @@ -1186,21 +1185,27 @@ public final class XSSFCell implements Cell { cellType = getBaseCellType(false); String textValue = _cell.getV(); switch (cellType) { - case CELL_TYPE_BOOLEAN: + case BOOLEAN: if (TRUE_AS_STRING.equals(textValue)) { - return "TRUE"; + return TRUE; } if (FALSE_AS_STRING.equals(textValue)) { - return "FALSE"; + return FALSE; } throw new IllegalStateException("Unexpected boolean cached formula value '" + textValue + "'."); - case CELL_TYPE_STRING: - case CELL_TYPE_NUMERIC: - case CELL_TYPE_ERROR: + + case STRING: + // fall-through + case NUMERIC: + // fall-through + case ERROR: return textValue; + + default: + throw new IllegalStateException("Unexpected formula result type (" + cellType + ")"); } - throw new IllegalStateException("Unexpected formula result type (" + cellType + ")"); + } @Override diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvaluationCell.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvaluationCell.java index 9fd3d9edd6..40b44001ab 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvaluationCell.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvaluationCell.java @@ -19,6 +19,7 @@ package org.apache.poi.xssf.usermodel; import org.apache.poi.ss.formula.EvaluationCell; import org.apache.poi.ss.formula.EvaluationSheet; +import org.apache.poi.ss.usermodel.CellType; /** * XSSF wrapper for a cell under evaluation @@ -37,6 +38,7 @@ final class XSSFEvaluationCell implements EvaluationCell { this(cell, new XSSFEvaluationSheet(cell.getSheet())); } + @Override public Object getIdentityKey() { // save memory by just using the cell itself as the identity key // Note - this assumes XSSFCell has not overridden hashCode and equals @@ -46,31 +48,40 @@ final class XSSFEvaluationCell implements EvaluationCell { public XSSFCell getXSSFCell() { return _cell; } + @Override public boolean getBooleanCellValue() { return _cell.getBooleanCellValue(); } - public int getCellType() { + @Override + public CellType getCellType() { return _cell.getCellType(); } + @Override public int getColumnIndex() { return _cell.getColumnIndex(); } + @Override public int getErrorCellValue() { return _cell.getErrorCellValue(); } + @Override public double getNumericCellValue() { return _cell.getNumericCellValue(); } + @Override public int getRowIndex() { return _cell.getRowIndex(); } + @Override public EvaluationSheet getSheet() { return _evalSheet; } + @Override public String getStringCellValue() {
return _cell.getRichStringCellValue().getString();
}
- public int getCachedFormulaResultType() {
+ @Override + public CellType getCachedFormulaResultType() {
return _cell.getCachedFormulaResultType();
}
}
diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java index c4e63c70f9..8196abfb45 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java @@ -27,6 +27,7 @@ import org.apache.poi.ss.SpreadsheetVersion; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellCopyPolicy; import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellReference; @@ -184,7 +185,7 @@ public class XSSFRow implements Row, Comparable<XSSFRow> { /** * Use this to create new cells within the row and return it. * <p> - * The cell that is returned is a {@link Cell#CELL_TYPE_BLANK}. The type can be changed + * The cell that is returned is a {@link CellType#BLANK}. The type can be changed * either through calling <code>setCellValue</code> or <code>setCellType</code>. * </p> * @param columnIndex - the column number this cell represents @@ -194,7 +195,7 @@ public class XSSFRow implements Row, Comparable<XSSFRow> { */ @Override public XSSFCell createCell(int columnIndex) { - return createCell(columnIndex, Cell.CELL_TYPE_BLANK); + return createCell(columnIndex, CellType.BLANK); } /** @@ -205,15 +206,29 @@ public class XSSFRow implements Row, Comparable<XSSFRow> { * @return XSSFCell a high level representation of the created cell. * @throws IllegalArgumentException if the specified cell type is invalid, columnIndex < 0 * or greater than 16384, the maximum number of columns supported by the SpreadsheetML format (.xlsx) - * @see Cell#CELL_TYPE_BLANK - * @see Cell#CELL_TYPE_BOOLEAN - * @see Cell#CELL_TYPE_ERROR - * @see Cell#CELL_TYPE_FORMULA - * @see Cell#CELL_TYPE_NUMERIC - * @see Cell#CELL_TYPE_STRING + * @see CellType#BLANK + * @see CellType#BOOLEAN + * @see CellType#ERROR + * @see CellType#FORMULA + * @see CellType#NUMERIC + * @see CellType#STRING + * @deprecated POI 3.15 beta 3. Use {@link #createCell(int, CellType)} instead. */ @Override public XSSFCell createCell(int columnIndex, int type) { + return createCell(columnIndex, CellType.forInt(type)); + } + /** + * Use this to create new cells within the row and return it. + * + * @param columnIndex - the column number this cell represents + * @param type - the cell's data type + * @return XSSFCell a high level representation of the created cell. + * @throws IllegalArgumentException if the specified cell type is invalid, columnIndex < 0 + * or greater than 16384, the maximum number of columns supported by the SpreadsheetML format (.xlsx) + */ + @Override + public XSSFCell createCell(int columnIndex, CellType type) { // Performance optimization for bug 57840: explicit boxing is slightly faster than auto-unboxing, though may use more memory final Integer colI = new Integer(columnIndex); // NOSONAR CTCell ctCell; @@ -226,8 +241,8 @@ public class XSSFRow implements Row, Comparable<XSSFRow> { } XSSFCell xcell = new XSSFCell(this, ctCell); xcell.setCellNum(columnIndex); - if (type != Cell.CELL_TYPE_BLANK) { - xcell.setCellType(type); + if (type != CellType.BLANK) { + xcell.setCellType(type); } _cells.put(colI, xcell); return xcell; @@ -261,10 +276,10 @@ public class XSSFRow implements Row, Comparable<XSSFRow> { case RETURN_NULL_AND_BLANK: return cell; case RETURN_BLANK_AS_NULL: - boolean isBlank = (cell != null && cell.getCellType() == Cell.CELL_TYPE_BLANK); + boolean isBlank = (cell != null && cell.getCellType() == CellType.BLANK); return (isBlank) ? null : cell; case CREATE_NULL_AS_BLANK: - return (cell == null) ? createCell(cellnum, Cell.CELL_TYPE_BLANK) : cell; + return (cell == null) ? createCell(cellnum, CellType.BLANK) : cell; default: throw new IllegalArgumentException("Illegal policy " + policy + " (" + policy.id + ")"); } @@ -481,7 +496,7 @@ public class XSSFRow implements Row, Comparable<XSSFRow> { if(xcell.isPartOfArrayFormulaGroup()) { xcell.notifyArrayFormulaChanging(); } - if(cell.getCellType() == Cell.CELL_TYPE_FORMULA) { + if(cell.getCellType() == CellType.FORMULA) { _sheet.getWorkbook().onDeleteFormula(xcell); } // Performance optimization for bug 57840: explicit boxing is slightly faster than auto-unboxing, though may use more memory |