Author: fanningpj
Date: Thu Feb 13 20:24:17 2025
New Revision: 1923790
URL: http://svn.apache.org/viewvc?rev=1923790&view=rev
Log:
refactor cell toString to use DataFormatter
Modified:
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFCell.java
poi/trunk/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFCell.java
poi/trunk/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestCell.java
Modified:
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFCell.java
URL:
http://svn.apache.org/viewvc/poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFCell.java?rev=1923790&r1=1923789&r2=1923790&view=diff
==============================================================================
---
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFCell.java
(original)
+++
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFCell.java
Thu Feb 13 20:24:17 2025
@@ -17,8 +17,6 @@
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 @@ 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;
@@ -779,6 +776,7 @@ public final class XSSFCell extends Cell
return _cell.getV();
}
+
/**
* Get the value of the cell as an error code.
* <p>
@@ -926,6 +924,8 @@ public final class XSSFCell extends Cell
}
}
+ private static final DataFormatter DATA_FORMATTER = new DataFormatter();
+
/**
* Returns a string representation of the cell
* <p>
@@ -938,14 +938,8 @@ public final class XSSFCell extends Cell
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:
Modified:
poi/trunk/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFCell.java
URL:
http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFCell.java?rev=1923790&r1=1923789&r2=1923790&view=diff
==============================================================================
--- poi/trunk/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFCell.java
(original)
+++ poi/trunk/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFCell.java Thu
Feb 13 20:24:17 2025
@@ -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.CellS
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.RichT
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();
}
Modified:
poi/trunk/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestCell.java
URL:
http://svn.apache.org/viewvc/poi/trunk/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestCell.java?rev=1923790&r1=1923789&r2=1923790&view=diff
==============================================================================
--- poi/trunk/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestCell.java
(original)
+++ poi/trunk/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestCell.java
Thu Feb 13 20:24:17 2025
@@ -47,6 +47,8 @@ import org.apache.poi.ss.SpreadsheetVers
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)) {
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]