Example usage for org.apache.poi.hssf.record FormulaRecord getParsedExpression

List of usage examples for org.apache.poi.hssf.record FormulaRecord getParsedExpression

Introduction

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

Prototype

public Ptg[] getParsedExpression() 

Source Link

Usage

From source file:XLS2CSV.java

License:Apache License

/**
 * Main HSSFListener method, processes events, and outputs the CSV as the
 * file is processed./*w  w  w  .  j  ava  2s.  com*/
 */
@SuppressWarnings("unchecked")
public void processRecord(Record record) {
    int thisRow = -1;
    int thisColumn = -1;
    String thisStr = null;

    switch (record.getSid()) {
    case BoundSheetRecord.sid:
        boundSheetRecords.add(record);
        break;
    case BOFRecord.sid:
        BOFRecord br = (BOFRecord) record;
        if (br.getType() == BOFRecord.TYPE_WORKSHEET) {
            // Create sub workbook if required
            if (workbookBuildingListener != null && stubWorkbook == null) {
                stubWorkbook = workbookBuildingListener.getStubHSSFWorkbook();
            }

            // Output the worksheet name
            // Works by ordering the BSRs by the location of
            // their BOFRecords, and then knowing that we
            // process BOFRecords in byte offset order
            sheetIndex++;
            if (orderedBSRs == null) {
                orderedBSRs = BoundSheetRecord.orderByBofPosition(boundSheetRecords);
            }
            output.println();
            output.println(orderedBSRs[sheetIndex].getSheetname() + " [" + (sheetIndex + 1) + "]:");
        }
        break;

    case SSTRecord.sid:
        sstRecord = (SSTRecord) record;
        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) {
            if (Double.isNaN(frec.getValue())) {
                // Formula result is a string
                // This is stored in the next record
                outputNextStringRecord = true;
                nextRow = frec.getRow();
                nextColumn = frec.getColumn();
            } else {
                thisStr = formatListener.formatNumberDateCell(frec);
            }
        } else {
            thisStr = '"' + HSSFFormulaParser.toFormulaString(stubWorkbook, frec.getParsedExpression()) + '"';
        }
        break;
    case StringRecord.sid:
        if (outputNextStringRecord) {
            // String for formula
            StringRecord srec = (StringRecord) record;
            thisStr = srec.getString();
            thisRow = nextRow;
            thisColumn = nextColumn;
            outputNextStringRecord = false;
        }
        break;

    case LabelRecord.sid:
        LabelRecord lrec = (LabelRecord) record;

        thisRow = lrec.getRow();
        thisColumn = lrec.getColumn();
        thisStr = '"' + lrec.getValue() + '"';
        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() + '"';
        }
        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 = formatListener.formatNumberDateCell(numrec);
        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;

    // 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:cn.sinobest.jzpt.minidemo.poidemo.example.XLS2CSVmra.java

License:Apache License

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

    switch (record.getSid()) {
    case BoundSheetRecord.sid:
        boundSheetRecords.add(record);
        break;
    case BOFRecord.sid:
        BOFRecord br = (BOFRecord) record;
        if (br.getType() == BOFRecord.TYPE_WORKSHEET) {
            // Create sub workbook if required
            if (workbookBuildingListener != null && stubWorkbook == null) {
                stubWorkbook = workbookBuildingListener.getStubHSSFWorkbook();
            }

            // Output the worksheet name
            // Works by ordering the BSRs by the location of
            //  their BOFRecords, and then knowing that we
            //  process BOFRecords in byte offset order
            sheetIndex++;
            if (orderedBSRs == null) {
                orderedBSRs = BoundSheetRecord.orderByBofPosition(boundSheetRecords);
            }
            output.println();
            output.println(orderedBSRs[sheetIndex].getSheetname() + " [" + (sheetIndex + 1) + "]:");
        }
        break;

    case SSTRecord.sid:
        sstRecord = (SSTRecord) record;
        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) {
            if (Double.isNaN(frec.getValue())) {
                // Formula result is a string
                // This is stored in the next record
                outputNextStringRecord = true;
                nextRow = frec.getRow();
                nextColumn = frec.getColumn();
            } else {
                thisStr = formatListener.formatNumberDateCell(frec);
            }
        } else {
            thisStr = '"' + HSSFFormulaParser.toFormulaString(stubWorkbook, frec.getParsedExpression()) + '"';
        }
        break;
    case StringRecord.sid:
        if (outputNextStringRecord) {
            // String for formula
            StringRecord srec = (StringRecord) record;
            thisStr = srec.getString();
            thisRow = nextRow;
            thisColumn = nextColumn;
            outputNextStringRecord = false;
        }
        break;

    case LabelRecord.sid:
        LabelRecord lrec = (LabelRecord) record;

        thisRow = lrec.getRow();
        thisColumn = lrec.getColumn();
        thisStr = '"' + lrec.getValue() + '"';
        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() + '"';
        }
        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;
        System.out.println(numrec.getSid());

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

        // Format
        thisStr = formatListener.formatNumberDateCell(numrec);
        //            if(HSSFDateUtil.isValidExcelDate(numrec.getValue())){
        System.out.println(HSSFDateUtil.isValidExcelDate(numrec.getValue()));

        //            }
        //            System.out.println("formatListener--------"+formatListener.getFormatString(1));
        //            System.out.println("thisStr----------"+thisStr);
        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;

    // 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.bayareasoftware.chartengine.ds.util.XLS2Data.java

