Search in sources :

Example 1 with RKRecord

use of org.apache.poi.hssf.record.RKRecord in project jeesuite-libs by vakinge.

the class XLS2CSV method processRecord.

/**
 * Main HSSFListener method, processes events, and outputs the
 *  CSV as the file is processed.
 */
public void processRecord(Record record) {
    // 超过10行空白就不处理了
    if (blankRowNum == 10)
        return;
    int thisRow = -1;
    int thisColumn = -1;
    String thisStr = null;
    switch(record.getSid()) {
        case BoundSheetRecord.sid:
            boundSheetRecords.add((BoundSheetRecord) 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);
                }
                // sheetName
                // System.out.println(orderedBSRs[sheetIndex].getSheetname() + " [" + (sheetIndex+1) + "]:" );
                String sheetname = orderedBSRs[sheetIndex].getSheetname();
                results.add(ExcelValidator.SHEET_NAME_PREFIX + sheetname);
            }
            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 (thisStr != null) {
        if (thisColumn > 0) {
            _resultRowTmp.append(ExcelValidator.FIELD_SPLIT);
        }
        _resultRowTmp.append(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++) {
                _resultRowTmp.append(',');
            }
        }
        // We're onto a new row
        lastColumnNumber = -1;
        // End the row
        if (!ExcelValidator.isBlankCSVRow(_resultRowTmp.toString())) {
            results.add(_resultRowTmp.toString());
        } else {
            blankRowNum = blankRowNum + 1;
        }
        _resultRowTmp.setLength(0);
    }
}
Also used : BlankRecord(org.apache.poi.hssf.record.BlankRecord) BOFRecord(org.apache.poi.hssf.record.BOFRecord) LabelRecord(org.apache.poi.hssf.record.LabelRecord) LabelSSTRecord(org.apache.poi.hssf.record.LabelSSTRecord) StringRecord(org.apache.poi.hssf.record.StringRecord) BoolErrRecord(org.apache.poi.hssf.record.BoolErrRecord) FormulaRecord(org.apache.poi.hssf.record.FormulaRecord) RKRecord(org.apache.poi.hssf.record.RKRecord) LastCellOfRowDummyRecord(org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord) NoteRecord(org.apache.poi.hssf.record.NoteRecord) NumberRecord(org.apache.poi.hssf.record.NumberRecord) MissingCellDummyRecord(org.apache.poi.hssf.eventusermodel.dummyrecord.MissingCellDummyRecord)

Example 2 with RKRecord

use of org.apache.poi.hssf.record.RKRecord in project poi by apache.

the class XLS2CSVmra method processRecord.

/**
	 * Main HSSFListener method, processes events, and outputs the
	 *  CSV as the file is processed.
	 */
