Example usage for org.apache.poi.hssf.record FormatRecord getFormatString

List of usage examples for org.apache.poi.hssf.record FormatRecord getFormatString

Introduction

In this page you can find the example usage for org.apache.poi.hssf.record FormatRecord getFormatString.

Prototype

public String getFormatString() 

Source Link

Document

get the format string

Usage

From source file:XLS2CSVmra.java

License:Apache License

/**
 * Formats a number or date cell, be that a real number, or the
 *  answer to a formula//from ww  w .  ja  va 2 s  .co m
 */
private String formatNumberDateCell(CellValueRecordInterface cell, double value) {
    // Get the built in format, if there is one
    ExtendedFormatRecord xfr = (ExtendedFormatRecord) xfRecords.get(cell.getXFIndex());
    if (xfr == null) {
        System.err.println("Cell " + cell.getRow() + "," + cell.getColumn() + " uses XF with index "
                + cell.getXFIndex() + ", but we don't have that");
        return Double.toString(value);
    } else {
        int formatIndex = xfr.getFormatIndex();
        String format;
        if (formatIndex >= HSSFDataFormat.getNumberOfBuiltinBuiltinFormats()) {
            FormatRecord tfr = (FormatRecord) customFormatRecords.get(new Integer(formatIndex));
            format = tfr.getFormatString();
        } else {
            format = HSSFDataFormat.getBuiltinFormat(xfr.getFormatIndex());
        }

        // Is it a date?
        if (HSSFDateUtil.isADateFormat(formatIndex, format) && HSSFDateUtil.isValidExcelDate(value)) {
            // Java wants M not m for month
            format = format.replace('m', 'M');
            // Change \- into -, if it's there
            format = format.replaceAll("\\\\-", "-");

            // Format as a date
            Date d = HSSFDateUtil.getJavaDate(value, false);
            DateFormat df = new SimpleDateFormat(format);
            return df.format(d);
        } else {
            if (format.toUpperCase().equals("GENERAL")) {
                // Some sort of wierd default
                return Double.toString(value);
            }

            // Format as a number
            DecimalFormat df = new DecimalFormat(format);
            return df.format(value);
        }
    }
}

From source file:com.adanac.excel.reader.hssf.ExcelFormatTrackingHSSFListener.java

License:Apache License

/**
 * Returns the format string, eg $##.##, for the given number format index.
 *///from   w  ww . j  ava 2  s  . c  om
public String getFormatString(int formatIndex) {
    String format = null;
    if (formatIndex >= ExcelHSSFDataFormat.getNumberOfBuiltinBuiltinFormats()) {
        FormatRecord tfr = _customFormatRecords.get(Integer.valueOf(formatIndex));
        if (tfr == null) {
            logger.log(POILogger.ERROR, "Requested format at index " + formatIndex + ", but it wasn't found");
        } else {
            format = tfr.getFormatString();
        }
    } else {
        format = ExcelHSSFDataFormat.getBuiltinFormat((short) formatIndex);
    }
    return format;
}

From source file:com.adanac.excel.reader.usermodel.ExcelHSSFDataFormat.java

License:Apache License

/**
 * Constructs a new data formatter.  It takes a workbook to have
 * access to the workbooks format records.
 * @param workbook the workbook the formats are tied to.
 *//*  ww  w  . j  a  v a 2 s.  co  m*/
ExcelHSSFDataFormat(InternalWorkbook workbook) {
    _workbook = workbook;

    Iterator<FormatRecord> i = workbook.getFormats().iterator();
    while (i.hasNext()) {
        FormatRecord r = i.next();
        ensureFormatsSize(r.getIndexCode());
        _formats.set(r.getIndexCode(), r.getFormatString());
    }
}