License:Apache License

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

    //p("looking at " + record.getClass().getName() + " sid=0x" + Integer.toHexString(record.getSid()));
    if ((record.getSid() != BoundSheetRecord.sid && record.getSid() != BOFRecord.sid)
            && currentSheetName != null && this.requiredSheetName != null
            && !this.requiredSheetName.equals(currentSheetName)) {
        return;
    }
    switch (record.getSid()) {
    case BoundSheetRecord.sid:
        boundSheetRecords.add(record);
        break;
    case BOFRecord.sid:
        BOFRecord br = (BOFRecord) record;
        if (br.getType() == BOFRecord.TYPE_WORKSHEET) {
            // Create sub workbook if required
            if (workbookBuildingListener != null && stubWorkbook == null) {
                stubWorkbook = workbookBuildingListener.getStubHSSFWorkbook();
            }

            // Output the worksheet name
            // Works by ordering the BSRs by the location of
            // their BOFRecords, and then knowing that we
            // process BOFRecords in byte offset order
            sheetIndex++;
            if (orderedBSRs == null) {
                orderedBSRs = BoundSheetRecord.orderByBofPosition(boundSheetRecords);
            }
            //output.println();
            startNewSheet();
            currentSheetName = orderedBSRs[sheetIndex].getSheetname();
            //System.err.println("got sheet name '" + currentSheetName + "' required='"
            //      + this.requiredSheetName + "'");
            /*if (requiredSheetName == null) {
            startNewRow();
            //output.println(orderedBSRs[sheetIndex].getSheetname() + " ["
            //+ (sheetIndex + 1) + "]:");
            appendCell(currentSheetName + " ["
                    + (sheetIndex + 1) + "]:");
            startNewRow();
            }
            */
        } else if (br.getType() == BOFRecord.TYPE_CHART) {
            //p("got chart");
        }
        break;

    case SSTRecord.sid:
        sstRecord = (SSTRecord) record;
        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) {
            if (Double.isNaN(frec.getValue())) {
                // Formula result is a string
                // This is stored in the next record
                outputNextStringRecord = true;
                nextRow = frec.getRow();
                nextColumn = frec.getColumn();
            } else {
                thisStr = "" + frec.getValue();
                //thisStr = formatListener.formatNumberDateCell(frec);
            }
        } else {
            thisStr = '"' + HSSFFormulaParser.toFormulaString(stubWorkbook, frec.getParsedExpression()) + '"';
        }
        break;
    case StringRecord.sid:
        if (outputNextStringRecord) {
            // String for formula
            StringRecord srec = (StringRecord) record;
            thisStr = srec.getString();
            thisRow = nextRow;
            thisColumn = nextColumn;
            outputNextStringRecord = false;
        }
        break;

    case LabelRecord.sid:
        LabelRecord lrec = (LabelRecord) record;

        thisRow = lrec.getRow();
        thisColumn = lrec.getColumn();
        //thisStr = '"' + lrec.getValue() + '"';
        thisStr = lrec.getValue();
        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() + '"';
            thisStr = sstRecord.getString(lsrec.getSSTIndex()).toString();
        }
        break;
    case NoteRecord.sid:
        NoteRecord nrec = (NoteRecord) record;

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

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

        // Format
        thisStr = formatListener.formatNumberDateCell(numrec);
        break;
    case RKRecord.sid:
        RKRecord rkrec = (RKRecord) record;

        thisRow = rkrec.getRow();
        thisColumn = rkrec.getColumn();
        thisStr = '"' + "(TODO - RKRecord (" + thisRow + "," + thisColumn + "))" + '"';
        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) {
        appendCell(thisStr);
    }

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

    // Handle end of row
    if (record instanceof LastCellOfRowDummyRecord) {
        // We're onto a new row
        lastColumnNumber = -1;

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

From source file:com.bstek.dorado.importer.policy.impl.XLS2CSV.java

License:Apache License

/**
 * Main HSSFListener method, processes events, and outputs the CSV as the
 * file is processed./*from  www  . j  a v a2s  .  c  om*/
 */
public void processRecord(Record record) {
    int thisRow = -1;
    int thisColumn = -1;
    String thisStr = null;

    switch (record.getSid()) {
    case BoundSheetRecord.sid:
        boundSheetRecords.add(record);
        break;
    case BOFRecord.sid:
        BOFRecord br = (BOFRecord) record;
        if (br.getType() == BOFRecord.TYPE_WORKSHEET) {
            // Create sub workbook if required
            if (workbookBuildingListener != null && stubWorkbook == null) {
                stubWorkbook = workbookBuildingListener.getStubHSSFWorkbook();
            }

            // Output the worksheet name
            // Works by ordering the BSRs by the location of
            // their BOFRecords, and then knowing that we
            // process BOFRecords in byte offset order
            sheetIndex++;
            if (orderedBSRs == null) {
                orderedBSRs = BoundSheetRecord.orderByBofPosition(boundSheetRecords);
            }
            output.println();
            output.println(orderedBSRs[sheetIndex].getSheetname() + " [" + (sheetIndex + 1) + "]:");
        }
        break;

    case SSTRecord.sid:
        sstRecord = (SSTRecord) record;
        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) {
            if (Double.isNaN(frec.getValue())) {
                // Formula result is a string
                // This is stored in the next record
                outputNextStringRecord = true;
                nextRow = frec.getRow();
                nextColumn = frec.getColumn();
            } else {
                thisStr = formatListener.formatNumberDateCell(frec);
            }
        } else {
            thisStr = '"' + HSSFFormulaParser.toFormulaString(stubWorkbook, frec.getParsedExpression()) + '"';
        }
        break;
    case StringRecord.sid:
        if (outputNextStringRecord) {
            // String for formula
            StringRecord srec = (StringRecord) record;
            thisStr = srec.getString();
            thisRow = nextRow;
            thisColumn = nextColumn;
            outputNextStringRecord = false;
        }
        break;

    case LabelRecord.sid:
        LabelRecord lrec = (LabelRecord) record;

        thisRow = lrec.getRow();
        thisColumn = lrec.getColumn();
        thisStr = '"' + lrec.getValue() + '"';
        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() + '"';
        }
        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 = formatListener.formatNumberDateCell(numrec);
        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;

    // 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.epac.java.converter.ms2text.XLS2CSVmra.java

License:Apache License

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

    switch (record.getSid()) {
    case BoundSheetRecord.sid:
        boundSheetRecords.add(record);
        break;
    case BOFRecord.sid:
        BOFRecord br = (BOFRecord) record;
        if (br.getType() == BOFRecord.TYPE_WORKSHEET) {
            // Create sub workbook if required
            if (workbookBuildingListener != null && stubWorkbook == null) {
                stubWorkbook = workbookBuildingListener.getStubHSSFWorkbook();
            }

            // Output the worksheet name
            // Works by ordering the BSRs by the location of
            //  their BOFRecords, and then knowing that we
            //  process BOFRecords in byte offset order
            sheetIndex++;
            if (orderedBSRs == null) {
                orderedBSRs = BoundSheetRecord.orderByBofPosition(boundSheetRecords);
            }
            output.println();
            output.println(orderedBSRs[sheetIndex].getSheetname() + " [" + (sheetIndex + 1) + "]:");
        }
        break;

    case SSTRecord.sid:
        sstRecord = (SSTRecord) record;
        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) {
            if (Double.isNaN(frec.getValue())) {
                // Formula result is a string
                // This is stored in the next record
                outputNextStringRecord = true;
                nextRow = frec.getRow();
                nextColumn = frec.getColumn();
            } else {
                thisStr = formatListener.formatNumberDateCell(frec);
            }
        } else {
            thisStr = '"' + HSSFFormulaParser.toFormulaString(stubWorkbook, frec.getParsedExpression()) + '"';
        }
        break;
    case StringRecord.sid:
        if (outputNextStringRecord) {
            // String for formula
            StringRecord srec = (StringRecord) record;
            thisStr = srec.getString();
            thisRow = nextRow;
            thisColumn = nextColumn;
            outputNextStringRecord = false;
        }
        break;

    case LabelRecord.sid:
        LabelRecord lrec = (LabelRecord) record;

        thisRow = lrec.getRow();
        thisColumn = lrec.getColumn();
        thisStr = '"' + lrec.getValue() + '"';
        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() + '"';
        }
        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 = formatListener.formatNumberDateCell(numrec);
        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;

    // 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.googlecode.sqlsheet.stream.XlsSheetIterator.java

