Example usage for org.apache.poi.ss.usermodel DateUtil isADateFormat

List of usage examples for org.apache.poi.ss.usermodel DateUtil isADateFormat

Introduction

In this page you can find the example usage for org.apache.poi.ss.usermodel DateUtil isADateFormat.

Prototype

public static boolean isADateFormat(int formatIndex, String formatString) 

Source Link

Document

Given a format ID and its format String, will check to see if the format represents a date format or not.

Usage

From source file:com.googlecode.sqlsheet.stream.AbstractXlsSheetIterator.java

License:Apache License

Date convertDateValue(double value, int formatIndex, String formatString) {
    // Get the built in format, if there is one
    if (DateUtil.isADateFormat(formatIndex, formatString)) {
        if (DateUtil.isValidExcelDate(value)) {
            return DateUtil.getJavaDate(value, false);
        }//from  w w w  .j  a  v a  2s . c o m
    }
    return null;
}

From source file:com.ostrichemulators.semtool.poi.main.xlsxml.LoadingSheetXmlHandler.java

@Override
public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {
    if (null != name) {
        switch (name) {
        case "row":
            rownum = Integer.parseInt(attributes.getValue("r")) - 1;
            currentrowdata.clear();//from  w  w w .j av  a2s.c o m
            break;
        case "c": // c is a new cell
            String celltypestr = attributes.getValue("t");
            celltype = (formats.containsKey(celltypestr) ? formats.get(celltypestr) : CellType.BLANK);
            // dates don't always have a type attribute
            if (CellType.NUMERIC == celltype || null == celltypestr) {
                celltype = CellType.NUMERIC;

                // check if it's a date
                String styleidstr = attributes.getValue("s");
                int styleid = (null == styleidstr ? 0 : Integer.parseInt(styleidstr));

                XSSFCellStyle style = styles.getStyleAt(styleid);
                int formatIndex = style.getDataFormat();
                String formatString = style.getDataFormatString();
                isdate = DateUtil.isADateFormat(formatIndex, formatString);
            }

            String colname = attributes.getValue("r");
            colnum = getColNum(colname.substring(0, colname.lastIndexOf(Integer.toString(rownum + 1))));
            break;
        case "v": // new value for a cell
            setReading(true);
            resetContents();
            break;
        }
    }
}

From source file:com.rapidminer.operator.nio.model.xlsx.XlsxNumberFormats.java

License:Open Source License

private boolean checkForDateFormat(int numberFormatId, String formatCode) {
    return DateUtil.isADateFormat(numberFormatId, formatCode);
}

From source file:net.geoprism.data.etl.excel.ExcelDataFormatter.java

License:Open Source License

@Override
public String formatRawCellContents(double value, int formatIndex, String formatString) {
    if (DateUtil.isADateFormat(formatIndex, formatString)) {
        return super.formatRawCellContents(value, formatIndex, DATE_TIME_FORMAT);
    } else {/*from   w  w  w.  ja  va 2  s  . c o  m*/
        return new Double(value).toString();
        //      return super.formatRawCellContents(value, formatIndex, formatString);
    }
}

From source file:net.geoprism.data.etl.excel.ExcelDataFormatter.java

License:Open Source License

@Override
public String formatRawCellContents(double value, int formatIndex, String formatString,
        boolean use1904Windowing) {
    if (DateUtil.isADateFormat(formatIndex, formatString)) {
        return super.formatRawCellContents(value, formatIndex, DATE_TIME_FORMAT, use1904Windowing);
    } else {/*from   w  ww .j  av  a 2 s.c o  m*/
        return new Double(value).toString();
        //       return super.formatRawCellContents(value, formatIndex, formatString, use1904Windowing);
    }
}

From source file:net.geoprism.data.etl.excel.XSSFSheetXMLHandler.java

License:Open Source License

