示例#1
0
  /**
   * Get the value of the cell as a date.
   *
   * <p>For strings we throw an exception. For blank cells we return a null.
   *
   * @return the value of the cell as a date
   * @throws IllegalStateException if the cell type returned by {@link #getCellType()} is
   *     CELL_TYPE_STRING
   * @exception NumberFormatException if the cell value isn't a parsable <code>double</code>.
   * @see DataFormatter for formatting this date into a string similar to how excel does.
   */
  public Date getDateCellValue() {
    int cellType = getCellType();
    if (cellType == CELL_TYPE_BLANK) {
      return null;
    }

    double value = getNumericCellValue();
    boolean date1904 = getSheet().getWorkbook().isDate1904();
    return DateUtil.getJavaDate(value, date1904);
  }
示例#2
0
  /**
   * Get the value of the cell as a date.
   *
   * <p>For strings we throw an exception. For blank cells we return a null.
   *
   * @return the value of the cell as a date
   * @throws IllegalStateException if the cell type returned by {@link #getCellType()} is
   *     CELL_TYPE_STRING
   * @exception NumberFormatException if the cell value isn't a parsable <code>double</code>.
   * @see org.zkoss.poi.ss.usermodel.DataFormatter for formatting this date into a string similar to
   *     how excel does.
   */
  public Date getDateCellValue() {
    int cellType = getCellType();
    if (cellType == CELL_TYPE_BLANK) {
      return null;
    }

    double value = getNumericCellValue();
    // TODO: activate this when compiling against 3.7.
    // boolean date1904 = getSheet().getXSSFWorkbook().isDate1904();
    boolean date1904 = false;
    return DateUtil.getJavaDate(value, date1904);
  }
示例#3
0
 /**
  * Returns a string representation of the cell
  *
  * <p>Formula cells return the formula string, rather than the formula result. Dates are displayed
  * in dd-MMM-yyyy format Errors are displayed as #ERR&lt;errIdx&gt;
  */
 public String toString() {
   switch (getCellType()) {
     case CELL_TYPE_BLANK:
       return "";
     case CELL_TYPE_BOOLEAN:
       return getBooleanCellValue() ? "TRUE" : "FALSE";
     case CELL_TYPE_ERROR:
       return ErrorEval.getText(getErrorCellValue());
     case CELL_TYPE_FORMULA:
       return getCellFormula();
     case CELL_TYPE_NUMERIC:
       if (DateUtil.isCellDateFormatted(this)) {
         DateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
         return sdf.format(getDateCellValue());
       }
       return getNumericCellValue() + "";
     case CELL_TYPE_STRING:
       return getRichStringCellValue().toString();
     default:
       return "Unknown Cell Type: " + getCellType();
   }
 }
示例#4
0
 /**
  * Set a date value for the cell. Excel treats dates as numeric so you will need to format the
  * cell as a date.
  *
  * <p>This will set the cell value based on the Calendar's timezone. As Excel does not support
  * timezones this means that both 20:00+03:00 and 20:00-03:00 will be reported as the same value
  * (20:00) even that there are 6 hours difference between the two times. This difference can be
  * preserved by using <code>setCellValue(value.getTime())</code> which will automatically shift
  * the times to the default timezone.
  *
  * @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 othertypes we will change the cell to a
  *     numeric cell and set its value.
  */
 public void setCellValue(Calendar value) {
   // TODO: activate this when compiling against 3.7.
   // boolean date1904 = getSheet().getXSSFWorkbook().isDate1904();
   boolean date1904 = false;
   setCellValue(DateUtil.getExcelDate(value, date1904));
 }
示例#5
0
 /**
  * Set a date value for the cell. Excel treats dates as numeric so you will need to format the
  * cell as a date.
  *
  * <p>This will set the cell value based on the Calendar's timezone. As Excel does not support
  * timezones this means that both 20:00+03:00 and 20:00-03:00 will be reported as the same value
  * (20:00) even that there are 6 hours difference between the two times. This difference can be
  * preserved by using <code>setCellValue(value.getTime())</code> which will automatically shift
  * the times to the default timezone.
  *
  * @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 othertypes we will change the cell to a
  *     numeric cell and set its value.
  */
 public void setCellValue(Calendar value) {
   boolean date1904 = getSheet().getWorkbook().isDate1904();
   setCellValue(DateUtil.getExcelDate(value, date1904));
 }