aboutsummaryrefslogtreecommitdiffstats
path: root/poi/src
diff options
context:
space:
mode:
authorPJ Fanning <fanningpj@apache.org>2025-02-13 20:24:17 +0000
committerPJ Fanning <fanningpj@apache.org>2025-02-13 20:24:17 +0000
commitca8014ebef6b5f2b872179d00f70971582882c92 (patch)
treefc92b12a0188689bd4efe87041fefd6ee683ffe0 /poi/src
parent694765368e84e85372dc8c94e1fe484d7b1da759 (diff)
downloadpoi-ca8014ebef6b5f2b872179d00f70971582882c92.tar.gz
poi-ca8014ebef6b5f2b872179d00f70971582882c92.zip
refactor cell toString to use DataFormatter
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1923790 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'poi/src')
-rw-r--r--poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFCell.java16
-rw-r--r--poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestCell.java20
2 files changed, 22 insertions, 14 deletions
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 b2efc5c964..1569553682 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
@@ -17,7 +17,6 @@
package org.apache.poi.hssf.usermodel;
-import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;
@@ -48,6 +47,7 @@ import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.Comment;
+import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.FormulaError;
import org.apache.poi.ss.usermodel.Hyperlink;
@@ -55,7 +55,6 @@ 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.
@@ -1021,6 +1020,8 @@ public class HSSFCell extends CellBase {
_sheet.getSheet().setActiveCellCol(col);
}
+ private static final DataFormatter DATA_FORMATTER = new DataFormatter();
+
/**
* Returns a string representation of the cell
*
@@ -1038,21 +1039,14 @@ public class HSSFCell extends CellBase {
case BLANK:
return "";
case BOOLEAN:
- return getBooleanCellValue()?"TRUE":"FALSE";
+ return getBooleanCellValue() ? "TRUE" : "FALSE";
case ERROR:
return ErrorEval.getText((( BoolErrRecord ) _record).getErrorValue());
case FORMULA:
return getCellFormula();
case NUMERIC:
- //TODO apply the dataformat for this cell
- if (DateUtil.isCellDateFormatted(this)) {
- SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy", LocaleUtil.getUserLocale());
- sdf.setTimeZone(LocaleUtil.getUserTimeZone());
- return sdf.format(getDateCellValue());
- }
- return String.valueOf(getNumericCellValue());
case STRING:
- return getStringCellValue();
+ return DATA_FORMATTER.formatCellValue(this);
default:
return "Unknown Cell Type: " + getCellType();
}
diff --git a/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestCell.java b/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestCell.java
index e69b5abbb2..0130e9b4f6 100644
--- a/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestCell.java
+++ b/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestCell.java
@@ -47,6 +47,8 @@ import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.formula.eval.NotImplementedException;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.util.LocaleUtil;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
/**
@@ -56,6 +58,7 @@ import org.junit.jupiter.api.Test;
@SuppressWarnings("deprecation")
public abstract class BaseTestCell {
+ protected static TimeZone userTimeZone;
protected final ITestDataProvider _testDataProvider;
/**
@@ -65,6 +68,19 @@ public abstract class BaseTestCell {
_testDataProvider = testDataProvider;
}
+ @BeforeAll
+ public static void setTimeZone() {
+ userTimeZone = LocaleUtil.getUserTimeZone();
+ LocaleUtil.setUserTimeZone(TimeZone.getTimeZone("CET"));
+ LocaleUtil.setUserLocale(Locale.US);
+ }
+
+ @AfterAll
+ public static void resetTimeZone() {
+ LocaleUtil.setUserTimeZone(userTimeZone);
+ LocaleUtil.setUserLocale(Locale.ROOT);
+ }
+
@Test
void testSetValues() throws Exception {
try (Workbook book = _testDataProvider.createWorkbook()) {
@@ -357,9 +373,7 @@ public abstract class BaseTestCell {
assertEquals("", r.getCell(6).toString(), "Blank");
// toString on a date-formatted cell displays dates as dd-MMM-yyyy, which has locale problems with the month
String dateCell1 = r.getCell(7).toString();
- assertTrue(dateCell1.startsWith("02-"), "Date (Day)");
- assertTrue(dateCell1.endsWith("-2010"), "Date (Year)");
-
+ assertEquals("2/2/10 0:00", dateCell1);
//Write out the file, read it in, and then check cell values
try (Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1)) {