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
This commit is contained in:
Glen Stampoultzis 2002-02-12 02:19:12 +00:00
parent b6d098cbcc
commit 59837a47a8
2 changed files with 70 additions and 3 deletions

View File

@ -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.

View File

@ -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 <P>
* OBJECTIVE: Test that HSSF can create a simple spreadsheet with numeric and string values and styled with colors