From ca8014ebef6b5f2b872179d00f70971582882c92 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Thu, 13 Feb 2025 20:24:17 +0000 Subject: [PATCH] refactor cell toString to use DataFormatter git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1923790 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/xssf/usermodel/XSSFCell.java | 14 ++++--------- .../apache/poi/hssf/usermodel/HSSFCell.java | 16 +++++---------- .../apache/poi/ss/usermodel/BaseTestCell.java | 20 ++++++++++++++++--- 3 files changed, 26 insertions(+), 24 deletions(-) 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 20b7a2cbf4b..5ef93237fa7 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 @@ -17,8 +17,6 @@ Licensed to the Apache Software Foundation (ASF) under one or more package org.apache.poi.xssf.usermodel; -import java.text.DateFormat; -import java.text.SimpleDateFormat; import java.time.LocalDateTime; import java.util.Calendar; import java.util.Date; @@ -51,7 +49,6 @@ Licensed to the Apache Software Foundation (ASF) under one or more 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; @@ -779,6 +776,7 @@ public String getErrorCellString() throws IllegalStateException { return _cell.getV(); } + /** * Get the value of the cell as an error code. *

@@ -926,6 +924,8 @@ protected void setCellType(CellType cellType, BaseXSSFEvaluationWorkbook evalWb) } } + private static final DataFormatter DATA_FORMATTER = new DataFormatter(); + /** * Returns a string representation of the cell *

@@ -938,14 +938,8 @@ protected void setCellType(CellType cellType, BaseXSSFEvaluationWorkbook evalWb) public String toString() { switch (getCellType()) { 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 STRING: - return getRichStringCellValue().toString(); + return DATA_FORMATTER.formatCellValue(this); 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 b2efc5c9643..1569553682f 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 @@ Licensed to the Apache Software Foundation (ASF) under one or more 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 @@ Licensed to the Apache Software Foundation (ASF) under one or more 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 @@ Licensed to the Apache Software Foundation (ASF) under one or more 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 void setAsActiveCell() _sheet.getSheet().setActiveCellCol(col); } + private static final DataFormatter DATA_FORMATTER = new DataFormatter(); + /** * Returns a string representation of the cell * @@ -1038,21 +1039,14 @@ public String toString() { 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 e69b5abbb2d..0130e9b4f6a 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 @@ Licensed to the Apache Software Foundation (ASF) under one or more 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 @@ Licensed to the Apache Software Foundation (ASF) under one or more @SuppressWarnings("deprecation") public abstract class BaseTestCell { + protected static TimeZone userTimeZone; protected final ITestDataProvider _testDataProvider; /** @@ -65,6 +68,19 @@ protected BaseTestCell(ITestDataProvider testDataProvider) { _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 @@ void testToString() throws Exception { 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)) {