本文整理了Java中org.apache.poi.ss.usermodel.Cell.getNumericCellValue()
方法的一些代码示例,展示了Cell.getNumericCellValue()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Cell.getNumericCellValue()
方法的具体详情如下:
包路径:org.apache.poi.ss.usermodel.Cell
类名称:Cell
方法名:getNumericCellValue
[英]Get the value of the cell as a number.
For strings we throw an exception. For blank cells we return a 0. For formulas or error cells we return the precalculated value;
[中]以数字形式获取单元格的值。
对于字符串,我们抛出一个异常。对于空白单元格,我们返回0。对于公式或误差单元格,我们返回预先计算的值;
代码示例来源:origin: kiegroup/optaplanner
protected double readDoubleCell(Cell cell) {
return cell.getNumericCellValue();
}
代码示例来源:origin: org.apache.poi/poi
public boolean isValidValue(Cell cell, DataValidationContext context) {
if (super.isValidValue(cell, context)) {
// we know it is a number in the proper range, now check if it is an int
final double value = cell.getNumericCellValue(); // can't get here without a valid numeric value
return Double.compare(value, (int) value) == 0;
}
return false;
}
},
代码示例来源:origin: org.apache.poi/poi
/**
* Returns a default format for a cell.
* @param cell The cell
* @return a default format
*/
public Format getDefaultFormat(Cell cell) {
return getDefaultFormat(cell.getNumericCellValue());
}
private Format getDefaultFormat(double cellValue) {
代码示例来源:origin: org.apache.poi/poi
/**
* Uses the cell value, which may be the cached formula result value.
* We won't re-evaluate cells here. This validation would be after the cell value was updated externally.
* Excel allows invalid values through methods like copy/paste, and only validates them when the user
* interactively edits the cell.
* @return if the cell is a valid numeric cell for the validation or not
*/
protected boolean isValidNumericCell(Cell cell, DataValidationContext context) {
if ( ! isType(cell, CellType.NUMERIC)) return false;
Double value = Double.valueOf(cell.getNumericCellValue());
return isValidNumericValue(value, context);
}
代码示例来源:origin: org.apache.poi/poi
/**
* Returns the formatted value of an Excel number as a <tt>String</tt>
* based on the cell's <code>DataFormat</code>. Supported formats include
* currency, percents, decimals, phone number, SSN, etc.:
* "61.54%", "$100.00", "(800) 555-1234".
* <p>
* Format comes from either the highest priority conditional format rule with a
* specified format, or from the cell style.
*
* @param cell The cell
* @param cfEvaluator if available, or null
* @return a formatted number string
*/
private String getFormattedNumberString(Cell cell, ConditionalFormattingEvaluator cfEvaluator) {
if (cell == null) {
return null;
}
Format numberFormat = getFormat(cell, cfEvaluator);
double d = cell.getNumericCellValue();
if (numberFormat == null) {
return String.valueOf(d);
}
String formatted = numberFormat.format(Double.valueOf(d));
return formatted.replaceFirst("E(\\d)", "E+$1"); // to match Excel's E-notation
}
代码示例来源:origin: stackoverflow.com
for(Cell cell : row) {
if(cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
System.out.println("Formula is " + cell.getCellFormula());
switch(cell.getCachedFormulaResultType()) {
case Cell.CELL_TYPE_NUMERIC:
System.out.println("Last evaluated as: " + cell.getNumericCellValue());
break;
case Cell.CELL_TYPE_STRING:
System.out.println("Last evaluated as \"" + cell.getRichStringCellValue() + "\"");
break;
}
}
}
代码示例来源:origin: kiegroup/optaplanner
protected long readLongCell(Cell cell) {
double d = cell.getNumericCellValue();
long l = (long) d;
if (d - (double) l != 0.0) {
throw new IllegalArgumentException("The keyCell (" + cell.getRow().getRowNum() + ","
+ cell.getColumnIndex() + ") with value (" + d + ") is expected to be a long.");
}
return l;
}
代码示例来源:origin: kiegroup/optaplanner
protected double readDoubleParameter(Row row, String key) {
Cell keyCell = row.getCell(0);
if (!key.equals(keyCell.getStringCellValue())) {
throw new IllegalArgumentException("The keyCell (" + keyCell.getRow().getRowNum() + ","
+ keyCell.getColumnIndex() + ") with value (" + keyCell.getStringCellValue()
+ ") is expected to have the key (" + key + ")");
}
Cell valueCell = row.getCell(1);
return valueCell.getNumericCellValue();
}
}
代码示例来源: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: stackoverflow.com
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.println(cell.getNumericCellValue());
break;
case Cell.CELL_TYPE_STRING:
代码示例来源:origin: org.apache.poi/poi
/**
* Return a Format for the given cell if one exists, otherwise try to
* create one. This method will return <code>null</code> if the any of the
* following is true:
* <ul>
* <li>the cell's style is null</li>
* <li>the style's data format string is null or empty</li>
* <li>the format string cannot be recognized as either a number or date</li>
* </ul>
*
* @param cell The cell to retrieve a Format for
* @return A Format for the format String
*/
private Format getFormat(Cell cell, ConditionalFormattingEvaluator cfEvaluator) {
if (cell == null) return null;
ExcelNumberFormat numFmt = ExcelNumberFormat.from(cell, cfEvaluator);
if ( numFmt == null) {
return null;
}
int formatIndex = numFmt.getIdx();
String formatStr = numFmt.getFormat();
if(formatStr == null || formatStr.trim().length() == 0) {
return null;
}
return getFormat(cell.getNumericCellValue(), formatIndex, formatStr);
}
代码示例来源:origin: org.apache.poi/poi
/**
* Check if a cell contains a date
* Since dates are stored internally in Excel as double values
* we infer it is a date if it is formatted as such.
* Format is determined from applicable conditional formatting, if
* any, or cell style.
* @param cell
* @param cfEvaluator if available, or null
* @return true if it looks like a date
* @see #isADateFormat(int, String)
* @see #isInternalDateFormat(int)
*/
public static boolean isCellDateFormatted(Cell cell, ConditionalFormattingEvaluator cfEvaluator) {
if (cell == null) return false;
boolean bDate = false;
double d = cell.getNumericCellValue();
if ( DateUtil.isValidExcelDate(d) ) {
ExcelNumberFormat nf = ExcelNumberFormat.from(cell, cfEvaluator);
if(nf==null) return false;
bDate = isADateFormat(nf);
}
return bDate;
}
代码示例来源:origin: org.apache.poi/poi
/**
* Create and return a Format based on the format string from a cell's
* style. If the pattern cannot be parsed, return a default pattern.
*
* @param cell The Excel cell
* @return A Format representing the excel format. May return null.
*/
public Format createFormat(Cell cell) {
int formatIndex = cell.getCellStyle().getDataFormat();
String formatStr = cell.getCellStyle().getDataFormatString();
return createFormat(cell.getNumericCellValue(), formatIndex, formatStr);
}
代码示例来源:origin: org.apache.poi/poi
/**
* Check if a cell contains a date, checking only for internal
* excel date formats.
* As Excel stores a great many of its dates in "non-internal"
* date formats, you will not normally want to use this method.
* @see #isADateFormat(int,String)
* @see #isInternalDateFormat(int)
*/
public static boolean isCellInternalDateFormatted(Cell cell) {
if (cell == null) return false;
boolean bDate = false;
double d = cell.getNumericCellValue();
if ( DateUtil.isValidExcelDate(d) ) {
CellStyle style = cell.getCellStyle();
int i = style.getDataFormat();
bDate = isInternalDateFormat(i);
}
return bDate;
}
代码示例来源: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
private ValueAndFormat getCellValue(Cell cell) {
if (cell != null) {
final String format = cell.getCellStyle().getDataFormatString();
CellType type = cell.getCellType();
if (type == CellType.FORMULA) {
type = cell.getCachedFormulaResultType();
}
switch (type) {
case NUMERIC:
return new ValueAndFormat(Double.valueOf(cell.getNumericCellValue()), format, decimalTextFormat);
case STRING:
case BOOLEAN:
return new ValueAndFormat(cell.getStringCellValue(), format);
default:
break;
}
}
return new ValueAndFormat("", "");
}
/**
代码示例来源:origin: org.apache.poi/poi-ooxml
private void handleNonStringCell(StringBuilder text, Cell cell, DataFormatter formatter) {
CellType type = cell.getCellType();
if (type == CellType.FORMULA) {
type = cell.getCachedFormulaResultType();
}
if (type == CellType.NUMERIC) {
CellStyle cs = cell.getCellStyle();
if (cs != null && cs.getDataFormatString() != null) {
String contents = formatter.formatRawCellContents(
cell.getNumericCellValue(), cs.getDataFormat(), cs.getDataFormatString());
checkMaxTextSize(text, contents);
text.append(contents);
return;
}
}
// No supported styling applies to this cell
String contents = ((XSSFCell)cell).getRawValue();
if (contents != null) {
checkMaxTextSize(text, contents);
text.append(contents);
}
}
代码示例来源:origin: org.apache.poi/poi
return evaluateFormulaCellValue(cell);
case NUMERIC:
return new CellValue(cell.getNumericCellValue());
case STRING:
return new CellValue(cell.getRichStringCellValue().getString());
内容来源于网络,如有侵权,请联系作者删除!