License:Apache License

/**
 * Main HSSFListener method, processes events
 *//*from  w  ww.j a  va 2  s . com*/
public void processRecord(Record record) {
    int thisRow = -1;
    int thisColumn = -1;
    CellValueHolder thisCellValue = new CellValueHolder();

    switch (record.getSid()) {
    case BoundSheetRecord.sid:
        boundSheetRecords.add(record);
        break;
    case BOFRecord.sid:
        BOFRecord br = (BOFRecord) record;
        if (br.getType() == BOFRecord.TYPE_WORKSHEET) {
            // Create sub workbook if required
            if (workbookBuildingListener != null && stubWorkbook == null) {
                stubWorkbook = workbookBuildingListener.getStubHSSFWorkbook();
            }
            // Output the worksheet name
            // Works by ordering the BSRs by the location of
            //  their BOFRecords, and then knowing that we
            //  postConstruct BOFRecords in byte offset order
            sheetIndex++;
            if (orderedBSRs == null) {
                orderedBSRs = BoundSheetRecord.orderByBofPosition(boundSheetRecords);
            }
            inRequiredSheet = getSheetName().equalsIgnoreCase(orderedBSRs[sheetIndex].getSheetname())
                    || ("\"" + orderedBSRs[sheetIndex].getSheetname() + "\"").equalsIgnoreCase(getSheetName());
        }
        break;

    case SSTRecord.sid:
        sstRecord = (SSTRecord) record;
        break;

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

        thisRow = brec.getRow();
        thisColumn = brec.getColumn();
        thisCellValue.stringValue = "";
        break;
    case BoolErrRecord.sid:
        BoolErrRecord berec = (BoolErrRecord) record;
        thisRow = berec.getRow();
        thisColumn = berec.getColumn();
        thisCellValue.stringValue = "";
        break;
    case FormulaRecord.sid:
        FormulaRecord frec = (FormulaRecord) record;
        thisRow = frec.getRow();
        thisColumn = frec.getColumn();
        if (outputFormulaValues) {
            if (Double.isNaN(frec.getValue())) {
                // Formula result is a string
                // This is stored in the next record
                outputNextStringRecord = true;
                nextRow = frec.getRow();
                nextColumn = frec.getColumn();
            } else {
                thisCellValue.stringValue = formatListener.formatNumberDateCell(frec);
                thisCellValue.doubleValue = frec.getValue();
                thisCellValue.dateValue = convertDateValue(frec.getValue(), formatListener.getFormatIndex(frec),
                        formatListener.getFormatString(frec));
            }
        } else {
            thisCellValue.stringValue = HSSFFormulaParser.toFormulaString(stubWorkbook,
                    frec.getParsedExpression());
            thisCellValue.doubleValue = frec.getValue();
            thisCellValue.dateValue = convertDateValue(frec.getValue(), formatListener.getFormatIndex(frec),
                    formatListener.getFormatString(frec));
        }
        break;
    case StringRecord.sid:
        if (outputNextStringRecord) {
            // String for formula
            StringRecord srec = (StringRecord) record;
            thisCellValue.stringValue = srec.getString();
            thisRow = nextRow;
            thisColumn = nextColumn;
            outputNextStringRecord = false;
        }
        break;

    case LabelRecord.sid:
        LabelRecord lrec = (LabelRecord) record;
        thisRow = lrec.getRow();
        thisColumn = lrec.getColumn();
        thisCellValue.stringValue = lrec.getValue();
        break;
    case LabelSSTRecord.sid:
        LabelSSTRecord lsrec = (LabelSSTRecord) record;
        thisRow = lsrec.getRow();
        thisColumn = lsrec.getColumn();
        if (sstRecord == null) {
            thisCellValue.stringValue = '"' + "(No SST Record, can't identify string)" + '"';
        } else {
            thisCellValue.stringValue = sstRecord.getString(lsrec.getSSTIndex()).toString();
        }
        break;
    case NoteRecord.sid:
        NoteRecord nrec = (NoteRecord) record;
        thisRow = nrec.getRow();
        thisColumn = nrec.getColumn();
        // TODO: Find object to match nrec.getShapeId()
        thisCellValue.stringValue = '"' + "(TODO)" + '"';
        break;
    case NumberRecord.sid:
        NumberRecord numrec = (NumberRecord) record;
        thisRow = numrec.getRow();
        thisColumn = numrec.getColumn();
        // Format
        thisCellValue.stringValue = formatListener.formatNumberDateCell(numrec);
        thisCellValue.doubleValue = numrec.getValue();
        thisCellValue.dateValue = convertDateValue(numrec.getValue(), formatListener.getFormatIndex(numrec),
                formatListener.getFormatString(numrec));
        break;
    case RKRecord.sid:
        RKRecord rkrec = (RKRecord) record;
        thisRow = rkrec.getRow();
        thisColumn = rkrec.getColumn();
        thisCellValue.stringValue = '"' + "(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();
        thisCellValue.stringValue = "";
    }
    // If we got something to print out, do so
    if (thisCellValue.stringValue != null) {
        //If we are on the first row - fill column names
        if (getCurrentSheetRowIndex().equals(0L) && inRequiredSheet) {
            getColumns().add(thisCellValue);
        } else if (inRequiredSheet) {
            addCurrentRowValue(thisCellValue);
        }
    }
    // Update column and row count
    if (thisRow > -1)
        lastRowNumber = thisRow;
    if (thisColumn > -1)
        lastColumnNumber = thisColumn;
    // Handle end of row
    if (record instanceof LastCellOfRowDummyRecord) {
        // We're onto a new row
        lastColumnNumber = -1;
        // End the row
        setCurrentSheetRowIndex(getCurrentSheetRowIndex() + 1);
    }
}

