From 60cba30078201a67c2c69e6baaf93afcbe5fc9ee Mon Sep 17 00:00:00 2001 From: Glen Stampoultzis Date: Tue, 12 Feb 2002 02:19:12 +0000 Subject: Added methods for date reading and writing on HSSFCell. git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@352092 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/hssf/usermodel/HSSFCell.java | 47 ++++++++++++++++++++-- .../apache/poi/hssf/usermodel/TestCellStyle.java | 26 ++++++++++++ 2 files changed, 70 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java index a19072531d..0cef964592 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java @@ -71,6 +71,8 @@ import org.apache.poi.hssf.record.BlankRecord; import org.apache.poi.hssf.record.BoolErrRecord; import org.apache.poi.hssf.record.ExtendedFormatRecord; +import java.util.Date; + /** * High level representation of a cell in a row of a spreadsheet. * Cells can be numeric, formula-based or string-based (text). The cell type @@ -608,11 +610,9 @@ public class HSSFCell * 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. */ - public void setCellValue(double value) { - if ((cellType != CELL_TYPE_NUMERIC) - && (cellType != CELL_TYPE_FORMULA)) + if ((cellType != CELL_TYPE_NUMERIC) && (cellType != CELL_TYPE_FORMULA)) { setCellType(CELL_TYPE_NUMERIC, false); } @@ -620,6 +620,19 @@ public class HSSFCell cellValue = value; } + /** + * 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. + */ + public void setCellValue(Date value) + { + setCellValue(HSSFDateUtil.getExcelDate(value)); + } + /** * set a string value for the cell. * @@ -686,6 +699,34 @@ public class HSSFCell return cellValue; } + /** + * get the value of the cell as a date. For strings we throw an exception. + * For blank cells we return a null. + */ + public Date getDateCellValue() + { + if (cellType == CELL_TYPE_BLANK) + { + return null; + } + if (cellType == CELL_TYPE_STRING) + { + throw new NumberFormatException( + "You cannot get a date value from a String based cell"); + } + if (cellType == CELL_TYPE_BOOLEAN) + { + throw new NumberFormatException( + "You cannot get a date value from a boolean cell"); + } + if (cellType == CELL_TYPE_ERROR) + { + throw new NumberFormatException( + "You cannot get a date value from an error cell"); + } + return HSSFDateUtil.getJavaDate(cellValue); + } + /** * get the value of the cell as a string - for numeric cells we throw an exception. * For blank cells we return an empty string. diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestCellStyle.java b/src/testcases/org/apache/poi/hssf/usermodel/TestCellStyle.java index 20d06b071f..49f49c6e5d 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestCellStyle.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestCellStyle.java @@ -135,6 +135,32 @@ public class TestCellStyle // assert((s.getLastRowNum() == 99)); } + /** + * Tests that is creating a file with a date works correctly. + */ + public void testDataStyle() + throws Exception + { + File file = File.createTempFile("testWriteSheetStyleDate", + ".xls"); + FileOutputStream out = new FileOutputStream(file); + HSSFWorkbook wb = new HSSFWorkbook(); + HSSFSheet s = wb.createSheet(); + HSSFCellStyle cs = wb.createCellStyle(); + HSSFRow row = s.createRow((short)0); + HSSFCell cell = row.createCell((short)1); + cs.setDataFormat(HSSFDataFormat.getFormat("m/d/yy")); + cell.setCellStyle(cs); + cell.setCellValue(new Date()); + wb.write(out); + out.close(); + + assertEquals("FILE LENGTH ", 5632, file.length()); + assertEquals("LAST ROW ", 0, s.getLastRowNum()); + assertEquals("FIRST ROW ", 0, s.getFirstRowNum()); + + } + /** * TEST NAME: Test Write Sheet Style

* OBJECTIVE: Test that HSSF can create a simple spreadsheet with numeric and string values and styled with colors -- cgit v1.2.3