Beispiel #1
0
  /** Closes this workbook, and frees makes any memory allocated available for garbage collection */
  public void close() {
    if (lastSheet != null) {
      lastSheet.clear();
    }
    excelFile.clear();

    if (!settings.getGCDisabled()) {
      System.gc();
    }
  }
Beispiel #2
0
  /**
   * Gets the specified sheet within this workbook
   *
   * @param index the zero based index of the required sheet
   * @return The sheet specified by the index
   */
  public Sheet getSheet(int index) {
    // First see if the last sheet index is the same as this sheet index.
    // If so, then the same sheet is being re-requested, so simply
    // return it instead of rereading it
    if ((lastSheet != null) && lastSheetIndex == index) {
      return lastSheet;
    }

    // Flush out all of the cached data in the last sheet
    if (lastSheet != null) {
      lastSheet.clear();

      if (!settings.getGCDisabled()) {
        System.gc();
      }
    }

    lastSheet = (SheetImpl) sheets.get(index);
    lastSheetIndex = index;
    lastSheet.readSheet();

    return lastSheet;
  }
Beispiel #3
0
  /**
   * Does the hard work of building up the object graph from the excel bytes
   *
   * @exception BiffException
   * @exception PasswordException if the workbook is password protected
   */
  protected void parse() throws BiffException, PasswordException {
    Record r = null;

    BOFRecord bof = new BOFRecord(excelFile.next());
    workbookBof = bof;
    bofs++;

    if (!bof.isBiff8() && !bof.isBiff7()) {
      throw new BiffException(BiffException.unrecognizedBiffVersion);
    }

    if (!bof.isWorkbookGlobals()) {
      throw new BiffException(BiffException.expectedGlobals);
    }
    ArrayList continueRecords = new ArrayList();
    nameTable = new ArrayList();

    // Skip to the first worksheet
    while (bofs == 1) {
      r = excelFile.next();

      if (r.getType() == Type.SST) {
        continueRecords.clear();
        Record nextrec = excelFile.peek();
        while (nextrec.getType() == Type.CONTINUE) {
          continueRecords.add(excelFile.next());
          nextrec = excelFile.peek();
        }

        // cast the array
        Object[] rec = continueRecords.toArray();
        Record[] records = new Record[rec.length];
        System.arraycopy(rec, 0, records, 0, rec.length);

        sharedStrings = new SSTRecord(r, records, settings);
      } else if (r.getType() == Type.FILEPASS) {
        throw new PasswordException();
      } else if (r.getType() == Type.NAME) {
        NameRecord nr = null;

        if (bof.isBiff8()) {
          nr = new NameRecord(r, settings, namedRecords.size());
        } else {
          nr = new NameRecord(r, settings, namedRecords.size(), NameRecord.biff7);
        }

        namedRecords.put(nr.getName(), nr);
        nameTable.add(nr);
      } else if (r.getType() == Type.FONT) {
        FontRecord fr = null;

        if (bof.isBiff8()) {
          fr = new FontRecord(r, settings);
        } else {
          fr = new FontRecord(r, settings, FontRecord.biff7);
        }
        fonts.addFont(fr);
      } else if (r.getType() == Type.PALETTE) {
        PaletteRecord palette = new PaletteRecord(r);
        formattingRecords.setPalette(palette);
      } else if (r.getType() == Type.NINETEENFOUR) {
        NineteenFourRecord nr = new NineteenFourRecord(r);
        nineteenFour = nr.is1904();
      } else if (r.getType() == Type.FORMAT) {
        FormatRecord fr = null;
        if (bof.isBiff8()) {
          fr = new FormatRecord(r, settings, FormatRecord.biff8);
        } else {
          fr = new FormatRecord(r, settings, FormatRecord.biff7);
        }
        try {
          formattingRecords.addFormat(fr);
        } catch (NumFormatRecordsException e) {
          // This should not happen.  Bomb out
          Assert.verify(false, e.getMessage());
        }
      } else if (r.getType() == Type.XF) {
        XFRecord xfr = null;
        if (bof.isBiff8()) {
          xfr = new XFRecord(r, XFRecord.biff8);
        } else {
          xfr = new XFRecord(r, XFRecord.biff7);
        }

        try {
          formattingRecords.addStyle(xfr);
        } catch (NumFormatRecordsException e) {
          // This should not happen.  Bomb out
          Assert.verify(false, e.getMessage());
        }
      } else if (r.getType() == Type.BOUNDSHEET) {
        BoundsheetRecord br = null;

        if (bof.isBiff8()) {
          br = new BoundsheetRecord(r);
        } else {
          br = new BoundsheetRecord(r, BoundsheetRecord.biff7);
        }

        if (br.isSheet() || br.isChart()) {
          boundsheets.add(br);
        }
      } else if (r.getType() == Type.EXTERNSHEET) {
        if (bof.isBiff8()) {
          externSheet = new ExternalSheetRecord(r, settings);
        } else {
          externSheet = new ExternalSheetRecord(r, settings, ExternalSheetRecord.biff7);
        }
      } else if (r.getType() == Type.CODEPAGE) {
        CodepageRecord cr = new CodepageRecord(r);
        settings.setCharacterSet(cr.getCharacterSet());
      } else if (r.getType() == Type.SUPBOOK) {
        SupbookRecord sr = new SupbookRecord(r, settings);
        supbooks.add(sr);
      } else if (r.getType() == Type.PROTECT) {
        ProtectRecord pr = new ProtectRecord(r);
        wbProtected = pr.isProtected();
      } else if (r.getType() == Type.OBJPROJ) {
        containsMacros = true;
      } else if (r.getType() == Type.MSODRAWINGGROUP) {
        msoDrawingGroup = new MsoDrawingGroupRecord(r);

        if (drawingGroup == null) {
          drawingGroup = new DrawingGroup(DrawingGroup.READ);
        }

        drawingGroup.add(msoDrawingGroup);

        Record nextrec = excelFile.peek();
        while (nextrec.getType() == Type.CONTINUE) {
          drawingGroup.add(excelFile.next());
          nextrec = excelFile.peek();
        }
      } else if (r.getType() == Type.BUTTONPROPERTYSET) {
        buttonPropertySet = new ButtonPropertySetRecord(r);
      } else if (r.getType() == Type.EOF) {
        bofs--;
      }
    }

    bof = null;
    if (excelFile.hasNext()) {
      r = excelFile.next();

      if (r.getType() == Type.BOF) {
        bof = new BOFRecord(r);
      }
    }

    // Only get sheets for which there is a corresponding Boundsheet record
    while (bof != null && getNumberOfSheets() < boundsheets.size()) {
      if (!bof.isBiff8() && !bof.isBiff7()) {
        throw new BiffException(BiffException.unrecognizedBiffVersion);
      }

      if (bof.isWorksheet()) {
        // Read the sheet in
        SheetImpl s =
            new SheetImpl(
                excelFile, sharedStrings, formattingRecords, bof, workbookBof, nineteenFour, this);

        BoundsheetRecord br = (BoundsheetRecord) boundsheets.get(getNumberOfSheets());
        s.setName(br.getName());
        s.setHidden(br.isHidden());
        addSheet(s);
      } else if (bof.isChart()) {
        // Read the sheet in
        SheetImpl s =
            new SheetImpl(
                excelFile, sharedStrings, formattingRecords, bof, workbookBof, nineteenFour, this);

        BoundsheetRecord br = (BoundsheetRecord) boundsheets.get(getNumberOfSheets());
        s.setName(br.getName());
        s.setHidden(br.isHidden());
        addSheet(s);
      } else {
        logger.warn("BOF is unrecognized");

        while (excelFile.hasNext() && r.getType() != Type.EOF) {
          r = excelFile.next();
        }
      }

      // The next record will normally be a BOF or empty padding until
      // the end of the block is reached.  In exceptionally unlucky cases,
      // the last EOF  will coincide with a block division, so we have to
      // check there is more data to retrieve.
      // Thanks to liamg for spotting this
      bof = null;
      if (excelFile.hasNext()) {
        r = excelFile.next();

        if (r.getType() == Type.BOF) {
          bof = new BOFRecord(r);
        }
      }
    }
  }