/**
* get the value of the cell as a string - for numeric cells we throw an exception.
* For blank cells we return an empty string.
- * For formulaCells that are not string Formulas, we return empty String
+ * For formulaCells that are not string Formulas, we throw an exception
*/
public String getStringCellValue()
{
/**
* get the value of the cell as a string - for numeric cells we throw an exception.
* For blank cells we return an empty string.
- * For formulaCells that are not string Formulas, we return empty String
+ * For formulaCells that are not string Formulas, we throw an exception
*/
public HSSFRichTextString getRichStringCellValue() {
* Get the value of the cell as a XSSFRichTextString
* <p>
* For numeric cells we throw an exception. For blank cells we return an empty string.
- * For formula cells we return the pre-calculated value.
+ * For formula cells we return the pre-calculated value if a string, otherwise an exception.
* </p>
* @return the value of the cell as a XSSFRichTextString
*/
* Get the value of the cell as a string
* <p>
* For numeric cells we throw an exception. For blank cells we return an empty string.
- * For formulaCells that are not string Formulas, we return empty String.
+ * For formulaCells that are not string Formulas, we throw an exception.
* </p>
* @return the value of the cell as a string
*/
return 0.0;
case CELL_TYPE_FORMULA:
case CELL_TYPE_NUMERIC:
- return _cell.isSetV() ? Double.parseDouble(_cell.getV()) : 0.0;
+ if(_cell.isSetV()) {
+ try {
+ return Double.parseDouble(_cell.getV());
+ } catch(NumberFormatException e) {
+ throw typeMismatch(CELL_TYPE_NUMERIC, CELL_TYPE_STRING, false);
+ }
+ } else {
+ return 0.0;
+ }
default:
throw typeMismatch(CELL_TYPE_NUMERIC, cellType, false);
}
* Get the value of the cell as a string
* <p>
* For numeric cells we throw an exception. For blank cells we return an empty string.
- * For formulaCells that are not string Formulas, we return empty String.
+ * For formulaCells that are not string Formulas, we throw an exception
* </p>
* @return the value of the cell as a string
*/
* Get the value of the cell as a XSSFRichTextString
* <p>
* For numeric cells we throw an exception. For blank cells we return an empty string.
- * For formula cells we return the pre-calculated value.
+ * For formula cells we return the pre-calculated value if a string, otherwise an exception
* </p>
* @return the value of the cell as a XSSFRichTextString
*/
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Font;
+import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Name;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
assertEquals("123", df.formatRawCellContents(123.0, -1, "@"));
assertEquals("123", df.formatRawCellContents(123.0, -1, "General"));
}
+
+ /**
+ * Ensures that XSSF and HSSF agree with each other,
+ * and with the docs on when fetching the wrong
+ * kind of value from a Formula cell
+ */
+ public void test47815() {
+ Workbook[] wbs = new Workbook[] {
+ new HSSFWorkbook(),
+ new XSSFWorkbook()
+ };
+ for(Workbook wb : wbs) {
+ Sheet s = wb.createSheet();
+ Row r = s.createRow(0);
+
+ // Setup
+ Cell cn = r.createCell(0, Cell.CELL_TYPE_NUMERIC);
+ cn.setCellValue(1.2);
+ Cell cs = r.createCell(1, Cell.CELL_TYPE_STRING);
+ cs.setCellValue("Testing");
+
+ Cell cfn = r.createCell(2, Cell.CELL_TYPE_FORMULA);
+ cfn.setCellFormula("A1");
+ Cell cfs = r.createCell(3, Cell.CELL_TYPE_FORMULA);
+ cfs.setCellFormula("B1");
+
+ FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
+ assertEquals(Cell.CELL_TYPE_NUMERIC, fe.evaluate(cfn).getCellType());
+ assertEquals(Cell.CELL_TYPE_STRING, fe.evaluate(cfs).getCellType());
+ fe.evaluateFormulaCell(cfn);
+ fe.evaluateFormulaCell(cfs);
+
+ // Now test
+ assertEquals(Cell.CELL_TYPE_NUMERIC, cn.getCellType());
+ assertEquals(Cell.CELL_TYPE_STRING, cs.getCellType());
+ assertEquals(Cell.CELL_TYPE_FORMULA, cfn.getCellType());
+ assertEquals(Cell.CELL_TYPE_NUMERIC, cfn.getCachedFormulaResultType());
+ assertEquals(Cell.CELL_TYPE_FORMULA, cfs.getCellType());
+ assertEquals(Cell.CELL_TYPE_STRING, cfs.getCachedFormulaResultType());
+
+ // Different ways of retrieving
+ assertEquals(1.2, cn.getNumericCellValue());
+ try {
+ cn.getRichStringCellValue();
+ fail();
+ } catch(IllegalStateException e) {}
+
+ assertEquals("Testing", cs.getStringCellValue());
+ try {
+ cs.getNumericCellValue();
+ fail();
+ } catch(IllegalStateException e) {}
+
+ assertEquals(1.2, cfn.getNumericCellValue());
+ try {
+ cfn.getRichStringCellValue();
+ fail();
+ } catch(IllegalStateException e) {}
+
+ assertEquals("Testing", cfs.getStringCellValue());
+ try {
+ cfs.getNumericCellValue();
+ fail();
+ } catch(IllegalStateException e) {}
+ }
+ }
}