Search in sources :

Example 1 with OldStringRecord

use of org.apache.poi.hssf.record.OldStringRecord 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

EncryptedDocumentException (org.apache.poi.EncryptedDocumentException)1 CodepageRecord (org.apache.poi.hssf.record.CodepageRecord)1 FormulaRecord (org.apache.poi.hssf.record.FormulaRecord)1 NumberRecord (org.apache.poi.hssf.record.NumberRecord)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 RKRecord (org.apache.poi.hssf.record.RKRecord)1