org.apache.poi.ss.usermodel.Cell.getDateCellValue()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(7.4k)|赞(0)|评价(0)|浏览(332)

本文整理了Java中org.apache.poi.ss.usermodel.Cell.getDateCellValue()方法的一些代码示例,展示了Cell.getDateCellValue()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Cell.getDateCellValue()方法的具体详情如下:
包路径:org.apache.poi.ss.usermodel.Cell
类名称:Cell
方法名:getDateCellValue

Cell.getDateCellValue介绍

[英]Get the value of the cell as a date.

For strings we throw an exception. For blank cells we return a null.
[中]获取单元格的值作为日期。
对于字符串,我们抛出一个异常。对于空白单元格,我们返回空值。

代码示例

代码示例来源:origin: pentaho/pentaho-kettle

public Object getValue() {
 try {
  switch ( getType() ) {
   case BOOLEAN_FORMULA:
   case BOOLEAN:
    return Boolean.valueOf( cell.getBooleanCellValue() );
   case DATE_FORMULA:
   case DATE:
    // Timezone conversion needed since POI doesn't support this apparently
    //
    long time = cell.getDateCellValue().getTime();
    long tzOffset = TimeZone.getDefault().getOffset( time );
    return new Date( time + tzOffset );
   case NUMBER_FORMULA:
   case NUMBER:
    return Double.valueOf( cell.getNumericCellValue() );
   case STRING_FORMULA:
   case LABEL:
    return cell.getStringCellValue();
   case EMPTY:
   default:
    return null;
  }
 } catch ( Exception e ) {
  throw new RuntimeException( "Unable to get value of cell ("
   + cell.getColumnIndex() + ", " + cell.getRowIndex() + ")", e );
 }
}

代码示例来源:origin: looly/hutool

/**
 * 获取数字类型的单元格值
 * 
 * @param cell 单元格
 * @return 单元格值,可能为Long、Double、Date
 */
private static Object getNumericValue(Cell cell) {
  final double value = cell.getNumericCellValue();
  final CellStyle style = cell.getCellStyle();
  if (null == style) {
    return value;
  }
  final short formatIndex = style.getDataFormat();
  // 判断是否为日期
  if (isDateType(cell, formatIndex)) {
    return DateUtil.date(cell.getDateCellValue());// 使用Hutool的DateTime包装
  }
  final String format = style.getDataFormatString();
  // 普通数字
  if (null != format && format.indexOf(StrUtil.C_DOT) < 0) {
    final long longPart = (long) value;
    if (longPart == value) {
      // 对于无小数部分的数字类型,转为Long
      return longPart;
    }
  }
  return value;
}

代码示例来源:origin: looly/hutool

/**
 * 获取数字类型的单元格值
 * 
 * @param cell 单元格
 * @return 单元格值,可能为Long、Double、Date
 */
private static Object getNumericValue(Cell cell) {
  final double value = cell.getNumericCellValue();
  final CellStyle style = cell.getCellStyle();
  if (null == style) {
    return value;
  }
  final short formatIndex = style.getDataFormat();
  // 判断是否为日期
  if (isDateType(cell, formatIndex)) {
    return DateUtil.date(cell.getDateCellValue());// 使用Hutool的DateTime包装
  }
  final String format = style.getDataFormatString();
  // 普通数字
  if (null != format && format.indexOf(StrUtil.C_DOT) < 0) {
    final long longPart = (long) value;
    if (longPart == value) {
      // 对于无小数部分的数字类型,转为Long
      return longPart;
    }
  }
  return value;
}

代码示例来源:origin: org.apache.poi/poi

/**
 * Returns the formatted value of an Excel date as a <tt>String</tt> based
 * on the cell's <code>DataFormat</code>. i.e. "Thursday, January 02, 2003"
 * , "01/02/2003" , "02-Jan" , etc.
 * <p>
 * If any conditional format rules apply, the highest priority with a number format is used.
 * If no rules contain a number format, or no rules apply, the cell's style format is used.
 * If the style does not have a format, the default date format is applied.
 *
 * @param cell to format
 * @param cfEvaluator ConditionalFormattingEvaluator (if available)
 * @return Formatted value
 */
private String getFormattedDateString(Cell cell, ConditionalFormattingEvaluator cfEvaluator) {
  if (cell == null) {
    return null;
  }
  Format dateFormat = getFormat(cell, cfEvaluator);
  if(dateFormat instanceof ExcelStyleDateFormatter) {
    // Hint about the raw excel value
    ((ExcelStyleDateFormatter)dateFormat).setDateToBeFormatted(
       cell.getNumericCellValue()
    );
  }
  Date d = cell.getDateCellValue();
  return performDateFormatting(d, dateFormat);
}

代码示例来源:origin: org.apache.poi/poi

