From 53eee01b5dcd28ab894f85403a0e640c054f089f Mon Sep 17 00:00:00 2001 From: Vladislav Galas Date: Sat, 26 Jan 2019 23:19:59 +0000 Subject: [PATCH] pulled *Cell.setCellValue(Date) and setCellValue(Calendar) to the common base git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1852254 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/hssf/usermodel/HSSFCell.java | 40 ++++------------- .../org/apache/poi/ss/usermodel/CellBase.java | 42 ++++++++++++++++++ .../apache/poi/xssf/streaming/SXSSFCell.java | 44 +++---------------- .../apache/poi/xssf/usermodel/XSSFCell.java | 36 ++------------- 4 files changed, 59 insertions(+), 103 deletions(-) diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java index 7abfe59d20..9292004bb5 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java @@ -442,45 +442,21 @@ public class HSSFCell extends CellBase { } /** - * set a date value for the cell. Excel treats dates as numeric so you will need to format the cell as - * a date. + * {@inheritDoc} * - * @param value the date value to set this cell to. For formulas we'll set the - * precalculated value, for numerics we'll set its value. For other types we - * will change the cell to a numeric cell and set its value. + *

In HSSF, only the number of days is stored. The fractional part is ignored.

+ * @see HSSFDateUtil + * @see org.apache.poi.ss.usermodel.DateUtil */ - public void setCellValue(Date value) - { - if(value == null) { - setBlank(); - return; - } - + protected void setCellValueImpl(Date value) { setCellValue(HSSFDateUtil.getExcelDate(value, _book.getWorkbook().isUsing1904DateWindowing())); } /** - * set a date value for the cell. Excel treats dates as numeric so you will need to format the cell as - * a date. - * - * This will set the cell value based on the Calendar's timezone. As Excel - * does not support timezones this means that both 20:00+03:00 and - * 20:00-03:00 will be reported as the same value (20:00) even that there - * are 6 hours difference between the two times. This difference can be - * preserved by using setCellValue(value.getTime()) which will - * automatically shift the times to the default timezone. - * - * @param value the date value to set this cell to. For formulas we'll set the - * precalculated value, for numerics we'll set its value. For othertypes we - * will change the cell to a numeric cell and set its value. + * {@inheritDoc} */ - public void setCellValue(Calendar value) - { - if(value == null) { - setBlank(); - return; - } - + @Override + protected void setCellValueImpl(Calendar value) { setCellValue( HSSFDateUtil.getExcelDate(value, _book.getWorkbook().isUsing1904DateWindowing()) ); } diff --git a/src/java/org/apache/poi/ss/usermodel/CellBase.java b/src/java/org/apache/poi/ss/usermodel/CellBase.java index 107f906668..7506ae3419 100644 --- a/src/java/org/apache/poi/ss/usermodel/CellBase.java +++ b/src/java/org/apache/poi/ss/usermodel/CellBase.java @@ -23,6 +23,9 @@ import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellReference; import org.apache.poi.util.Removal; +import java.util.Calendar; +import java.util.Date; + /** * Common implementation-independent logic shared by all implementations of {@link Cell}. * @author Vladislav "gallon" Galas gallon at apache dot org @@ -213,4 +216,43 @@ public abstract class CellBase implements Cell { * @param value the new value to set */ protected abstract void setCellValueImpl(double value); + + @Override + public void setCellValue(Date value) { + if(value == null) { + setBlank(); + return; + } + setCellValueImpl(value); + } + + /** + * Implementation-specific way to set a date value. + * value is guaranteed to be non-null. + * The implementation is expected to adjust the cell type accordingly, so that after this call + * getCellType() or getCachedFormulaResultType() would return {@link CellType#NUMERIC}. + * @param value the new date to set + */ + protected abstract void setCellValueImpl(Date value); + + /** + * {@inheritDoc} + */ + @Override + public final void setCellValue(Calendar value) { + if(value == null) { + setBlank(); + return; + } + setCellValueImpl(value); + } + + /** + * Implementation-specific way to set a calendar value. + * value is guaranteed to be non-null. + * The implementation is expected to adjust the cell type accordingly, so that after this call + * getCellType() or getCachedFormulaResultType() would return {@link CellType#NUMERIC}. + * @param value the new calendar value to set + */ + protected abstract void setCellValueImpl(Calendar value); } diff --git a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCell.java b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCell.java index 2b5f4bbf5d..be95a63114 100644 --- a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCell.java +++ b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCell.java @@ -171,54 +171,20 @@ public class SXSSFCell extends CellBase { } /** - * Converts the supplied date to its equivalent Excel numeric value and sets - * that into the cell. - *

- * Note - There is actually no 'DATE' cell type in Excel. In many - * cases (when entering date values), Excel automatically adjusts the - * cell style to some date format, creating the illusion that the cell - * data type is now something besides {@link CellType#NUMERIC}. POI - * does not attempt to replicate this behaviour. To make a numeric cell - * display as a date, use {@link #setCellStyle(CellStyle)} etc. - * - * @param value the numeric value to set this cell to. For formulas we'll set the - * precalculated value, for numerics we'll set its value. For other types we - * will change the cell to a numerics cell and set its value. + * {@inheritDoc} */ @Override - public void setCellValue(Date value) { - if(value == null) { - setBlank(); - return; - } - + protected void setCellValueImpl(Date value) { boolean date1904 = getSheet().getWorkbook().isDate1904(); setCellValue(DateUtil.getExcelDate(value, date1904)); } + /** - * Set a date value for the cell. Excel treats dates as numeric so you will need to format the cell as - * a date. - *

- * This will set the cell value based on the Calendar's timezone. As Excel - * does not support timezones this means that both 20:00+03:00 and - * 20:00-03:00 will be reported as the same value (20:00) even that there - * are 6 hours difference between the two times. This difference can be - * preserved by using setCellValue(value.getTime()) which will - * automatically shift the times to the default timezone. - *

- * - * @param value the date value to set this cell to. For formulas we'll set the - * precalculated value, for numerics we'll set its value. For othertypes we - * will change the cell to a numeric cell and set its value. + * {@inheritDoc} */ @Override - public void setCellValue(Calendar value) { - if(value == null) { - setBlank(); - return; - } - + protected void setCellValueImpl(Calendar value) { boolean date1904 = getSheet().getWorkbook().isDate1904(); setCellValue( DateUtil.getExcelDate(value, date1904 )); } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java index b16ce37d03..ae9eaf7af5 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java @@ -776,47 +776,19 @@ public final class XSSFCell extends CellBase { } /** - * Set a date value for the cell. Excel treats dates as numeric so you will need to format the cell as - * a date. - * - * @param value the date value to set this cell to. For formulas we'll set the - * precalculated value, for numerics we'll set its value. For other types we - * will change the cell to a numeric cell and set its value. + * {@inheritDoc} */ @Override - public void setCellValue(Date value) { - if(value == null) { - setBlank(); - return; - } - + protected void setCellValueImpl(Date value) { boolean date1904 = getSheet().getWorkbook().isDate1904(); setCellValue(DateUtil.getExcelDate(value, date1904)); } /** - * Set a date value for the cell. Excel treats dates as numeric so you will need to format the cell as - * a date. - *

- * This will set the cell value based on the Calendar's timezone. As Excel - * does not support timezones this means that both 20:00+03:00 and - * 20:00-03:00 will be reported as the same value (20:00) even that there - * are 6 hours difference between the two times. This difference can be - * preserved by using setCellValue(value.getTime()) which will - * automatically shift the times to the default timezone. - *

- * - * @param value the date value to set this cell to. For formulas we'll set the - * precalculated value, for numerics we'll set its value. For othertypes we - * will change the cell to a numeric cell and set its value. + * {@inheritDoc} */ @Override - public void setCellValue(Calendar value) { - if(value == null) { - setBlank(); - return; - } - + protected void setCellValueImpl(Calendar value) { boolean date1904 = getSheet().getWorkbook().isDate1904(); setCellValue( DateUtil.getExcelDate(value, date1904 )); } -- 2.39.5