public void endElement(String uri, String localName, String name) throws SAXException {
    String thisStr = null;//  ww  w .ja  v a  2  s  . co  m
    String thisData = null;

    // v => contents of a cell
    if (isTextTag(name)) {
        vIsOpen = false;

        // Process the value contents as required, now we have it all
        switch (nextDataType) {
        case BOOLEAN:
            char first = value.charAt(0);
            thisStr = first == '0' ? "FALSE" : "TRUE";
            thisData = new String(thisStr);
            break;

        case ERROR:
            thisStr = "ERROR:" + value.toString();
            thisData = new String(thisStr);
            break;

        case FORMULA:
            if (formulasNotResults) {
                thisStr = formula.toString();
                thisData = new String(thisStr);
            } else {
                String fv = value.toString();

                if (this.formatString != null) {
                    try {
                        // Try to use the value as a formattable number
                        double d = Double.parseDouble(fv);
                        thisStr = this.cellFormatter.formatRawCellContents(d, this.formatIndex,
                                this.formatString);
                        thisData = this.contentFormatter.formatRawCellContents(d, this.formatIndex,
                                this.formatString);
                    } catch (NumberFormatException e) {
                        // Formula is a String result not a Numeric one
                        thisStr = fv;
                        thisData = new String(thisStr);
                    }
                } else {
                    // No formating applied, just do raw value in all cases
                    thisStr = fv;
                    thisData = new String(thisStr);
                }
            }
            break;

        case INLINE_STRING:
            // TODO: Can these ever have formatting on them?
            XSSFRichTextString rtsi = new XSSFRichTextString(value.toString());
            thisStr = rtsi.toString();
            thisData = new String(thisStr);
            break;

        case TEXT:
            String sstIndex = value.toString();
            try {
                int idx = Integer.parseInt(sstIndex);
                XSSFRichTextString rtss = new XSSFRichTextString(sharedStringsTable.getEntryAt(idx));
                thisStr = rtss.toString();
                thisData = new String(thisStr);
            } catch (NumberFormatException ex) {
                System.err.println("Failed to parse SST index '" + sstIndex + "': " + ex.toString());
            }
            break;

        case NUMBER:
            String n = value.toString();
            if (this.formatString != null) {
                thisStr = cellFormatter.formatRawCellContents(Double.parseDouble(n), this.formatIndex,
                        this.formatString);
                thisData = this.contentFormatter.formatRawCellContents(Double.parseDouble(n), this.formatIndex,
                        this.formatString);
            } else {
                thisStr = n;
                thisData = new String(thisStr);
            }
            break;

        default:
            thisStr = "(TODO: Unexpected type: " + nextDataType + ")";
            thisData = new String(thisStr);
            break;
        }

        if (DateUtil.isADateFormat(this.formatIndex, this.formatString)) {
            output.cell(cellRef, thisData, thisStr, ColumnType.DATE);
        } else {
            output.cell(cellRef, thisData, thisStr, nextDataType);
        }
    } else if ("f".equals(name)) {
        fIsOpen = false;
    } else if ("is".equals(name)) {
        isIsOpen = false;
    } else if ("row".equals(name)) {
        output.endRow();
    } else if ("oddHeader".equals(name) || "evenHeader".equals(name) || "firstHeader".equals(name)) {
        hfIsOpen = false;
        output.headerFooter(headerFooter.toString(), true, name);
    } else if ("oddFooter".equals(name) || "evenFooter".equals(name) || "firstFooter".equals(name)) {
        hfIsOpen = false;
        output.headerFooter(headerFooter.toString(), false, name);
    }
}

From source file:org.bbreak.excella.core.util.PoiUtil.java

License:Open Source License

/**
 * DateUtil?Localize??(,,?)?????????//  www  .ja  v a2s .c  o  m
 * ?""????????
 * DateUtil???????? 
 * Bug 47071????
 * 
 * @param cell 
 */
public static boolean isCellDateFormatted(Cell cell) {
    if (cell == null) {
        return false;
    }
    boolean bDate = false;

    double d = cell.getNumericCellValue();
    if (DateUtil.isValidExcelDate(d)) {
        CellStyle style = cell.getCellStyle();
        if (style == null) {
            return false;
        }
        int i = style.getDataFormat();
        String fs = style.getDataFormatString();
        if (fs != null) {
            // And '"any"' into ''
            while (fs.contains("\"")) {
                int beginIdx = fs.indexOf("\"");
                if (beginIdx == -1) {
                    break;
                }
                int endIdx = fs.indexOf("\"", beginIdx + 1);
                if (endIdx == -1) {
                    break;
                }
                fs = fs.replaceFirst(Pattern.quote(fs.substring(beginIdx, endIdx + 1)), "");
            }
        }
        bDate = DateUtil.isADateFormat(i, fs);
    }
    return bDate;
}

From source file:org.pentaho.di.trans.steps.excelinput.staxpoi.StaxPoiSheet.java

License:Apache License

@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);
        }//from   w w  w  .  j  a v  a  2  s.c om
    }
    return false;
}

From source file:org.tiefaces.components.websheet.utility.CellStyleUtility.java

License:MIT License

/**
 * Gets the input type from cell type.//ww w .  j  a  v  a 2s.com
 *
 * @param cell
 *            the cell
 * @return the input type from cell type
 */
@SuppressWarnings("deprecation")
private static String getInputTypeFromCellType(final Cell cell) {

    String inputType = TieConstants.CELL_INPUT_TYPE_TEXT;
    if (cell.getCellTypeEnum() == CellType.NUMERIC) {
        inputType = TieConstants.CELL_INPUT_TYPE_DOUBLE;
    }
    CellStyle style = cell.getCellStyle();
    if (style != null) {
        int formatIndex = style.getDataFormat();
        String formatString = style.getDataFormatString();
        if (DateUtil.isADateFormat(formatIndex, formatString)) {
            inputType = TieConstants.CELL_INPUT_TYPE_DATE;
        } else {
            if (isAPercentageCell(formatString)) {
                inputType = TieConstants.CELL_INPUT_TYPE_PERCENTAGE;
            }
        }
    }
    return inputType;
}