From source file:com.googlecode.sqlsheet.XLS2CSVmra.java

License:Apache License

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

    switch (record.getSid()) {
    case BoundSheetRecord.sid:
        boundSheetRecords.add(record);
        break;
    case BOFRecord.sid:
        BOFRecord br = (BOFRecord) record;
        if (br.getType() == BOFRecord.TYPE_WORKSHEET) {
            // Create sub workbook if required
            if (workbookBuildingListener != null && stubWorkbook == null) {
                stubWorkbook = workbookBuildingListener.getStubHSSFWorkbook();
            }

            // Output the worksheet name
            // Works by ordering the BSRs by the location of
            //  their BOFRecords, and then knowing that we
            //  process BOFRecords in byte offset order
            sheetIndex++;
            if (orderedBSRs == null) {
                orderedBSRs = BoundSheetRecord.orderByBofPosition(boundSheetRecords);
            }
            currentSheetName = orderedBSRs[sheetIndex].getSheetname();
            output.println();
            output.println(orderedBSRs[sheetIndex].getSheetname() + " [" + (sheetIndex + 1) + "]:");
        }
        break;

    case SSTRecord.sid:
        sstRecord = (SSTRecord) record;
        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) {
            if (Double.isNaN(frec.getValue())) {
                // Formula result is a string
                // This is stored in the next record
                outputNextStringRecord = true;
                nextRow = frec.getRow();
                nextColumn = frec.getColumn();
            } else {
                thisStr = formatListener.formatNumberDateCell(frec);
            }
        } else {
            thisStr = '"' + HSSFFormulaParser.toFormulaString(stubWorkbook, frec.getParsedExpression()) + '"';
        }
        break;
    case StringRecord.sid:
        if (outputNextStringRecord) {
            // String for formula
            StringRecord srec = (StringRecord) record;
            thisStr = srec.getString();
            thisRow = nextRow;
            thisColumn = nextColumn;
            outputNextStringRecord = false;
        }
        break;

    case LabelRecord.sid:
        LabelRecord lrec = (LabelRecord) record;

        thisRow = lrec.getRow();
        thisColumn = lrec.getColumn();
        thisStr = '"' + lrec.getValue() + '"';
        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() + '"';
        }
        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 = formatListener.formatNumberDateCell(numrec);
        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;

    // 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("--> " + currentSheetName);
    }
}

