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
This commit is contained in:
Dominik Stadler 2016-03-28 20:20:29 +00:00
parent 4f4531c67e
commit 8afb394974
3 changed files with 41 additions and 7 deletions

View File

@ -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.</p>
*
* <p>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)}</p>
* use {@link org.apache.poi.ss.util.CellUtil#setCellStyleProperties(Cell, Map<String,Object>)}</p>
*
* @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.

View File

@ -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.</p>
*
* <p>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)}</p>
* use {@link org.apache.poi.ss.util.CellUtil#setCellStyleProperties(Cell, java.util.Map<String, Object>)}</p>
*
* @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 ));
}

View File

@ -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);
}
}