From 8afb394974940ed6b9adb6887178018fad191b34 Mon Sep 17 00:00:00 2001
From: Dominik Stadler
Date: Mon, 28 Mar 2016 20:20:29 +0000
Subject: [PATCH] Bug 59199: Handle null date-values in a similar way as
null-Strings
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1736923 13f79535-47bb-0310-9956-ffa450edef68
---
.../apache/poi/xssf/streaming/SXSSFCell.java | 13 +++++++++++-
.../apache/poi/xssf/usermodel/XSSFCell.java | 21 +++++++++++++------
.../poi/ss/usermodel/BaseTestXCell.java | 14 +++++++++++++
3 files changed, 41 insertions(+), 7 deletions(-)
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 839f6921f..b91efa908 100644
--- a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCell.java
+++ b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCell.java
@@ -21,6 +21,7 @@ import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
+import java.util.Map;
import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.formula.FormulaParseException;
@@ -206,6 +207,11 @@ public class SXSSFCell implements Cell {
*/
@Override
public void setCellValue(Date value) {
+ if(value == null) {
+ setCellType(Cell.CELL_TYPE_BLANK);
+ return;
+ }
+
boolean date1904 = getSheet().getWorkbook().isDate1904();
setCellValue(DateUtil.getExcelDate(value, date1904));
}
@@ -228,6 +234,11 @@ public class SXSSFCell implements Cell {
*/
@Override
public void setCellValue(Calendar value) {
+ if(value == null) {
+ setCellType(Cell.CELL_TYPE_BLANK);
+ return;
+ }
+
boolean date1904 = getSheet().getWorkbook().isDate1904();
setCellValue( DateUtil.getExcelDate(value, date1904 ));
}
@@ -549,7 +560,7 @@ public class SXSSFCell implements Cell {
* the Workbook.
*
* To change the style of a cell without affecting other cells that use the same style,
- * use {@link org.apache.poi.ss.util.CellUtil#setCellStyleProperties(Cell, Map)}
+ * use {@link org.apache.poi.ss.util.CellUtil#setCellStyleProperties(Cell, Map)}
*
* @param style reference contained in the workbook.
* If the value is null then the style information is removed causing the cell to used the default workbook style.
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 ca56b59fd..b694ee8ca 100644
--- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java
+++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java
@@ -120,13 +120,13 @@ public final class XSSFCell implements Cell {
}
/**
- * Copy cell value, formula, and style, from srcCell per cell copy policy
+ * Copy cell value, formula and style, from srcCell per cell copy policy
* If srcCell is null, clears the cell value and cell style per cell copy policy
*
* This does not shift references in formulas. Use {@link org.apache.poi.xssf.usermodel.helpers.XSSFRowShifter} to shift references in formulas.
*
- * @param srcCell
- * @param policy
+ * @param srcCell The cell to take value, formula and style from
+ * @param policy The policy for copying the information, see {@link CellCopyPolicy}
* @throws IllegalArgumentException if copy cell style and srcCell is from a different workbook
*/
@Beta
@@ -619,7 +619,7 @@ public final class XSSFCell implements Cell {
* the XSSFWorkbook.
*
* To change the style of a cell without affecting other cells that use the same style,
- * use {@link org.apache.poi.ss.util.CellUtil#setCellStyleProperties(Cell, Map)}
+ * use {@link org.apache.poi.ss.util.CellUtil#setCellStyleProperties(Cell, java.util.Map)}
*
* @param style reference contained in the workbook.
* If the value is null then the style information is removed causing the cell to used the default workbook style.
@@ -718,8 +718,7 @@ public final class XSSFCell implements Cell {
*/
@Override
public Date getDateCellValue() {
- int cellType = getCellType();
- if (cellType == CELL_TYPE_BLANK) {
+ if (getCellType() == CELL_TYPE_BLANK) {
return null;
}
@@ -738,6 +737,11 @@ public final class XSSFCell implements Cell {
*/
@Override
public void setCellValue(Date value) {
+ if(value == null) {
+ setCellType(Cell.CELL_TYPE_BLANK);
+ return;
+ }
+
boolean date1904 = getSheet().getWorkbook().isDate1904();
setCellValue(DateUtil.getExcelDate(value, date1904));
}
@@ -760,6 +764,11 @@ public final class XSSFCell implements Cell {
*/
@Override
public void setCellValue(Calendar value) {
+ if(value == null) {
+ setCellType(Cell.CELL_TYPE_BLANK);
+ return;
+ }
+
boolean date1904 = getSheet().getWorkbook().isDate1904();
setCellValue( DateUtil.getExcelDate(value, date1904 ));
}
diff --git a/src/ooxml/testcases/org/apache/poi/ss/usermodel/BaseTestXCell.java b/src/ooxml/testcases/org/apache/poi/ss/usermodel/BaseTestXCell.java
index 76f4e2bf1..44cd2a58b 100644
--- a/src/ooxml/testcases/org/apache/poi/ss/usermodel/BaseTestXCell.java
+++ b/src/ooxml/testcases/org/apache/poi/ss/usermodel/BaseTestXCell.java
@@ -20,6 +20,8 @@ package org.apache.poi.ss.usermodel;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
+import java.util.Calendar;
+import java.util.Date;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.ss.ITestDataProvider;
@@ -55,4 +57,16 @@ public abstract class BaseTestXCell extends BaseTestCell {
assertEquals("???<>\t\n\u00a0 &\"POI\'\u2122", wb2.getSheetAt(0).getRow(0).getCell(0).getStringCellValue());
wb2.close();
}
+
+ @Test
+ public void testSetNullValues() {
+ Workbook wb = _testDataProvider.createWorkbook();
+ Cell cell = wb.createSheet("test").createRow(0).createCell(0);
+
+ cell.setCellValue((Calendar)null);
+ cell.setCellValue((Date)null);
+ cell.setCellValue((String)null);
+ cell.setCellValue((RichTextString) null);
+ cell.setCellValue((String)null);
+ }
}