Class InternalWorkbook

java.lang.Object
org.apache.poi.hssf.model.InternalWorkbook

@Internal public final class InternalWorkbook extends Object
Low level model implementation of a Workbook. Provides creational methods for settings and objects contained in the workbook object.

This file contains the low level binary records starting at the workbook's BOF and ending with the workbook's EOF. Use HSSFWorkbook for a high level representation.

The structures of the highlevel API use references to this to perform most of their operations. Its probably unwise to use these low level structures directly unless you really know what you're doing. I recommend you read the Microsoft Excel 97 Developer's Kit (Microsoft Press) and the documentation at http://sc.openoffice.org/excelfileformat.pdf before even attempting to use this.

See Also:
  • Field Details

    • WORKBOOK_DIR_ENTRY_NAMES

      public static final String[] WORKBOOK_DIR_ENTRY_NAMES
      Normally, the Workbook will be in a POIFS Stream called "Workbook". However, some weird XLS generators use "WORKBOOK" or "BOOK".
    • OLD_WORKBOOK_DIR_ENTRY_NAME

      public static final String OLD_WORKBOOK_DIR_ENTRY_NAME
      Name of older (pre-Excel 97) Workbook streams, which aren't supported by HSSFWorkbook, only by OldExcelExtractor
      See Also:
    • sst

      protected SSTRecord sst
      this contains a reference to the SSTRecord so that new stings can be added to it.
  • Method Details

    • createWorkbook

      public static InternalWorkbook createWorkbook(List<Record> recs)
      read support for low level API. Pass in an array of Record objects, A Workbook object is constructed and passed back with all of its initialization set to the passed in records and references to those records held. Unlike Sheet workbook does not use an offset (its assumed to be 0) since its first in a file. If you need an offset then construct a new array with a 0 offset or write your own ;-p.
      Parameters:
      recs - an array of Record objects
      Returns:
      Workbook object
    • createWorkbook

      public static InternalWorkbook createWorkbook()
      Creates an empty workbook object with three blank sheets and all the empty fields. Use this to create a workbook from scratch.
      Returns:
      an empty workbook object
    • getSpecificBuiltinRecord

      public NameRecord getSpecificBuiltinRecord(byte name, int sheetNumber)
      Retrieves the Builtin NameRecord that matches the name and index There shouldn't be too many names to make the sequential search too slow
      Parameters:
      name - byte representation of the builtin name to match
      sheetNumber - 1-based sheet number
      Returns:
      null if no builtin NameRecord matches
    • removeBuiltinRecord

      public void removeBuiltinRecord(byte name, int sheetIndex)
      Removes the specified Builtin NameRecord that matches the name and index
      Parameters:
      name - byte representation of the builtin to match
      sheetIndex - zero-based sheet reference
    • getNumRecords

      public int getNumRecords()
    • getFontRecordAt

      public FontRecord getFontRecordAt(int idx)
      gets the font record at the given index in the font table. Remember "There is No Four" (someone at M$ must have gone to Rocky Horror one too many times)
      Parameters:
      idx - the index to look at (0 or greater but NOT 4)
      Returns:
      FontRecord located at the given index
    • getFontIndex

      public int getFontIndex(FontRecord font)
      Retrieves the index of the given font
      Parameters:
      font - the font
      Returns:
      the font index
      Throws:
      IllegalArgumentException - if the font index can't be determined
    • createNewFont

      public FontRecord createNewFont()
      creates a new font record and adds it to the "font table". This causes the boundsheets to move down one, extended formats to move down (so this function moves those pointers as well)
      Returns:
      FontRecord that was just created
    • removeFontRecord

      public void removeFontRecord(FontRecord rec)
      Removes the given font record from the file's list. This will make all subsequent font indicies drop by one, so you'll need to update those yourself!
      Parameters:
      rec - the font record
    • getNumberOfFontRecords

      public int getNumberOfFontRecords()
      gets the number of font records
      Returns:
      number of font records in the "font table"
    • setSheetBof

      public void setSheetBof(int sheetIndex, int pos)
      Sets the BOF for a given sheet
      Parameters:
      sheetIndex - the number of the sheet to set the positing of the bof for
      pos - the actual bof position
    • getBackupRecord

      public BackupRecord getBackupRecord()
      Returns the position of the backup record.
      Returns:
      the position of the backup record
    • setSheetName

      public void setSheetName(int sheetnum, String sheetname)
      sets the name for a given sheet. If the boundsheet record doesn't exist and its only one more than we have, go ahead and create it. If it's > 1 more than we have, except
      Parameters:
      sheetnum - the sheet number (0 based)
      sheetname - the name for the sheet
    • doesContainsSheetName

      public boolean doesContainsSheetName(String name, int excludeSheetIdx)
      Determines whether a workbook contains the provided sheet name. For the purpose of comparison, long names are truncated to 31 chars.
      Parameters:
      name - the name to test (case insensitive match)
      excludeSheetIdx - the sheet to exclude from the check or -1 to include all sheets in the check.
      Returns:
      true if the sheet contains the name, false otherwise.
    • setSheetOrder

      public void setSheetOrder(String sheetname, int pos)
      sets the order of appearance for a given sheet.
      Parameters:
      sheetname - the name of the sheet to reorder
      pos - the position that we want to insert the sheet into (0 based)
    • getSheetName

      public String getSheetName(int sheetIndex)
      gets the name for a given sheet.
      Parameters:
      sheetIndex - the sheet number (0 based)
      Returns:
      sheetname the name for the sheet
    • isSheetHidden

      public boolean isSheetHidden(int sheetnum)
      Gets the hidden flag for a given sheet. Note that a sheet could instead be set to be very hidden, which is different (isSheetVeryHidden(int))
      Parameters:
      sheetnum - the sheet number (0 based)
      Returns:
      True if sheet is hidden
    • isSheetVeryHidden

      public boolean isSheetVeryHidden(int sheetnum)
      Gets the very hidden flag for a given sheet. This is different from the normal hidden flag (isSheetHidden(int))
      Parameters:
      sheetnum - the sheet number (0 based)
      Returns:
      True if sheet is very hidden
    • getSheetVisibility

      public SheetVisibility getSheetVisibility(int sheetnum)
      Gets the hidden flag for a given sheet. Note that a sheet could instead be set to be very hidden, which is different (isSheetVeryHidden(int))
      Parameters:
      sheetnum - the sheet number (0 based)
      Returns:
      True if sheet is hidden
      Since:
      3.16 beta 2
    • setSheetHidden

      public void setSheetHidden(int sheetnum, boolean hidden)
      Hide or unhide a sheet
      Parameters:
      sheetnum - The sheet number
      hidden - True to mark the sheet as hidden, false otherwise
    • setSheetHidden

      public void setSheetHidden(int sheetnum, SheetVisibility visibility)
      Hide or unhide a sheet.
      Parameters:
      sheetnum - The sheet number
      visibility - the sheet visibility to set (visible, hidden, very hidden)
      Since:
      3.16 beta 2
    • getSheetIndex

      public int getSheetIndex(String name)
      get the sheet's index
      Parameters:
      name - sheet name
      Returns:
      sheet index or -1 if it was not found.
    • removeSheet

      public void removeSheet(int sheetIndex)
      Parameters:
      sheetIndex - zero based sheet index
    • getNumSheets

      public int getNumSheets()
      returns the number of boundsheet objects contained in this workbook.
      Returns:
      number of BoundSheet records
    • getNumExFormats

      public int getNumExFormats()
      get the number of ExtendedFormat records contained in this workbook.
      Returns:
      int count of ExtendedFormat records
    • getExFormatAt

      public ExtendedFormatRecord getExFormatAt(int index)
      gets the ExtendedFormatRecord at the given 0-based index
      Parameters:
      index - of the Extended format record (0-based)
      Returns:
      ExtendedFormatRecord at the given index
    • removeExFormatRecord

      public void removeExFormatRecord(ExtendedFormatRecord rec)
      Removes the given ExtendedFormatRecord record from the file's list. This will make all subsequent font indicies drop by one, so you'll need to update those yourself!
      Parameters:
      rec - the ExtendedFormatRecord
    • removeExFormatRecord

      public void removeExFormatRecord(int index)
      Removes ExtendedFormatRecord record with given index from the file's list. This will make all subsequent font indicies drop by one, so you'll need to update those yourself!
      Parameters:
      index - of the Extended format record (0-based)
    • createCellXF

      public ExtendedFormatRecord createCellXF()
      creates a new Cell-type Extended Format Record and adds it to the end of ExtendedFormatRecords collection
      Returns:
      ExtendedFormatRecord that was created
    • getStyleRecord

      public StyleRecord getStyleRecord(int xfIndex)
      Returns the StyleRecord for the given xfIndex, or null if that ExtendedFormat doesn't have a Style set.
      Parameters:
      xfIndex - the extended format index
      Returns:
      the StyleRecord, null if it that ExtendedFormat doesn't have a Style set.
    • updateStyleRecord

      public void updateStyleRecord(int oldXf, int newXf)
      Update the StyleRecord to point to the new given index.
      Parameters:
      oldXf - the extended format index that was previously associated with this StyleRecord
      newXf - the extended format index that is now associated with this StyleRecord
    • createStyleRecord

      public StyleRecord createStyleRecord(int xfIndex)
      Creates a new StyleRecord, for the given Extended Format index, and adds it onto the end of the records collection
      Parameters:
      xfIndex - the extended format index
      Returns:
      a new StyleRecord
    • addSSTString

      public int addSSTString(UnicodeString string)
      Adds a string to the SST table and returns its index (if its a duplicate just returns its index and update the counts) ASSUMES compressed unicode (meaning 8bit)
      Parameters:
      string - the string to be added to the SSTRecord
      Returns:
      index of the string within the SSTRecord
    • getSSTString

      public UnicodeString getSSTString(int str)
      given an index into the SST table, this function returns the corresponding String value
      Parameters:
      str - the index into the SST table
      Returns:
      String containing the SST String
    • insertSST

      public void insertSST()
      use this function to add a Shared String Table to an existing sheet (say generated by a different java api) without an sst....
      See Also:
    • serialize

      public int serialize(int offset, byte[] data)
      Serializes all records int the worksheet section into a big byte array. Use this to write the Workbook out.
      Parameters:
      offset - of the data to be written
      data - array of bytes to write this to
      Returns:
      the length of serialized bytes
    • preSerialize

      public void preSerialize()
      Perform any work necessary before the workbook is about to be serialized. Include in it ant code that modifies the workbook record stream and affects its size.
    • getSize

      public int getSize()
    • linkExternalWorkbook

      public int linkExternalWorkbook(String name, Workbook externalWorkbook)
    • findSheetFirstNameFromExternSheet

      public String findSheetFirstNameFromExternSheet(int externSheetIndex)
      Finds the first sheet name by his extern sheet index
      Parameters:
      externSheetIndex - extern sheet index
      Returns:
      first sheet name.
    • findSheetLastNameFromExternSheet

      public String findSheetLastNameFromExternSheet(int externSheetIndex)
    • getExternalSheet

      public EvaluationWorkbook.ExternalSheet getExternalSheet(int externSheetIndex)
    • getExternalName

      public EvaluationWorkbook.ExternalName getExternalName(int externSheetIndex, int externNameIndex)
    • getFirstSheetIndexFromExternSheetIndex

      public int getFirstSheetIndexFromExternSheetIndex(int externSheetNumber)
      Finds the (first) sheet index for a particular external sheet number.
      Parameters:
      externSheetNumber - The external sheet number to convert
      Returns:
      The index to the sheet found.
    • getLastSheetIndexFromExternSheetIndex

      public int getLastSheetIndexFromExternSheetIndex(int externSheetNumber)
      Finds the last sheet index for a particular external sheet number, which may be the same as the first (except for multi-sheet references)
      Parameters:
      externSheetNumber - The external sheet number to convert
      Returns:
      The index to the sheet found.
    • checkExternSheet

      public short checkExternSheet(int sheetNumber)
      Returns the extern sheet number for specific sheet number. If this sheet doesn't exist in extern sheet, add it
      Parameters:
      sheetNumber - local sheet number
      Returns:
      index to extern sheet
    • checkExternSheet

      public short checkExternSheet(int firstSheetNumber, int lastSheetNumber)
      Returns the extern sheet number for specific range of sheets. If this sheet range doesn't exist in extern sheet, add it
      Parameters:
      firstSheetNumber - first local sheet number
      lastSheetNumber - last local sheet number
      Returns:
      index to extern sheet
    • getExternalSheetIndex

      public int getExternalSheetIndex(String workbookName, String sheetName)
    • getExternalSheetIndex

      public int getExternalSheetIndex(String workbookName, String firstSheetName, String lastSheetName)
    • getNumNames

      public int getNumNames()
      gets the total number of names
      Returns:
      number of names
    • getNameRecord

      public NameRecord getNameRecord(int index)
      gets the name record
      Parameters:
      index - name index
      Returns:
      name record
    • getNameCommentRecord

      public NameCommentRecord getNameCommentRecord(NameRecord nameRecord)
      gets the name comment record
      Parameters:
      nameRecord - name record who's comment is required.
      Returns:
      name comment record or null if there isn't one for the given name.
    • createName

      public NameRecord createName()
      creates new name
      Returns:
      new name record
    • addName

      public NameRecord addName(NameRecord name)
      adds a name record
      Parameters:
      name - the name record to be added
      Returns:
      the given name record
    • createBuiltInName

      public NameRecord createBuiltInName(byte builtInName, int sheetNumber)
      Generates a NameRecord to represent a built-in region
      Parameters:
      builtInName - the built-in name
      sheetNumber - the sheet number
      Returns:
      a new NameRecord
    • removeName

      public void removeName(int nameIndex)
      removes the name
      Parameters:
      nameIndex - name index
    • updateNameCommentRecordCache

      public void updateNameCommentRecordCache(NameCommentRecord commentRecord)
      If a NameCommentRecord is added or the name it references is renamed, then this will update the lookup cache for it.
      Parameters:
      commentRecord - the comment record
    • getFormat

      public short getFormat(String format, boolean createIfNotFound)
      Returns a format index that matches the passed in format. It does not tie into HSSFDataFormat.
      Parameters:
      format - the format string
      createIfNotFound - creates a new format if format not found
      Returns:
      the format id of a format that matches or -1 if none found and createIfNotFound
    • getFormats

      public List<FormatRecord> getFormats()
      Returns the list of FormatRecords in the workbook.
      Returns:
      ArrayList of FormatRecords in the notebook
    • createFormat

      public int createFormat(String formatString)
      Creates a FormatRecord, inserts it, and returns the index code.
      Parameters:
      formatString - the format string
      Returns:
      the index code of the format record.
      See Also:
    • findFirstRecordBySid

      public Record findFirstRecordBySid(short sid)
      Returns the first occurance of a record matching a particular sid.
      Parameters:
      sid - the sid
      Returns:
      the matching record or null if it wasn't found
    • findFirstRecordLocBySid

      public int findFirstRecordLocBySid(short sid)
      Returns the index of a record matching a particular sid.
      Parameters:
      sid - The sid of the record to match
      Returns:
      The index of -1 if no match made.
    • findNextRecordBySid

      public Record findNextRecordBySid(short sid, int pos)
      Returns the next occurance of a record matching a particular sid.
      Parameters:
      sid - the sid
      pos - specifies the n-th matching sid
      Returns:
      the matching record or null if it wasn't found
    • getHyperlinks

      public List<HyperlinkRecord> getHyperlinks()
    • getRecords

      public List<Record> getRecords()
    • isUsing1904DateWindowing

      public boolean isUsing1904DateWindowing()
      Whether date windowing is based on 1/2/1904 or 1/1/1900. Some versions of Excel (Mac) can save workbooks using 1904 date windowing.
      Returns:
      true if using 1904 date windowing
    • getCustomPalette

      public PaletteRecord getCustomPalette()
      Returns the custom palette in use for this workbook; if a custom palette record does not exist, then it is created.
      Returns:
      the custom palette
    • findDrawingGroup

      public DrawingManager2 findDrawingGroup()
      Finds the primary drawing group, if one already exists
      Returns:
      the primary drawing group
    • createDrawingGroup

      public void createDrawingGroup()
      Creates a primary drawing group record. If it already exists then it's modified.
    • getWindowOne

      public WindowOneRecord getWindowOne()
    • getBSERecord

      public EscherBSERecord getBSERecord(int pictureIndex)
    • addBSERecord

      public int addBSERecord(EscherBSERecord e)
    • getDrawingManager

      public DrawingManager2 getDrawingManager()
    • getWriteProtect

      public WriteProtectRecord getWriteProtect()
    • getWriteAccess

      public WriteAccessRecord getWriteAccess()
    • getFileSharing

      public FileSharingRecord getFileSharing()
    • isWriteProtected

      public boolean isWriteProtected()
      is the workbook protected with a password (not encrypted)?
      Returns:
      true if the workbook is write protected
    • writeProtectWorkbook

      public void writeProtectWorkbook(String password, String username)
      protect a workbook with a password (not encypted, just sets writeprotect flags and the password.
      Parameters:
      password - the password
      username - the username
    • unwriteProtectWorkbook

      public void unwriteProtectWorkbook()
      removes the write protect flag
    • resolveNameXText

      public String resolveNameXText(int refIndex, int definedNameIndex)
      Parameters:
      refIndex - Index to REF entry in EXTERNSHEET record in the Link Table
      definedNameIndex - zero-based to DEFINEDNAME or EXTERNALNAME record
      Returns:
      the string representation of the defined or external name
    • getNameXPtg

      public NameXPtg getNameXPtg(String name, int sheetRefIndex, UDFFinder udf)
      Parameters:
      name - the name of an external function, typically a name of a UDF
      sheetRefIndex - the sheet ref index, or -1 if not known
      udf - locator of user-defiend functions to resolve names of VBA and Add-In functions
      Returns:
      the external name or null
    • getNameXPtg

      public NameXPtg getNameXPtg(String name, UDFFinder udf)
    • cloneDrawings

      public void cloneDrawings(InternalSheet sheet)
      Check if the cloned sheet has drawings. If yes, then allocate a new drawing group ID and re-generate shape IDs
      Parameters:
      sheet - the cloned sheet
    • cloneFilter

      public NameRecord cloneFilter(int filterDbNameIndex, int newSheetIndex)
    • updateNamesAfterCellShift

      public void updateNamesAfterCellShift(FormulaShifter shifter)
      Updates named ranges due to moving of cells
      Parameters:
      shifter - the formula shifter
    • getRecalcId

      public RecalcIdRecord getRecalcId()
      Get or create RecalcIdRecord
      Returns:
      a new RecalcIdRecord
      See Also:
    • changeExternalReference

      public boolean changeExternalReference(String oldUrl, String newUrl)
      Changes an external referenced file to another file. A formular in Excel which refers a cell in another file is saved in two parts: The referenced file is stored in an reference table. the row/cell information is saved separate. This method invokation will only change the reference in the lookup-table itself.
      Parameters:
      oldUrl - The old URL to search for and which is to be replaced
      newUrl - The URL replacement
      Returns:
      true if the oldUrl was found and replaced with newUrl. Otherwise false
    • getWorkbookRecordList

      @Internal public WorkbookRecordList getWorkbookRecordList()
      Only for internal calls - code based on this is not supported ...
      Returns:
      The list of records.