## org.apache.poi.hssf.usermodel
Class HSSFWorkbook

    java.lang.Object
      org.apache.poi.hssf.usermodel.HSSFWorkbook

public class HSSFWorkbook
extends java.lang.Object

High level representation of a workbook. This is the first object most users will construct whether they are reading or writing a workbook. It is also the top level object for creating new sheets/etc.

Version:
  2.0-pre
Author:
  Andrew C. Oliver (acoliver at apache dot org), Glen Stampoultzis (glens at apache.org), Shawn Laubach (slaubach at apache dot org)
See Also:
  `Workbook`, `HSSFSheet`

Field Summary
`static byte` `ENCODING_COMPRESSED_UNICODE`
`static byte` `ENCODING_UTF_16`
`static int` `INITIAL_CAPACITY` used for compile-time performance/memory optimization.

Constructor Summary
`HSSFWorkbook()` Creates new HSSFWorkbook from scratch (start here!)
`HSSFWorkbook(java.io.InputStream s)`
`HSSFWorkbook(java.io.InputStream s, boolean preserveNodes)` Companion to HSSFWorkbook(POIFSFileSystem), this constructs the POI filesystem around your inputstream.
`HSSFWorkbook(POIFSFileSystem fs)`
`HSSFWorkbook(POIFSFileSystem fs, boolean preserveNodes)` given a POI POIFSFileSystem object, read in its Workbook and populate the high and low level models.

Method Summary
`int` `addSSTString(java.lang.String string)`
`HSSFSheet` `cloneSheet(int sheetNum)` create an HSSFSheet from an existing sheet in the HSSFWorkbook.
`HSSFCellStyle` `createCellStyle()` create a new Cell style and add it to the workbook's style table
`HSSFDataFormat` `createDataFormat()` Returns the instance of HSSFDataFormat for this workbook.
`HSSFFont` `createFont()` create a new Font and add it to the workbook's font table
`HSSFName` `createName()` creates a new named range and add it to the model
`HSSFSheet` `createSheet()` create an HSSFSheet for this HSSFWorkbook, adds it to the sheets and returns the high level representation.
`HSSFSheet` `createSheet(java.lang.String sheetname)` create an HSSFSheet for this HSSFWorkbook, adds it to the sheets and returns the high level representation.
`HSSFFont` `findFont(short boldWeight, short color, short fontHeight, java.lang.String name, boolean italic, boolean strikeout, short typeOffset, byte underline)` Finds a font that matches the one with the supplied attributes
`boolean` `getBackupFlag()` determine whether the Excel GUI will backup the workbook when saving.
`byte[]` `getBytes()` Method getBytes - get the bytes of just the HSSF portions of the XLS file.
`HSSFCellStyle` `getCellStyleAt(short idx)` get the cell style object at the given index
`HSSFPalette` `getCustomPalette()`
`HSSFFont` `getFontAt(short idx)` get the font at the given index number
`HSSFName` `getNameAt(int index)` gets the Named range
`int` `getNameIndex(java.lang.String name)` gets the named range index by his name
`java.lang.String` `getNameName(int index)` gets the named range name
`short` `getNumberOfFonts()` get the number of fonts in the font table
`int` `getNumberOfNames()` gets the total number of named ranges in the workboko
`int` `getNumberOfSheets()` get the number of spreadsheets in the workbook (this will be three after serialization)
`short` `getNumCellStyles()` get the number of styles the workbook contains
`java.lang.String` `getPrintArea(int sheetIndex)` Retrieves the reference for the printarea of the specified sheet, the sheet name is appended to the reference even if it was not specified.
`HSSFSheet` `getSheet(java.lang.String name)` Get sheet with the given name
`HSSFSheet` `getSheetAt(int index)` Get the HSSFSheet object at the given index.
`int` `getSheetIndex(java.lang.String name)` Returns the index of the sheet by his name
`java.lang.String` `getSheetName(int sheet)` get the sheet name
`java.lang.String` `getSSTString(int index)`
`void` `insertChartRecord()` Test only.
`void` `removeName(int index)` remove the named range by his index
`void` `removeName(java.lang.String name)` remove the named range by his name
`void` `removePrintArea(int sheetIndex)` Delete the printarea for the sheet specified
`void` `removeSheetAt(int index)` removes sheet at the given index
`void` `setBackupFlag(boolean backupValue)` determine whether the Excel GUI will backup the workbook when saving.
`void` `setPrintArea(int sheetIndex, int startColumn, int endColumn, int startRow, int endRow)` For the Convenience of Java Programmers maintaining pointers.
`void` `setPrintArea(int sheetIndex, java.lang.String reference)` Sets the printarea for the sheet provided
`void` `setRepeatingRowsAndColumns(int sheetIndex, int startColumn, int endColumn, int startRow, int endRow)` Sets the repeating rows and columns for a sheet (as found in File->PageSetup->Sheet).
`void` `setSheetName(int sheet, java.lang.String name)` set the sheet name.
`void` `setSheetName(int sheet, java.lang.String name, short encoding)`
`void` `setSheetOrder(java.lang.String sheetname, int pos)` sets the order of appearance for a given sheet.
`void` `write(java.io.OutputStream stream)` Method write - write out this workbook to an Outputstream.

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Field Detail