@Override
public void processRecord(Record record) {
    int thisRow = -1;
    int thisColumn = -1;
    String thisStr = null;
    switch(record.getSid()) {
        case BoundSheetRecord.sid:
            boundSheetRecords.add((BoundSheetRecord) 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();
    }
}
Also used : BlankRecord(org.apache.poi.hssf.record.BlankRecord) BOFRecord(org.apache.poi.hssf.record.BOFRecord) LabelRecord(org.apache.poi.hssf.record.LabelRecord) LabelSSTRecord(org.apache.poi.hssf.record.LabelSSTRecord) StringRecord(org.apache.poi.hssf.record.StringRecord) BoolErrRecord(org.apache.poi.hssf.record.BoolErrRecord) FormulaRecord(org.apache.poi.hssf.record.FormulaRecord) RKRecord(org.apache.poi.hssf.record.RKRecord) LastCellOfRowDummyRecord(org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord) NoteRecord(org.apache.poi.hssf.record.NoteRecord) NumberRecord(org.apache.poi.hssf.record.NumberRecord) MissingCellDummyRecord(org.apache.poi.hssf.eventusermodel.dummyrecord.MissingCellDummyRecord)

Example 3 with RKRecord

use of org.apache.poi.hssf.record.RKRecord in project poi by apache.

the class OldExcelExtractor method getText.

/**
     * Retrieves the text contents of the file, as best we can
     *  for these old file formats
     * 
     * @return the text contents of the file
     */
public String getText() {
    StringBuffer text = new StringBuffer();
    // To track formats and encodings
    CodepageRecord codepage = null;
    // Process each record in turn, looking for interesting ones
    while (ris.hasNextRecord()) {
        int sid = ris.getNextSid();
        ris.nextRecord();
        switch(sid) {
            case FILE_PASS_RECORD_SID:
                throw new EncryptedDocumentException("Encryption not supported for Old Excel files");
            case OldSheetRecord.sid:
                OldSheetRecord shr = new OldSheetRecord(ris);
                shr.setCodePage(codepage);
                text.append("Sheet: ");
                text.append(shr.getSheetname());
                text.append('\n');
                break;
            case OldLabelRecord.biff2_sid:
            case OldLabelRecord.biff345_sid:
                OldLabelRecord lr = new OldLabelRecord(ris);
                lr.setCodePage(codepage);
                text.append(lr.getValue());
                text.append('\n');
                break;
            case OldStringRecord.biff2_sid:
            case OldStringRecord.biff345_sid:
                OldStringRecord sr = new OldStringRecord(ris);
                sr.setCodePage(codepage);
                text.append(sr.getString());
                text.append('\n');
                break;
            case NumberRecord.sid:
                NumberRecord nr = new NumberRecord(ris);
                handleNumericCell(text, nr.getValue());
                break;
            case OldFormulaRecord.biff2_sid:
            case OldFormulaRecord.biff3_sid:
            case OldFormulaRecord.biff4_sid:
                // Biff 2 and 5+ share the same SID, due to a bug...
                if (biffVersion == 5) {
                    FormulaRecord fr = new FormulaRecord(ris);
                    if (fr.getCachedResultType() == CellType.NUMERIC.getCode()) {
                        handleNumericCell(text, fr.getValue());
                    }
                } else {
                    OldFormulaRecord fr = new OldFormulaRecord(ris);
                    if (fr.getCachedResultType() == CellType.NUMERIC.getCode()) {
                        handleNumericCell(text, fr.getValue());
                    }
                }
                break;
            case RKRecord.sid:
                RKRecord rr = new RKRecord(ris);
                handleNumericCell(text, rr.getRKNumber());
                break;
            case CodepageRecord.sid:
                codepage = new CodepageRecord(ris);
                break;
            default:
                ris.readFully(new byte[ris.remaining()]);
        }
    }
    close();
    ris = null;
    return text.toString();
}
Also used : EncryptedDocumentException(org.apache.poi.EncryptedDocumentException) OldFormulaRecord(org.apache.poi.hssf.record.OldFormulaRecord) FormulaRecord(org.apache.poi.hssf.record.FormulaRecord) OldSheetRecord(org.apache.poi.hssf.record.OldSheetRecord) OldLabelRecord(org.apache.poi.hssf.record.OldLabelRecord) RKRecord(org.apache.poi.hssf.record.RKRecord) CodepageRecord(org.apache.poi.hssf.record.CodepageRecord) OldStringRecord(org.apache.poi.hssf.record.OldStringRecord) OldFormulaRecord(org.apache.poi.hssf.record.OldFormulaRecord) NumberRecord(org.apache.poi.hssf.record.NumberRecord)

Aggregations

FormulaRecord (org.apache.poi.hssf.record.FormulaRecord)3 NumberRecord (org.apache.poi.hssf.record.NumberRecord)3 RKRecord (org.apache.poi.hssf.record.RKRecord)3 LastCellOfRowDummyRecord (org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord)2 MissingCellDummyRecord (org.apache.poi.hssf.eventusermodel.dummyrecord.MissingCellDummyRecord)2 BOFRecord (org.apache.poi.hssf.record.BOFRecord)2 BlankRecord (org.apache.poi.hssf.record.BlankRecord)2 BoolErrRecord (org.apache.poi.hssf.record.BoolErrRecord)2 LabelRecord (org.apache.poi.hssf.record.LabelRecord)2 LabelSSTRecord (org.apache.poi.hssf.record.LabelSSTRecord)2 NoteRecord (org.apache.poi.hssf.record.NoteRecord)2 StringRecord (org.apache.poi.hssf.record.StringRecord)2 EncryptedDocumentException (org.apache.poi.EncryptedDocumentException)1 CodepageRecord (org.apache.poi.hssf.record.CodepageRecord)1 OldFormulaRecord (org.apache.poi.hssf.record.OldFormulaRecord)1 OldLabelRecord (org.apache.poi.hssf.record.OldLabelRecord)1 OldSheetRecord (org.apache.poi.hssf.record.OldSheetRecord)1 OldStringRecord (org.apache.poi.hssf.record.OldStringRecord)1