if (getApplicableFormatPart(value).getCellFormatType() == CellFormatType.DATE) {
  if (DateUtil.isValidExcelDate(value)) {
    return apply(label, c.getDateCellValue(), value);
  } else {
    return apply(label, INVALID_VALUE_FOR_FORMAT);

代码示例来源:origin: org.apache.poi/poi

if (getApplicableFormatPart(value).getCellFormatType() == CellFormatType.DATE) {
  if (DateUtil.isValidExcelDate(value)) {
    return apply(c.getDateCellValue(), value);
  } else {
    return apply(INVALID_VALUE_FOR_FORMAT);

代码示例来源:origin: org.apache.poi/poi-ooxml

setCellValue(srcCell.getDateCellValue());

代码示例来源:origin: neo4j-contrib/neo4j-apoc-procedures

private static Object getValue(Cell cell, CellType type) {
  switch (type) {
    case NUMERIC: // In excel the date is NUMERIC Type
      if (DateUtil.isCellDateFormatted(cell)) {
        return LocalDateTimeValue.localDateTime(cell.getDateCellValue().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
      }
      double value = cell.getNumericCellValue();
      if (value == Math.floor(value)) return (long) value;
      return value;
    case STRING: return cell.getStringCellValue();
    case FORMULA: return getValue(cell,cell.getCachedFormulaResultTypeEnum());
    case BOOLEAN: return cell.getBooleanCellValue();
    case _NONE: return null;
    case BLANK: return null;
    case ERROR: return null;
    default: return null;
  }
}

代码示例来源:origin: arnaudroger/SimpleFlatMapper

@Override
public Date get(Row target) throws Exception {
  final Cell cell = target.getCell(index);
  if (cell != null) {
    return cell.getDateCellValue();
  } else {
    return null;
  }
}

代码示例来源:origin: AlreadyGo/springboot-simple

private static String getDateResult(Cell cell) {
  Date date = cell.getDateCellValue();
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
  return sdf.format(date);
}

代码示例来源:origin: org.spdx/spdx-tools

public Date getReviewerTimestamp(int rowNum) {
  Row row = sheet.getRow(rowNum);
  if (row == null) {
    return null;
  }
  Cell tsCell = row.getCell(TIMESTAMP_COL);
  if (tsCell == null) {
    return null;
  }
  return tsCell.getDateCellValue();
}

代码示例来源:origin: com.github.mygreen/excel-cellformatter

@Override
public Date getDateCellValue() {
  final Date date = cell.getDateCellValue();
  // タイムゾーン分、引かれているので調整する。
  return new Date(date.getTime() + TimeZone.getDefault().getRawOffset());
}

代码示例来源:origin: org.apache.poi/poi-examples

/**
 * Checks if cell content matches for date.
 */
private void isCellContentMatchesForDate(Locator loc1, Locator loc2) {
  Date date1 = loc1.cell.getDateCellValue();
  Date date2 = loc2.cell.getDateCellValue();
  if (!date1.equals(date2)) {
    addMessage(loc1, loc2, CELL_DATA_DOES_NOT_MATCH, date1.toString(), date2.toString());
  }
}

代码示例来源:origin: dayatang/dddlib

/**
 * throws an exception for non-numeric cells
 */
private static boolean isDate1904(Cell cell) {
  double value = cell.getNumericCellValue();
  Date date = cell.getDateCellValue();
  Calendar cal = new GregorianCalendar();
  cal.setTime(date);
  long year1900 = cal.get(Calendar.YEAR) - 1900;
  long yearEst1900 = Math.round(value / (365.25));
  return year1900 > yearEst1900;
}

代码示例来源:origin: org.spdx/spdx-tools

protected Date getDataCellDateValue(int colNum) {
  Cell cell = getDataRow().getCell(colNum);
  if (cell == null) {
    return null;
  } else {
    return cell.getDateCellValue();
  }
}

代码示例来源:origin: stackoverflow.com

Cell c = r.getCell(19);
if(c.getCellType()==Cell.CELL_TYPE_NUMERIC){
  if (HSSFDateUtil.isCellDateFormatted(row.getCell(0))) {
    Date date = c.getDateCellValue();
  }
}

代码示例来源:origin: spdx/tools

protected Date getDataCellDateValue(int colNum) {
  Cell cell = getDataRow().getCell(colNum);
  if (cell == null) {
    return null;
  } else {
    return cell.getDateCellValue();
  }
}

代码示例来源:origin: io.github.aktoluna/slnarch-common

public static Object getNumericCellValue(final Cell cell) {
 Object cellValue;
 if (DateUtil.isCellDateFormatted(cell)) {
  cellValue = cell.getDateCellValue();
 } else {
  cellValue = cell.getNumericCellValue();
 }
 return cellValue;
}

代码示例来源:origin: net.guerlab/azeroth

private static String getNumberTypeStringValue(
    Cell cell,
    DecimalFormat decimalFormat) {
  if (DateUtil.isCellDateFormatted(cell)) {
    return TimeHelper.formatDate(cell.getDateCellValue());
  } else if (decimalFormat != null) {
    return decimalFormat.format(cell.getNumericCellValue());
  } else {
    return DECIMAL_FORMAT.format(cell.getNumericCellValue());
  }
}

代码示例来源:origin: tobyweston/simple-excel

@Override
  public Cell adapt(org.apache.poi.ss.usermodel.Cell cell) {
    if (isCellDateFormatted(cell))
      return new DateCell(cell.getDateCellValue());
    return new DoubleCell(cell.getNumericCellValue());
  }
},

相关文章