diff options
author | Glen Stampoultzis <glens@apache.org> | 2002-02-12 02:19:12 +0000 |
---|---|---|
committer | Glen Stampoultzis <glens@apache.org> | 2002-02-12 02:19:12 +0000 |
commit | 60cba30078201a67c2c69e6baaf93afcbe5fc9ee (patch) | |
tree | 67b17eecceac4ca500d2a2cbafd90d977338efd9 /src | |
parent | fc64c555f9291f1c5e7a6ad108d3fe78a86f6f46 (diff) | |
download | poi-60cba30078201a67c2c69e6baaf93afcbe5fc9ee.tar.gz poi-60cba30078201a67c2c69e6baaf93afcbe5fc9ee.zip |
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
Diffstat (limited to 'src')
-rw-r--r-- | src/java/org/apache/poi/hssf/usermodel/HSSFCell.java | 47 | ||||
-rw-r--r-- | src/testcases/org/apache/poi/hssf/usermodel/TestCellStyle.java | 26 |
2 files changed, 70 insertions, 3 deletions
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); } @@ -621,6 +621,19 @@ public class HSSFCell } /** + * 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. * * @param value value to set the cell to. For formulas we'll set the formula @@ -687,6 +700,34 @@ public class HSSFCell } /** + * 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 @@ -136,6 +136,32 @@ public class TestCellStyle } /** + * 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 <P> * OBJECTIVE: Test that HSSF can create a simple spreadsheet with numeric and string values and styled with colors * and borders.<P> |