org.apache.poi.hssf.usermodel.HSSFDataFormatter Java Examples

The following examples show how to use org.apache.poi.hssf.usermodel.HSSFDataFormatter. 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: ExcelOperator.java    From minsx-framework with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
private static String getCellData(Cell cell) {
    String value = null;
    if (cell == null) {
        return null;
    }
    switch (cell.getCellType()) {
        case HSSFCell.CELL_TYPE_STRING:
            value = cell.getStringCellValue();
            break;
        case HSSFCell.CELL_TYPE_FORMULA:
            value = cell.getCellFormula();
            break;
        case HSSFCell.CELL_TYPE_NUMERIC:
            HSSFDataFormatter dataFormatter = new HSSFDataFormatter();
            value = dataFormatter.formatCellValue(cell);
            break;
        case HSSFCell.CELL_TYPE_BLANK:
            value = null;
            break;
        case HSSFCell.CELL_TYPE_ERROR:
            value = "#ERROR#";
            break;
    }
    return value;
}
 
Example #2
Source File: XlsUtils.java    From data-prep with Apache License 2.0 6 votes vote down vote up
/**
 * Return the numeric value.
 *
 * @param cell the cell to extract the value from.
 * @return the numeric value from the cell.
 */
private static String getNumericValue(Cell cell, CellValue cellValue, boolean fromFormula) {
    // Date is typed as numeric
    if (HSSFDateUtil.isCellDateFormatted(cell)) { // TODO configurable??
        DateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);
        return sdf.format(cell.getDateCellValue());
    }
    // Numeric type (use data formatter to get number format right)
    DataFormatter formatter = new HSSFDataFormatter(Locale.ENGLISH);

    if (cellValue == null) {
        return formatter.formatCellValue(cell);
    }

    return fromFormula ? cellValue.formatAsString() : formatter.formatCellValue(cell);
}
 
Example #3
Source File: ExcelExtractor.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public ExcelExtractor(HSSFWorkbook wb) {
	super(wb);
	_wb = wb;
	_formatter = new HSSFDataFormatter();
}
 
Example #4
Source File: FormatTrackingHSSFListener.java    From lams with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Creates a format tracking wrapper around the given listener, using
 * the given locale for the formats.
    * 
    * @param childListener the listener to be wrapped
    * @param locale the locale for the formats
 */
public FormatTrackingHSSFListener(
		HSSFListener childListener, Locale locale) {
	_childListener = childListener;
	_formatter = new HSSFDataFormatter(locale);
	_defaultFormat = NumberFormat.getInstance(locale);
}