From source file:com.tsm.xlstocsv.XlsToCsv.java

License:Apache License

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

    switch (record.getSid()) {
    case BoundSheetRecord.sid:
        boundSheetRecords.add(record);
        break;
    case BOFRecord.sid:
        BOFRecord br = (BOFRecord) record;
        if (br.getType() == BOFRecord.TYPE_WORKSHEET) {

            // Create sub workbook if required
            if (workbookBuildingListener != null && stubWorkbook == null) {
                stubWorkbook = workbookBuildingListener.getStubHSSFWorkbook();
            }

            // Output the worksheet name
            // Works by ordering the BSRs by the location of
            //  their BOFRecords, and then knowing that we
            //  process BOFRecords in byte offset order
            sheetIndex++;

            if (orderedBSRs == null) {
                orderedBSRs = BoundSheetRecord.orderByBofPosition(boundSheetRecords);
            }

            try {
                if (currentOutput != null && currentSheetName != null) {
                    dispatcher.closeStreamForSheet(currentOutput, currentSheetName);
                }
                currentSheetName = orderedBSRs[sheetIndex].getSheetname();
                currentOutput = dispatcher.openStreamForSheet(currentSheetName);
            } catch (FileNotFoundException e) {
                System.out.println("Aborting as attempt to write sheet failed");
                System.exit(1);
            }
        }
        break;

    case SSTRecord.sid:
        sstRecord = (SSTRecord) record;
        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) {
            if (Double.isNaN(frec.getValue())) {
                // Formula result is a string
                // This is stored in the next record
                outputNextStringRecord = true;
                nextRow = frec.getRow();
                nextColumn = frec.getColumn();
            } else {
                thisStr = formatListener.formatNumberDateCell(frec);
            }
        } else {
            thisStr = '"' + HSSFFormulaParser.toFormulaString(stubWorkbook, frec.getParsedExpression()) + '"';
        }
        break;
    case StringRecord.sid:
        if (outputNextStringRecord) {
            // String for formula
            StringRecord srec = (StringRecord) record;
            thisStr = srec.getString();
            thisRow = nextRow;
            thisColumn = nextColumn;
            outputNextStringRecord = false;
        }
        break;

    case LabelRecord.sid:
        LabelRecord lrec = (LabelRecord) record;

        thisRow = lrec.getRow();
        thisColumn = lrec.getColumn();
        thisStr = '"' + lrec.getValue() + '"';
        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() + '"';
        }
        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 = formatListener.formatNumberDateCell(numrec);
        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) {
            currentOutput.print(',');
        }
        currentOutput.print(thisStr);
    }

    // Update column and row count
    if (thisRow > -1)
        lastRowNumber = thisRow;
    if (thisColumn > -1)
        lastColumnNumber = 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++) {
                currentOutput.print(',');
            }
        }

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

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

