/** * 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 }
/** * 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); }
/** * 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); }
/** * 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". * * @param cell The cell * @return a formatted number string */ private String getFormattedNumberString(Cell cell) { Format numberFormat = getFormat(cell); double d = cell.getNumericCellValue(); if (numberFormat == null) { return String.valueOf(d); } return numberFormat.format(new Double(d)); }
/** * 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". * * @param cell The cell * @return a formatted number string */ private String getFormattedNumberString(Cell cell) { Format numberFormat = getFormat(cell); double d = cell.getNumericCellValue(); if (numberFormat == null) { return String.valueOf(d); } return numberFormat.format(new Double(d)); }
/** * 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 }
/** * 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); }
/** * 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) { if ( cell.getCellStyle() == null) { return null; } int formatIndex = cell.getCellStyle().getDataFormat(); String formatStr = cell.getCellStyle().getDataFormatString(); if(formatStr == null || formatStr.trim().length() == 0) { return null; } return getFormat(cell.getNumericCellValue(), formatIndex, formatStr); }
/** * 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) { if ( cell.getCellStyle() == null) { return null; } int formatIndex = cell.getCellStyle().getDataFormat(); String formatStr = cell.getCellStyle().getDataFormatString(); if(formatStr == null || formatStr.trim().length() == 0) { return null; } return getFormat(cell.getNumericCellValue(), formatIndex, formatStr); }
/** * 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. * * @param cell The cell * @return a formatted date string */ private String getFormattedDateString(Cell cell) { Format dateFormat = getFormat(cell); if(dateFormat instanceof ExcelStyleDateFormatter) { // Hint about the raw excel value ((ExcelStyleDateFormatter)dateFormat).setDateToBeFormatted( cell.getNumericCellValue() ); } Date d = cell.getDateCellValue(); return performDateFormatting(d, dateFormat); }
/** * 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. * * @param cell The cell * @return a formatted date string */ private String getFormattedDateString(Cell cell) { Format dateFormat = getFormat(cell); if(dateFormat instanceof ExcelStyleDateFormatter) { // Hint about the raw excel value ((ExcelStyleDateFormatter)dateFormat).setDateToBeFormatted( cell.getNumericCellValue() ); } Date d = cell.getDateCellValue(); return performDateFormatting(d, dateFormat); }
/** * 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); }