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

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

Introduction

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

Prototype

public int getIndexCode() 

Source Link

Document

get the format index code (for built in formats)

Usage

From source file:XLS2CSVmra.java

License:Apache License

/**
 * Main HSSFListener method, processes events, and outputs the
 *  CSV as the file is processed.//from w  w w.  ja  va  2  s  . c o  m
 */
public void processRecord(Record record) {
    int thisRow = -1;
    int thisColumn = -1;
    String thisStr = null;

    switch (record.getSid()) {
    case SSTRecord.sid:
        sstRecord = (SSTRecord) record;
        break;
    case FormatRecord.sid:
        FormatRecord fr = (FormatRecord) record;
        customFormatRecords.put(new Integer(fr.getIndexCode()), fr);
        break;
    case ExtendedFormatRecord.sid:
        ExtendedFormatRecord xr = (ExtendedFormatRecord) record;
        xfRecords.add(xr);
        break;

    case BlankRecord.sid:
        BlankRecord brec = (BlankRecord) record;

        thisRow = brec.getRow();
        thisColumn = brec.getColumn();
        thisStr = "";
        break;
    case BoolErrRecord.sid:
        BoolErrRecord berec = (BoolErrRecord) record;

        thisRow = berec.getRow();
        thisColumn = berec.getColumn();
        thisStr = "";
        break;
    case FormulaRecord.sid:
        FormulaRecord frec = (FormulaRecord) record;

        thisRow = frec.getRow();
        thisColumn = frec.getColumn();

        if (outputFormulaValues) {
            thisStr = '"' + formatNumberDateCell(frec, frec.getValue()) + '"';
        } else {
            // TODO: Output the formula string
            thisStr = '"' + frec.toString().replaceAll("\"", "\"\"") + '"';
        }
        break;
    case LabelRecord.sid:
        LabelRecord lrec = (LabelRecord) record;

        thisRow = lrec.getRow();
        thisColumn = lrec.getColumn();
        thisStr = '"' + lrec.getValue().replaceAll("\"", "\"\"") + '"';
        break;
    case LabelSSTRecord.sid:
        LabelSSTRecord lsrec = (LabelSSTRecord) record;

        thisRow = lsrec.getRow();
        thisColumn = lsrec.getColumn();
        if (sstRecord == null) {
            thisStr = '"' + "(No SST Record, can't identify string)" + '"';
        } else {
            thisStr = '"' + sstRecord.getString(lsrec.getSSTIndex()).toString().replaceAll("\"", "\"\"") + '"';
        }
        break;
    case NoteRecord.sid:
        NoteRecord nrec = (NoteRecord) record;

        thisRow = nrec.getRow();
        thisColumn = nrec.getColumn();
        // TODO: Find object to match nrec.getShapeId()
        thisStr = '"' + "(TODO)" + '"';
        break;
    case NumberRecord.sid:
        NumberRecord numrec = (NumberRecord) record;

        thisRow = numrec.getRow();
        thisColumn = numrec.getColumn();

        // Format
        thisStr = '"' + formatNumberDateCell(numrec, numrec.getValue()) + '"';
        break;
    case RKRecord.sid:
        RKRecord rkrec = (RKRecord) record;

        thisRow = rkrec.getRow();
        thisColumn = rkrec.getColumn();
        thisStr = '"' + "(TODO)" + '"';
        break;
    default:
        break;
    }

    // Handle new row
    if (thisRow != -1 && thisRow != lastRowNumber) {
        lastColumnNumber = -1;
    }

    // Handle missing column
    if (record instanceof MissingCellDummyRecord) {
        MissingCellDummyRecord mc = (MissingCellDummyRecord) record;
        thisRow = mc.getRow();
        thisColumn = mc.getColumn();
        thisStr = "";
    }

    // If we got something to print out, do so
    if (thisStr != null) {
        if (thisColumn > 0) {
            output.print(',');
        }
        output.print(thisStr);
    }

    // Update column and row count
    if (thisRow > -1)
        lastRowNumber = thisRow;
    if (thisColumn > -1)
        lastColumnNumber = thisColumn;

    //set the minColumns based on the first row (headers)
    if (thisRow == 0)
        minColumns = thisColumn;

    // Handle end of row
    if (record instanceof LastCellOfRowDummyRecord) {
        // Print out any missing commas if needed
        if (minColumns > 0) {
            // Columns are 0 based
            if (lastColumnNumber == -1) {
                lastColumnNumber = 0;
            }
            for (int i = lastColumnNumber; i < (minColumns); i++) {
                output.print(",\"\"");
            }
        }

        // We're onto a new row
        lastColumnNumber = -1;

        // End the row
        output.println();
    }
}

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

License:Apache License

/**
 * Process the record ourselves, but do not pass it on to the child
 * Listener.//ww  w.  j  ava 2 s  .com
 *
 * @param record
 */
public void processRecordInternally(Record record) {
    if (record instanceof FormatRecord) {
        FormatRecord fr = (FormatRecord) record;
        _customFormatRecords.put(Integer.valueOf(fr.getIndexCode()), fr);
    }
    if (record instanceof ExtendedFormatRecord) {
        ExtendedFormatRecord xr = (ExtendedFormatRecord) record;
        _xfRecords.add(xr);
    }
}

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.
 *///from   w  w 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());
    }
}