From 71dcabc947654c37e101185994e451e012bc8365 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Sun, 23 Mar 2025 10:59:43 +0000 Subject: [PATCH] partially revert cell toString changes git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1924533 13f79535-47bb-0310-9956-ffa450edef68 --- .../java/org/apache/poi/xssf/streaming/SXSSFCell.java | 10 +++++++--- .../java/org/apache/poi/xssf/usermodel/XSSFCell.java | 11 ++++++++--- .../java/org/apache/poi/hssf/usermodel/HSSFCell.java | 11 ++++++++--- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFCell.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFCell.java index 20689d5696..50f87b0d5b 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFCell.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFCell.java @@ -721,8 +721,6 @@ public class SXSSFCell extends CellBase { } //end of interface implementation - private static final DataFormatter DATA_FORMATTER = new DataFormatter(); - /** * Returns a string representation of the cell *

@@ -743,8 +741,14 @@ public class SXSSFCell extends CellBase { case FORMULA: return getCellFormula(); case NUMERIC: + if (DateUtil.isCellDateFormatted(this)) { + DataFormatter df = new DataFormatter(); + df.setUseCachedValuesForFormulaCells(true); + return df.formatCellValue(this); + } + return Double.toString(getNumericCellValue()); case STRING: - return DATA_FORMATTER.formatCellValue(this); + return getRichStringCellValue().toString(); default: return "Unknown Cell Type: " + getCellType(); } diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFCell.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFCell.java index 5ef93237fa..38de9ccfb1 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFCell.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFCell.java @@ -49,6 +49,7 @@ import org.apache.poi.ss.util.CellUtil; import org.apache.poi.util.Beta; import org.apache.poi.util.ExceptionUtil; import org.apache.poi.util.Internal; +import org.apache.poi.util.LocaleUtil; import org.apache.poi.xssf.model.CalculationChain; import org.apache.poi.xssf.model.SharedStringsTable; import org.apache.poi.xssf.model.StylesTable; @@ -924,8 +925,6 @@ public final class XSSFCell extends CellBase { } } - private static final DataFormatter DATA_FORMATTER = new DataFormatter(); - /** * Returns a string representation of the cell *

@@ -938,8 +937,14 @@ public final class XSSFCell extends CellBase { public String toString() { switch (getCellType()) { case NUMERIC: + if (DateUtil.isCellDateFormatted(this)) { + DataFormatter df = new DataFormatter(); + df.setUseCachedValuesForFormulaCells(true); + return df.formatCellValue(this); + } + return Double.toString(getNumericCellValue()); case STRING: - return DATA_FORMATTER.formatCellValue(this); + return getRichStringCellValue().toString(); case FORMULA: return getCellFormula(); case BLANK: diff --git a/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFCell.java b/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFCell.java index 1569553682..f6d15b9837 100644 --- a/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFCell.java +++ b/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFCell.java @@ -55,6 +55,7 @@ import org.apache.poi.ss.usermodel.RichTextString; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellReference; import org.apache.poi.ss.util.NumberToTextConverter; +import org.apache.poi.util.LocaleUtil; /** * High level representation of a cell in a row of a spreadsheet. @@ -1020,8 +1021,6 @@ public class HSSFCell extends CellBase { _sheet.getSheet().setActiveCellCol(col); } - private static final DataFormatter DATA_FORMATTER = new DataFormatter(); - /** * Returns a string representation of the cell * @@ -1045,8 +1044,14 @@ public class HSSFCell extends CellBase { case FORMULA: return getCellFormula(); case NUMERIC: + if (DateUtil.isCellDateFormatted(this)) { + DataFormatter df = new DataFormatter(); + df.setUseCachedValuesForFormulaCells(true); + return df.formatCellValue(this); + } + return Double.toString(getNumericCellValue()); case STRING: - return DATA_FORMATTER.formatCellValue(this); + return getRichStringCellValue().toString(); default: return "Unknown Cell Type: " + getCellType(); }