From source file:com.ufo.util.XLS2CSV.java

License:Apache License

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

    switch (record.getSid()) {
    case BoundSheetRecord.sid:
        boundSheetRecords.add(record);
        break;
    case BOFRecord.sid:
        BOFRecord br = (BOFRecord) record;
        if (br.getType() == BOFRecord.TYPE_WORKSHEET) {
            // Create sub workbook if required
            if (workbookBuildingListener != null && stubWorkbook == null) {
                stubWorkbook = workbookBuildingListener.getStubHSSFWorkbook();
            }

            // Output the worksheet name
            // Works by ordering the BSRs by the location of
            // their BOFRecords, and then knowing that we
            // process BOFRecords in byte offset order
            sheetIndex++;
            if (orderedBSRs == null) {
                orderedBSRs = BoundSheetRecord.orderByBofPosition(boundSheetRecords);
            }
            //            output.println();
            //            output.println(orderedBSRs[sheetIndex].getSheetname() + " ["
            //                  + (sheetIndex + 1) + "]:");
        }
        break;

    case SSTRecord.sid:
        sstRecord = (SSTRecord) record;
        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) {
            if (Double.isNaN(frec.getValue())) {
                // Formula result is a string
                // This is stored in the next record
                outputNextStringRecord = true;
                nextRow = frec.getRow();
                nextColumn = frec.getColumn();
            } else {
                thisStr = formatListener.formatNumberDateCell(frec);
            }
        } else {
            thisStr = '"' + HSSFFormulaParser.toFormulaString(stubWorkbook, frec.getParsedExpression()) + '"';
        }
        break;
    case StringRecord.sid:
        if (outputNextStringRecord) {
            // String for formula
            StringRecord srec = (StringRecord) record;
            thisStr = srec.getString();
            thisRow = nextRow;
            thisColumn = nextColumn;
            outputNextStringRecord = false;
        }
        break;

    case LabelRecord.sid:
        LabelRecord lrec = (LabelRecord) record;

        thisRow = lrec.getRow();
        thisColumn = lrec.getColumn();
        thisStr = '"' + lrec.getValue() + '"';
        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() + '"';
        }
        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 = formatListener.formatNumberDateCell(numrec);
        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;

    // 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.vaadin.addon.tableexport.XLS2CSVmra.java

