Java Code Examples for org.apache.poi.ss.usermodel.DateUtil#isADateFormat()
The following examples show how to use
org.apache.poi.ss.usermodel.DateUtil#isADateFormat() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StaxPoiSheet.java From hop with Apache License 2.0 | 5 votes |
@VisibleForTesting protected boolean isDateCell( String cellStyle ) { if ( cellStyle != null ) { int styleIdx = Integer.parseInt( cellStyle ); CTXf cellXf = styles.getCellXfAt( styleIdx ); if ( cellXf != null ) { // need id for builtin types, format if custom short formatId = (short) cellXf.getNumFmtId(); String format = styles.getNumberFormatAt( formatId ); return DateUtil.isADateFormat( formatId, format ); } } return false; }
Example 2
Source File: StAXBasedParser.java From dremio-oss with Apache License 2.0 | 5 votes |
/** * Helper method to determine the output types based on the explicit type given as attribute in "cell" element * and the type through "style" definition. * @param type Value of attribute type {@link ExcelUtil#TYPE} on cell. * @param style Value of attribute style {@link ExcelUtil#STYLE} on cell. */ private void determineOutputTypes(String type, String style) { // Determine the type from attribute valueTypeFromAttribute = MinorType.FLOAT8; if (type != null) { if (TYPE_MAP.containsKey(type)) { valueTypeFromAttribute = TYPE_MAP.get(type); } } valueTypeFromStyle = MinorType.FLOAT8; if (style != null) { if (styleToTypeCache.containsKey(style)) { valueTypeFromStyle = styleToTypeCache.get(style); return; } int styleId = Integer.valueOf(style); CTXf styleDef = styles.getCellXfAt(styleId); if (styleDef != null) { long numFmtId = styleDef.getNumFmtId(); String format = styles.getNumberFormatAt((short) numFmtId); if (DateUtil.isADateFormat((int) numFmtId, format)) { valueTypeFromStyle = MinorType.TIMESTAMP; } } } styleToTypeCache.put(style, valueTypeFromStyle); }
Example 3
Source File: StaxPoiSheet.java From pentaho-kettle with Apache License 2.0 | 5 votes |
@VisibleForTesting protected boolean isDateCell( String cellStyle ) { if ( cellStyle != null ) { int styleIdx = Integer.parseInt( cellStyle ); CTXf cellXf = styles.getCellXfAt( styleIdx ); if ( cellXf != null ) { // need id for builtin types, format if custom short formatId = (short) cellXf.getNumFmtId(); String format = styles.getNumberFormatAt( formatId ); return DateUtil.isADateFormat( formatId, format ); } } return false; }
Example 4
Source File: DataFormatter1.java From easyexcel with Apache License 2.0 | 4 votes |
private Format getFormat(double cellValue, int formatIndex, String formatStrIn) { localeChangedObservable.checkForLocaleChange(); // Might be better to separate out the n p and z formats, falling back to p when n and z are not set. // That however would require other code to be re factored. // String[] formatBits = formatStrIn.split(";"); // int i = cellValue > 0.0 ? 0 : cellValue < 0.0 ? 1 : 2; // String formatStr = (i < formatBits.length) ? formatBits[i] : formatBits[0]; String formatStr = formatStrIn; // Excel supports 2+ part conditional data formats, eg positive/negative/zero, // or (>1000),(>0),(0),(negative). As Java doesn't handle these kinds // of different formats for different ranges, just +ve/-ve, we need to // handle these ourselves in a special way. // For now, if we detect 2+ parts, we call out to CellFormat to handle it // TODO Going forward, we should really merge the logic between the two classes if (formatStr.contains(";") && (formatStr.indexOf(';') != formatStr.lastIndexOf(';') || rangeConditionalPattern.matcher(formatStr).matches())) { try { // Ask CellFormat to get a formatter for it CellFormat cfmt = CellFormat.getInstance(locale, formatStr); // CellFormat requires callers to identify date vs not, so do so Object cellValueO = Double.valueOf(cellValue); if (DateUtil.isADateFormat(formatIndex, formatStr) && // don't try to handle Date value 0, let a 3 or 4-part format take care of it ((Double)cellValueO).doubleValue() != 0.0) { cellValueO = DateUtil.getJavaDate(cellValue); } // Wrap and return (non-cachable - CellFormat does that) return new CellFormatResultWrapper(cfmt.apply(cellValueO)); } catch (Exception e) { logger.log(POILogger.WARN, "Formatting failed for format " + formatStr + ", falling back", e); } } // Excel's # with value 0 will output empty where Java will output 0. This hack removes the # from the format. if (emulateCSV && cellValue == 0.0 && formatStr.contains("#") && !formatStr.contains("0")) { formatStr = formatStr.replaceAll("#", ""); } // See if we already have it cached Format format = formats.get(formatStr); if (format != null) { return format; } // Is it one of the special built in types, General or @? if ("General".equalsIgnoreCase(formatStr) || "@".equals(formatStr)) { return generalNumberFormat; } // Build a formatter, and cache it format = createFormat(cellValue, formatIndex, formatStr); formats.put(formatStr, format); return format; }
Example 5
Source File: DataFormatter1.java From easyexcel with Apache License 2.0 | 4 votes |
/** * Formats the given raw cell value, based on the supplied format index and string, according to excel style rules. * * @see #formatCellValue(Cell) */ public String formatRawCellContents(double value, int formatIndex, String formatString, boolean use1904Windowing) { localeChangedObservable.checkForLocaleChange(); // Is it a date? if (DateUtil.isADateFormat(formatIndex, formatString)) { if (DateUtil.isValidExcelDate(value)) { Format dateFormat = getFormat(value, formatIndex, formatString); if (dateFormat instanceof ExcelStyleDateFormatter) { // Hint about the raw excel value ((ExcelStyleDateFormatter)dateFormat).setDateToBeFormatted(value); } Date d = DateUtil.getJavaDate(value, use1904Windowing); return performDateFormatting(d, dateFormat); } // RK: Invalid dates are 255 #s. if (emulateCSV) { return invalidDateTimeString; } } // else Number Format numberFormat = getFormat(value, formatIndex, formatString); if (numberFormat == null) { return String.valueOf(value); } // When formatting 'value', double to text to BigDecimal produces more // accurate results than double to Double in JDK8 (as compared to // previous versions). However, if the value contains E notation, this // would expand the values, which we do not want, so revert to // original method. String result; final String textValue = NumberToTextConverter.toText(value); if (textValue.indexOf('E') > -1) { result = numberFormat.format(new Double(value)); } else { result = numberFormat.format(new BigDecimal(textValue)); } // Complete scientific notation by adding the missing +. if (result.indexOf('E') > -1 && !result.contains("E-")) { result = result.replaceFirst("E", "E+"); } return result; }
Example 6
Source File: FormatManager.java From dremio-oss with Apache License 2.0 | 4 votes |
boolean isDateFormat(CellValueRecordInterface cell) { final int formatIndex = getFormatIndex(cell); return DateUtil.isADateFormat(formatIndex, getFormatString(formatIndex)); }
Example 7
Source File: XlsxNumberFormats.java From rapidminer-studio with GNU Affero General Public License v3.0 | 4 votes |
private boolean checkForDateFormat(int numberFormatId, String formatCode) { return DateUtil.isADateFormat(numberFormatId, formatCode); }