### INITIAL_CAPACITY

    public static final int INITIAL_CAPACITY

  used for compile-time performance/memory optimization. This determines the initial capacity for the sheet collection. Its currently set to 3. Changing it in this release will decrease performance since you're never allowed to have more or less than three sheets!

  See Also:
    Constant Field Values

### ENCODING_COMPRESSED_UNICODE

    public static final byte ENCODING_COMPRESSED_UNICODE

See Also:
  Constant Field Values

### ENCODING_UTF_16

    public static final byte ENCODING_UTF_16

See Also:
  Constant Field Values

Constructor Detail

### HSSFWorkbook

    public HSSFWorkbook()

  Creates new HSSFWorkbook from scratch (start here!)

### HSSFWorkbook

    public HSSFWorkbook(POIFSFileSystem fs)
                 throws java.io.IOException

### HSSFWorkbook

    public HSSFWorkbook(POIFSFileSystem fs,
                        boolean preserveNodes)
                 throws java.io.IOException

  given a POI POIFSFileSystem object, read in its Workbook and populate the high and low level models. If you're reading in a workbook...start here.

Parameters:
  `fs` - the POI filesystem that contains the Workbook stream.
  `preserveNodes` - whether to preseve other nodes, such as macros. This takes more memory, so only say yes if you need to.
Throws:
  `java.io.IOException` - if the stream cannot be read
See Also:
  `POIFSFileSystem`

### HSSFWorkbook

    public HSSFWorkbook(java.io.InputStream s)
                 throws java.io.IOException

### HSSFWorkbook

    public HSSFWorkbook(java.io.InputStream s,
                        boolean preserveNodes)
                 throws java.io.IOException

  Companion to HSSFWorkbook(POIFSFileSystem), this constructs the POI filesystem around your inputstream.

Parameters:
  `s` - the POI filesystem that contains the Workbook stream.
  `preserveNodes` - whether to preseve other nodes, such as macros. This takes more memory, so only say yes if you need to.
Throws:
  `java.io.IOException` - if the stream cannot be read
See Also:
  `POIFSFileSystem`, `HSSFWorkbook(POIFSFileSystem)`

Method Detail

### setSheetOrder

    public void setSheetOrder(java.lang.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)

### setSheetName

    public void setSheetName(int sheet,
                             java.lang.String name)

  set the sheet name. Will throw IllegalArgumentException if the name is greater than 31 chars or contains /\?*[]

  Parameters:
    `sheet` - number (0 based)

### setSheetName

    public void setSheetName(int sheet,
                             java.lang.String name,
                             short encoding)

### getSheetName

    public java.lang.String getSheetName(int sheet)

  get the sheet name

  Parameters:
    `sheet` - Number
  Returns:
    Sheet name

