aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/java/org/apache/poi/hssf/usermodel/HSSFCell.java47
-rw-r--r--src/testcases/org/apache/poi/hssf/usermodel/TestCellStyle.java26
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>