Skip to content

Commit

Permalink
refactor cell toString to use DataFormatter
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1923790 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
pjfanning committed Feb 13, 2025
1 parent 6947653 commit ca8014e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -779,6 +776,7 @@ public String getErrorCellString() throws IllegalStateException {

return _cell.getV();
}

/**
* Get the value of the cell as an error code.
* <p>
Expand Down Expand Up @@ -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
* <p>
Expand All @@ -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:
Expand Down
16 changes: 5 additions & 11 deletions poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFCell.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -48,14 +47,14 @@ 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;
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.
Expand Down Expand Up @@ -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
*
Expand All @@ -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();
}
Expand Down
20 changes: 17 additions & 3 deletions poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestCell.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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;

/**
Expand All @@ -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()) {
Expand Down Expand Up @@ -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)) {
Expand Down

0 comments on commit ca8014e

Please sign in to comment.