### getSheetIndex

    public int getSheetIndex(java.lang.String name)

  Returns the index of the sheet by his name

  Parameters:
    `name` - the sheet name
  Returns:
    index of the sheet (0 based)

### createSheet

    public HSSFSheet createSheet()

  create an HSSFSheet for this HSSFWorkbook, adds it to the sheets and returns the high level representation. Use this to create new sheets.

  Returns:
    HSSFSheet representing the new sheet.

### cloneSheet

    public HSSFSheet cloneSheet(int sheetNum)

  create an HSSFSheet from an existing sheet in the HSSFWorkbook.

  Returns:
    HSSFSheet representing the cloned sheet.

### createSheet

    public HSSFSheet createSheet(java.lang.String sheetname)

  create an HSSFSheet for this HSSFWorkbook, adds it to the sheets and returns the high level representation. Use this to create new sheets.

  Parameters:
    `sheetname` - sheetname to set for the sheet.
  Returns:
    HSSFSheet representing the new sheet.

### getNumberOfSheets

    public int getNumberOfSheets()

  get the number of spreadsheets in the workbook (this will be three after serialization)

  Returns:
    number of sheets

### getSheetAt

    public HSSFSheet getSheetAt(int index)

  Get the HSSFSheet object at the given index.

  Parameters:
    `index` - of the sheet number (0-based physical & logical)
  Returns:
    HSSFSheet at the provided index

### getSheet

    public HSSFSheet getSheet(java.lang.String name)

  Get sheet with the given name

  Parameters:
    `name` - of the sheet
  Returns:
    HSSFSheet with the name provided or null if it does not exist

### removeSheetAt

    public void removeSheetAt(int index)

  removes sheet at the given index

  Parameters:
    `index` - of the sheet (0-based)

### setBackupFlag

    public void setBackupFlag(boolean backupValue)

  determine whether the Excel GUI will backup the workbook when saving.

  Parameters:
    `backupValue` - true to indicate a backup will be performed.

### getBackupFlag

    public boolean getBackupFlag()

  determine whether the Excel GUI will backup the workbook when saving.

  Returns:
    the current setting for backups.

### setRepeatingRowsAndColumns

    public void setRepeatingRowsAndColumns(int sheetIndex,
                                           int startColumn,
                                           int endColumn,
                                           int startRow,
                                           int endRow)

  Sets the repeating rows and columns for a sheet (as found in File->PageSetup->Sheet). This is function is included in the workbook because it creates/modifies name records which are stored at the workbook level.

  To set just repeating columns:

        workbook.setRepeatingRowsAndColumns(0,0,1,-1-1);

  To set just repeating rows:

        workbook.setRepeatingRowsAndColumns(0,-1,-1,0,4);

  To remove all repeating rows and columns for a sheet.

        workbook.setRepeatingRowsAndColumns(0,-1,-1,-1,-1);

  Parameters:
    `sheetIndex` - 0 based index to sheet.
    `startColumn` - 0 based start of repeating columns.
    `endColumn` - 0 based end of repeating columns.
    `startRow` - 0 based start of repeating rows.
    `endRow` - 0 based end of repeating rows.

### createFont

    public HSSFFont createFont()

  create a new Font and add it to the workbook's font table

  Returns:
    new font object

### findFont

    public HSSFFont findFont(short boldWeight,
                             short color,
                             short fontHeight,
                             java.lang.String name,
                             boolean italic,
                             boolean strikeout,
                             short typeOffset,
                             byte underline)

  Finds a font that matches the one with the supplied attributes

### getNumberOfFonts

    public short getNumberOfFonts()

  get the number of fonts in the font table

  Returns:
    number of fonts

### getFontAt

    public HSSFFont getFontAt(short idx)

  get the font at the given index number

  Parameters:
    `idx` - index number
  Returns:
    HSSFFont at the index

### createCellStyle

    public HSSFCellStyle createCellStyle()

  create a new Cell style and add it to the workbook's style table

  Returns:
    the new Cell Style object