License:Apache License

/**
 * Main HSSFListener method, processes events, and outputs the CSV as the file is processed.
 *//*from w  w w  .ja  v  a2 s  .c om*/
@SuppressWarnings("unchecked")
@Override
public void processRecord(final Record record) {
    int thisRow = -1;
    int thisColumn = -1;
    String thisStr = null;

    switch (record.getSid()) {
    case BoundSheetRecord.sid:
        boundSheetRecords.add(record);
        break;
    case BOFRecord.sid:
        final BOFRecord br = (BOFRecord) record;
        if (br.getType() == BOFRecord.TYPE_WORKSHEET) {
            // Create sub workbook if required
            if ((workbookBuildingListener != null) && (stubWorkbook == null)) {
                stubWorkbook = workbookBuildingListener.getStubHSSFWorkbook();
            }

            // Output the worksheet name
            // Works by ordering the BSRs by the location of
            // their BOFRecords, and then knowing that we
            // process BOFRecords in byte offset order
            sheetIndex++;
            if (orderedBSRs == null) {
                orderedBSRs = BoundSheetRecord.orderByBofPosition(boundSheetRecords);
            }
            // uncomment the below if you want to output the worksheet name
            // output.println();
            // output.println(orderedBSRs[sheetIndex].getSheetname() + " [" + (sheetIndex +
            // 1)
            // + "]:");
        }
        break;

    case SSTRecord.sid:
        sstRecord = (SSTRecord) record;
        break;

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

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

        thisRow = berec.getRow();
        thisColumn = berec.getColumn();
        thisStr = "";
        break;

    case FormulaRecord.sid:
        final FormulaRecord frec = (FormulaRecord) record;

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

        if (outputFormulaValues) {
            if (Double.isNaN(frec.getValue())) {
                // Formula result is a string
                // This is stored in the next record
                outputNextStringRecord = true;
                nextRow = frec.getRow();
                nextColumn = frec.getColumn();
            } else {
                thisStr = formatListener.formatNumberDateCell(frec);
            }
        } else {
            thisStr = '"' + HSSFFormulaParser.toFormulaString(stubWorkbook, frec.getParsedExpression()) + '"';
        }
        break;
    case StringRecord.sid:
        if (outputNextStringRecord) {
            // String for formula
            final StringRecord srec = (StringRecord) record;
            thisStr = srec.getString();
            thisRow = nextRow;
            thisColumn = nextColumn;
            outputNextStringRecord = false;
        }
        break;

    case LabelRecord.sid:
        final LabelRecord lrec = (LabelRecord) record;

        thisRow = lrec.getRow();
        thisColumn = lrec.getColumn();
        thisStr = '"' + lrec.getValue() + '"';
        break;
    case LabelSSTRecord.sid:
        final 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() + '"';
        }
        break;
    case NoteRecord.sid:
        final NoteRecord nrec = (NoteRecord) record;

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

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

        // Format
        thisStr = formatListener.formatNumberDateCell(numrec);
        break;
    case RKRecord.sid:
        final 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) {
        final 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;

    // 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();
    }
}