### getNumCellStyles

    public short getNumCellStyles()

  get the number of styles the workbook contains

  Returns:
    count of cell styles

### getCellStyleAt

    public HSSFCellStyle getCellStyleAt(short idx)

  get the cell style object at the given index

  Parameters:
    `idx` - index within the set of styles
  Returns:
    HSSFCellStyle object at the index

### write

    public void write(java.io.OutputStream stream)
               throws java.io.IOException

  Method write - write out this workbook to an Outputstream. Constructs a new POI POIFSFileSystem, passes in the workbook binary representation and writes it out.

  Parameters:
    `stream` - - the java OutputStream you wish to write the XLS to
  Throws:
    `java.io.IOException` - if anything can't be written.
  See Also:
    `POIFSFileSystem`

### getBytes

    public byte[] getBytes()

  Method getBytes - get the bytes of just the HSSF portions of the XLS file. Use this to construct a POI POIFSFileSystem yourself.

  Returns:
    byte[] array containing the binary representation of this workbook and all contained sheets, rows, cells, etc.
  See Also:
    `Workbook`, `Sheet`

### addSSTString

    public int addSSTString(java.lang.String string)

### getSSTString

    public java.lang.String getSSTString(int index)

### getNumberOfNames

    public int getNumberOfNames()

  gets the total number of named ranges in the workboko

  Returns:
    number of named ranges

### getNameAt

    public HSSFName getNameAt(int index)

  gets the Named range

  Parameters:
    `index` - position of the named range
  Returns:
    named range high level

### getNameName

    public java.lang.String getNameName(int index)

  gets the named range name

  Parameters:
    `index` - the named range index (0 based)
  Returns:
    named range name

### setPrintArea

    public void setPrintArea(int sheetIndex,
                             java.lang.String reference)

  Sets the printarea for the sheet provided

  i.e. Reference = $A$1:$B$2

  Parameters:
    `sheetIndex` - Zero-based sheet index (0 Represents the first sheet to keep consistent with java)
    `reference` - Valid name Reference for the Print Area

### setPrintArea

    public void setPrintArea(int sheetIndex,
                             int startColumn,
                             int endColumn,
                             int startRow,
                             int endRow)

  For the Convenience of Java Programmers maintaining pointers.

  Parameters:
    `sheetIndex` - Zero-based sheet index (0 = First Sheet)
    `startColumn` - Column to begin printarea
    `endColumn` - Column to end the printarea
    `startRow` - Row to begin the printarea
    `endRow` - Row to end the printarea
  See Also:
    `setPrintArea(int, String)`

### getPrintArea

    public java.lang.String getPrintArea(int sheetIndex)

  Retrieves the reference for the printarea of the specified sheet, the sheet name is appended to the reference even if it was not specified.

  Parameters:
    `sheetIndex` - Zero-based sheet index (0 Represents the first sheet to keep consistent with java)
  Returns:
    String Null if no print area has been defined

### removePrintArea

    public void removePrintArea(int sheetIndex)

  Delete the printarea for the sheet specified

  Parameters:
    `sheetIndex` - Zero-based sheet index (0 = First Sheet)

### createName

    public HSSFName createName()

  creates a new named range and add it to the model

  Returns:
    named range high level

### getNameIndex

    public int getNameIndex(java.lang.String name)

  gets the named range index by his name

  Parameters:
    `name` - named range name
  Returns:
    named range index

### removeName

    public void removeName(int index)

  remove the named range by his index

  Parameters:
    `index` - named range index (0 based)

### createDataFormat

    public HSSFDataFormat createDataFormat()

  Returns the instance of HSSFDataFormat for this workbook.

  Returns:
    the HSSFDataFormat object
  See Also:
    `FormatRecord`, `Record`

### removeName

    public void removeName(java.lang.String name)

  remove the named range by his name

  Parameters:
    `name` - named range name

### getCustomPalette

    public HSSFPalette getCustomPalette()

### insertChartRecord

    public void insertChartRecord()

  Test only. Do not use

Copyright © 2003 Apache Software Foundation.