aspose.cells
Class Workbook

Represents a root object to create an Excel spreadsheet. The Workbook class denotes an Excel spreadsheet. Each spreadsheet can contain multiple worksheets. The basic feature of the class is to open and save native excel files. The class has some advanced features like copying data from other Workbooks, combining two Workbooks and protecting the Excel spreadsheet.

Example:

The following example creates a Workbook, opens a file named designer.xls in it and makes the horizontal and vertical scroll bars invisible for the Workbook. It then replaces two string values with an Integer value and string value respectively within the spreadsheet and finally save it to file named result.xls.
$designerFile = "book1.xls";
$workbook = new cells\Workbook($designerFile);

//Set scroll bars
$workbook->getSettings()->setHScrollBarVisible(false);
$workbook->getSettings()->setVScrollBarVisible(false);

//Replace the placeholder string with new values
$workbook->replace("OldInt", 100);

$newString = "Hello!";
$workbook->replace("OldString", newString);
$saveOptions = new cells\XlsSaveOptions();
$workbook->save("res.xls", $saveOptions);

Constructor Summary
Workbook()
           Initializes a new instance of the Workbook class.
Workbook(fileFormatType)
           Initializes a new instance of the Workbook class.
Workbook(file)
           Initializes a new instance of the Workbook class and open a file.
Workbook(file, loadOptions)
           Initializes a new instance of the Workbook class and open a file.
 
Property Getters/Setters Summary
functiongetAbsolutePath()
functionsetAbsolutePath(value)
           Gets and sets the absolute path of the file.
functiongetBuiltInDocumentProperties()
           Returns a DocumentProperties collection that represents all the built-in document properties of the spreadsheet.
functiongetColors()
           Returns colors in the palette for the spreadsheet.
functiongetContentTypeProperties()
           Gets the contenttypeproperties objects in the workbook.
functiongetCountOfStylesInPool()
           Gets number of the styles in the style pool.
functiongetCustomDocumentProperties()
           Returns a DocumentProperties collection that represents all the custom document properties of the spreadsheet.
functiongetCustomXmlParts()
           Represents a InnerCustom XML Data Storage Part (custom XML data within a package).
functiongetDataConnections()
           Gets the Aspose.Cells.ExternalConnections.ExternalConnection collection.
functiongetDataSorter()
           Gets a DataSorter object to sort data.
functiongetDefaultStyle()
functionsetDefaultStyle(value)
           Gets or sets the default Style object of the workbook.
functiongetFileFormat()
functionsetFileFormat(value)
           Gets and sets the file format. The value of the property is FileFormatType integer constant.
functiongetFileName()
functionsetFileName(value)
           Gets and sets the current file name.
functionhasMacro()
           Indicates if this spreadsheet contains macro/VBA.
functionhasRevisions()
           Gets if the workbook has any tracked changes
functiongetInterruptMonitor()
functionsetInterruptMonitor(value)
           Gets and sets the interrupt monitor.
functionisDigitallySigned()
           Indicates if this spreadsheet is digitally signed.
functionisLicensed()
           Indicates whether license is set.
functiongetRibbonXml()
functionsetRibbonXml(value)
           Gets and sets the XML file that defines the Ribbon UI.
functiongetSettings()
           Represents the workbook settings.
functiongetTheme()
           Gets the theme name.
functiongetVbaProject()
           Gets the VbaProject in a spreadsheet.
functiongetWorksheets()
           Gets the WorksheetCollection collection in the spreadsheet.
 
Method Summary
functionacceptAllRevisions()
           Accepts all tracked changes in the workbook.
functionaddDigitalSignature(digitalSignatureCollection)
           Adds digital signature to an OOXML spreadsheet file (Excel2007 and later).
functioncalculateFormula()
           Calculates the result of formulas.
functioncalculateFormula(ignoreError)
           Calculates the result of formulas.
functioncalculateFormula(ignoreError, customFunction)
           Calculates the result of formulas.
functioncalculateFormula(options)
           Calcualting formulas in this workbook.
functionchangePalette(color, index)
           Changes the palette for the spreadsheet in the specified index.
functioncombine(secondWorkbook)
           Combines another Workbook object.
functioncopy(source0)
           Copies data from a source Workbook object.
functioncopyTheme(source)
           Copies the theme from another workbook.
functioncreateBuiltinStyle(type)
           Creates built-in style by given type.
functioncreateCellsColor()
           Creates a CellsColor object.
functioncreateStyle()
           Creates a new style.
functioncustomTheme(themeName, colors)
           Customs the theme.
functiondispose()
           Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
functionexportXml(mapName, path)
           Export XML data.
functiongetDigitalSignature()
           Gets digital signature from file.
functiongetFonts()
           Gets all fonts in the style pool.
functiongetMatchingColor(rawColor)
           Find best matching Color in current palette.
functiongetNamedStyle(name)
           Gets the named style in the style pool.
functiongetStyleInPool(index)
           Gets the style in the style pool. All styles in the workbook will be gathered into a pool. There is only a simple reference index in the cells.
functiongetThemeColor(type)
           Gets theme color.
functionhasExernalLinks()
           Indicates whether this workbook contains external links to other data sources.
functionimportXml(url, sheetName, row, col)
           Imports an xml file into the workbook.
functionisColorInPalette(color)
           Checks if a color is in the palette for the spreadsheet.
functionprotect(protectionType, password)
           Protects a workbook.
functionprotectSharedWorkbook(password)
           Protects a shared workbook.
functionremoveDigitalSignature()
           Removes digital signature from this spreadsheet.
functionremoveExternalLinks()
           Removes all external links in the workbook.
functionremoveMacro()
           Removes VBA/macro from this spreadsheet.
functionremoveUnusedStyles()
           Remove all unused styles.
functionreplace(placeHolder, newValue)
           Replaces a cell's value with a new double.
functionreplace(placeHolder, newValue)
           Replaces a cell's value with a new integer.
functionreplace(placeHolder, newValue)
           Replaces a cell's value with a new string.
functionreplace(placeHolder, newValue, options)
           Replaces a cell's value with a new string.
functionreplace(placeHolder, newValues, isVertical)
           Replaces a cell's value with a new string array.
functionsave(fileName)
          
functionsave(fileName, saveOptions)
           Saves the workbook to the disk.
functionsave(fileName, saveFormat)
           Saves the workbook to the disk.
functionsetDigitalSignature(digitalSignatureCollection)
           Sets digital signature to an spreadsheet file (Excel2007 and later).
functionsetEncryptionOptions(encryptionType, keyLength)
           Set Encryption Options.
functionsetThemeColor(type, color)
           Sets the theme color
functionunprotect(password)
           Unprotects a workbook.
functionunprotectSharedWorkbook(password)
           Unprotects a shared workbook.
functionupdateLinkedDataSource(exteralWorkbooks)
           If this workbook contains external links to other data source, Aspose.Cells will attempt to retrieve the latest data.
 

Constructor Detail

Workbook

function Workbook()
Initializes a new instance of the Workbook class. The default file format type is Excel97To2003.If want create other format file type, please call Workbook(FileFormatType fileFormatType).

Workbook

function Workbook(fileFormatType)
Initializes a new instance of the Workbook class. The default file format type is Excel97To2003.
Parameters:
fileFormatType: Number - A FileFormatType value. The new file format.

Example:

The following code shows how to use the Workbook constructor to create and initialize a new instance of the class.
$workbook = new cells\Workbook(cells\FileFormatType::XLSX);

Workbook

function Workbook(file)
Initializes a new instance of the Workbook class and open a file.
Parameters:
file: String - The file name.

Workbook

function Workbook(file, loadOptions)
Initializes a new instance of the Workbook class and open a file.
Parameters:
file: String - The file name.
loadOptions: LoadOptions - The load options

Property Getters/Setters Detail

getSettings : WorkbookSettings 

function getSettings()
Represents the workbook settings.

getWorksheets : WorksheetCollection 

function getWorksheets()
Gets the WorksheetCollection collection in the spreadsheet.
Returns:
WorksheetCollection collection

isLicensed : boolean 

function isLicensed()
Indicates whether license is set.

getColors : Color[] 

function getColors()
Returns colors in the palette for the spreadsheet. The palette has 56 entries, each represented by an RGB value.

getCountOfStylesInPool : Number 

function getCountOfStylesInPool()
Gets number of the styles in the style pool.

getDefaultStyle/setDefaultStyle : Style 

function getDefaultStyle() / function setDefaultStyle(value)
Gets or sets the default Style object of the workbook. The DefaultStyle property is useful to implement a Style for the whole Workbook.

Example:

The following code creates and instantiates a new Workbook and sets a default Style to it.
$workbook = new cells\Workbook();
$defaultStyle = $workbook->getDefaultStyle();
$defaultStyle->getFont()->setName("Tahoma");
$workbook->setDefaultStyle($defaultStyle);

isDigitallySigned : boolean 

function isDigitallySigned()
Indicates if this spreadsheet is digitally signed.

getVbaProject : VbaProject 

function getVbaProject()
Gets the VbaProject in a spreadsheet.

hasMacro : boolean 

function hasMacro()
Indicates if this spreadsheet contains macro/VBA.

hasRevisions : boolean 

function hasRevisions()
Gets if the workbook has any tracked changes

getFileName/setFileName : String 

function getFileName() / function setFileName(value)
Gets and sets the current file name. If the file is opened by stream and there are some external formula references, please set the file name.

getDataSorter : DataSorter 

function getDataSorter()
Gets a DataSorter object to sort data.

getTheme : String 

function getTheme()
Gets the theme name.

getBuiltInDocumentProperties : BuiltInDocumentPropertyCollection 

function getBuiltInDocumentProperties()
Returns a DocumentProperties collection that represents all the built-in document properties of the spreadsheet. A new property cannot be added to built-in document properties list. You can only get a built-in property and change its value. The following is the built-in properties name list:

Title

Subject

Author

Keywords

Comments

Template

Last Author

Revision Number

Application Name

Last Print Date

Creation Date

Last Save Time

Total Editing Time

Number of Pages

Number of Words

Number of Characters

Security

Category

Format

Manager

Company

Number of Bytes

Number of Lines

Number of Paragraphs

Number of Slides

Number of Notes

Number of Hidden Slides

Number of Multimedia Clips

Example:

$workbook = new cells\Workbook();
$doc = $workbook->getBuiltInDocumentProperties()->get("Author");
$doc->setValue("John Smith");

getCustomDocumentProperties : CustomDocumentPropertyCollection 

function getCustomDocumentProperties()
Returns a DocumentProperties collection that represents all the custom document properties of the spreadsheet.

Example:

$workbook = new cells\Workbook();
$workbook->getCustomDocumentProperties()->add("Checked by", "Jane");

getFileFormat/setFileFormat : Number 

function getFileFormat() / function setFileFormat(value)
Gets and sets the file format. The value of the property is FileFormatType integer constant.

getInterruptMonitor/setInterruptMonitor : InterruptMonitor 

function getInterruptMonitor() / function setInterruptMonitor(value)
Gets and sets the interrupt monitor.

getContentTypeProperties : ContentTypePropertyCollection 

function getContentTypeProperties()
Gets the contenttypeproperties objects in the workbook.

getCustomXmlParts : CustomXmlPartCollection 

function getCustomXmlParts()
Represents a InnerCustom XML Data Storage Part (custom XML data within a package).

getRibbonXml/setRibbonXml : String 

function getRibbonXml() / function setRibbonXml(value)
Gets and sets the XML file that defines the Ribbon UI.

getAbsolutePath/setAbsolutePath : String 

function getAbsolutePath() / function setAbsolutePath(value)
Gets and sets the absolute path of the file. Only used for external links.

getDataConnections : ExternalConnectionCollection 

function getDataConnections()
Gets the Aspose.Cells.ExternalConnections.ExternalConnection collection.

Method Detail

save

function save(fileName, saveFormat)
Saves the workbook to the disk.
Parameters:
fileName: String - The file name.
saveFormat: Number - A SaveFormat value. The save format type.

Example:

$workbook = new cells\Workbook();
$sheets = $workbook->getWorksheets();
$cells = $sheets->get(0)->getCells();
$cells->get("A1")->putValue("Hello world!");
$workbook->save("res.xls", cells\SaveFormat::EXCEL_97_TO_2003);

save

function save(fileName)
Parameters:
fileName: String -

save

function save(fileName, saveOptions)
Saves the workbook to the disk.
Parameters:
fileName: String - The file name.
saveOptions: SaveOptions - The save options.

removeUnusedStyles

function removeUnusedStyles()
Remove all unused styles.

createStyle

function createStyle()
Creates a new style.
Returns:
Returns a style object.

createBuiltinStyle

function createBuiltinStyle(type)
Creates built-in style by given type.
Parameters:
type: Number - A BuiltinStyleType value.
Returns:
style object

createCellsColor

function createCellsColor()
Creates a CellsColor object.
Returns:
Returns a CellsColor object.

replace

function replace(placeHolder, newValue)
Replaces a cell's value with a new string.
Parameters:
placeHolder: String - Cell placeholder
newValue: String - String value to replace

Example:

$workbook = new cells\Workbook();
$cells = $workbook->getWorksheets()->get(0)->getCells();
$cells->get(0, 0)->putValue("AnOldValue");
$cells->get(0, 1)->putValue("AnotherOldValue");
$workbook->replace("AnOldValue", "NewValue");

replace

function replace(placeHolder, newValue)
Replaces a cell's value with a new integer.
Parameters:
placeHolder: String - Cell placeholder
newValue: Number - Integer value to replace

Example:

$workbook = new cells\Workbook();
$cells = $workbook->getWorksheets()->get(0)->getCells();
$cells->get(0, 0)->putValue("AnOldValue");
$cells->get(0, 1)->putValue("AnotherOldValue");
$newValue = 100;
$workbook->replace("AnOldValue", $newValue);

replace

function replace(placeHolder, newValue)
Replaces a cell's value with a new double.
Parameters:
placeHolder: String - Cell placeholder
newValue: Number - Double value to replace

Example:

$workbook = new cells\Workbook();
$cells = $workbook->getWorksheets()->get(0)->getCells();
$cells->get(0, 0)->putValue("AnOldValue");
$cells->get(0, 1)->putValue("AnotherOldValue");
$newValue = 100.1;
$workbook->replace("AnOldValue", $newValue);

replace

function replace(placeHolder, newValues, isVertical)
Replaces a cell's value with a new string array.
Parameters:
placeHolder: String - Cell placeholder
newValues: String[] - String array to replace
isVertical: boolean - True - Vertical, False - Horizontal

Example:

$workbook = new cells\Workbook();
$cells = $workbook->getWorksheets()->get(0)->getCells();
$cells->get(0, 0)->putValue("AnOldValue");
$cells->get(0, 1)->putValue("AnotherOldValue");
$ExampleUtil = new Java("ExampleUtil");
$newValues = ["Tom", "Alice", "Jerry"];
$workbook->replace("AnOldValue", $newValues, true);

replace

function replace(placeHolder, newValue, options)
Replaces a cell's value with a new string.
Parameters:
placeHolder: String - Cell placeholder
newValue: String - String value to replace
options: ReplaceOptions - The replace options

copy

function copy(source0)
Copies data from a source Workbook object.
Parameters:
source0: Workbook - Source Workbook object.

combine

function combine(secondWorkbook)
Combines another Workbook object. Currently, only cell data and cell style of the second Workbook object can be combined. Images, charts and other drawing objects are not supported.
Parameters:
secondWorkbook: Workbook - Another Workbook object.

getStyleInPool

function getStyleInPool(index)
Gets the style in the style pool. All styles in the workbook will be gathered into a pool. There is only a simple reference index in the cells. If the returned style is changed, the style of all cells(which refers to this style) will be changed.
Parameters:
index: Number - The index.
Returns:
The style in the pool corresponds to given index, may be null.

getFonts

function getFonts()
Gets all fonts in the style pool.

getNamedStyle

function getNamedStyle(name)
Gets the named style in the style pool.
Parameters:
name: String - name of the style
Returns:
named style, maybe null.

changePalette

function changePalette(color, index)
Changes the palette for the spreadsheet in the specified index. The palette has 56 entries, each represented by an RGB value. If you set a color which is not in the palette, it will not take effect. So if you want to set a custom color, please change the palette at first.

The following is the standard color palette.

Color Red Green Blue 
Black 0 0 0 
White 255 255 255 
Red 255 0 0 
Lime 0 255 0 
Blue 0 0 255 
Yellow 255 255 0 
Magenta 255 0 255 
Cyan 0 255 255 
Maroon 128 0 0 
Green 0 128 0 
Navy 0 0 128 
Olive 128 128 0 
Purple 128 0 128 
Teal 0 128 128 
Silver 192 192 192 
Gray 128 128 128 
Color17 153 153 255 
Color18 153 51 102 
Color19 255 255 204 
Color20 204 255 255 
Color21 102 0 102 
Color22 255 128 128 
Color23 0 102 204 
Color24 204 204 255 
Color25 0 0 128 
Color26 255 0 255 
Color27 255 255 0 
Color28 0 255 255 
Color29 128 0 128 
Color30 128 0 0 
Color31 0 128 128 
Color32 0 0 255 
Color33 0 204 255 
Color34 204 255 255 
Color35 204 255 204 
Color36 255 255 153 
Color37 153 204 255 
Color38 255 153 204 
Color39 204 153 255 
Color40 255 204 153 
Color41 51 102 255 
Color42 51 204 204 
Color43 153 204 0 
Color44 255 204 0 
Color45 255 153 0 
Color46 255 102 0 
Color47 102 102 153 
Color48 150 150 150 
Color49 0 51 102 
Color50 51 153 102 
Color51 0 51 0 
Color52 51 51 0 
Color53 153 51 0 
Color54 153 51 102 
Color55 51 51 153 
Color56 51 51 51 
Parameters:
color: Color - Color structure.
index: Number - Palette index, 0 - 55.

isColorInPalette

function isColorInPalette(color)
Checks if a color is in the palette for the spreadsheet.
Parameters:
color: Color - Color structure.
Returns:
Returns true if this color is in the palette. Otherwise, returns false

calculateFormula

function calculateFormula()
Calculates the result of formulas.

Now Workbook built-in functions are not supported in this method:

[A]

ASC

[B]

BAHTTEXT

[C]

CALL, CLEAN, CODE, CONVERT, CUBEKPIMEMBER, CUBEMEMBER, CUBEMEMBERPROPERTY, CUBERANKEDMEMBER, CUBESET, CUBESETCOUNT, CUBEVALUE

[E]

EUROCONVERT

[I]

INFO

[J]

JIS

[P]

PHONETIC

[R]

REGISTER.ID, RTD

[S]

SQL.REQUEST


calculateFormula

function calculateFormula(ignoreError)
Calculates the result of formulas.
Parameters:
ignoreError: boolean - Indicates if hide the error in calculating formulas. The error may be unsupported function, external links, etc.

calculateFormula

function calculateFormula(ignoreError, customFunction)
Calculates the result of formulas.
Parameters:
ignoreError: boolean - Indicates if hide the error in calculating formulas. The error may be unsupported function, external links, etc.
customFunction: ICustomFunction - The custom formula calculation functions to extend the calculation engine.

calculateFormula

function calculateFormula(options)
Calcualting formulas in this workbook.
Parameters:
options: CalculationOptions - Options for calculation

getMatchingColor

function getMatchingColor(rawColor)
Find best matching Color in current palette.
Parameters:
rawColor: Color - Raw color.
Returns:
Best matching color.

setEncryptionOptions

function setEncryptionOptions(encryptionType, keyLength)
Set Encryption Options.
Parameters:
encryptionType: Number - A EncryptionType value. The encryption type.
keyLength: Number - The key length.

protect

function protect(protectionType, password)
Protects a workbook.
Parameters:
protectionType: Number - A ProtectionType value. Protection type.
password: String - Password to protect the workbook.

protectSharedWorkbook

function protectSharedWorkbook(password)
Protects a shared workbook.
Parameters:
password: String - Password to protect the workbook.

unprotect

function unprotect(password)
Unprotects a workbook.
Parameters:
password: String - Password to unprotect the workbook.

unprotectSharedWorkbook

function unprotectSharedWorkbook(password)
Unprotects a shared workbook.
Parameters:
password: String - Password to unprotect the workbook.

removeMacro

function removeMacro()
Removes VBA/macro from this spreadsheet.

removeDigitalSignature

function removeDigitalSignature()
Removes digital signature from this spreadsheet.

acceptAllRevisions

function acceptAllRevisions()
Accepts all tracked changes in the workbook.

removeExternalLinks

function removeExternalLinks()
Removes all external links in the workbook.

getThemeColor

function getThemeColor(type)
Gets theme color.
Parameters:
type: Number - A ThemeColorType value. The theme color type.
Returns:
The theme color.

setThemeColor

function setThemeColor(type, color)
Sets the theme color
Parameters:
type: Number - A ThemeColorType value. The theme color type.
color: Color - the theme color

customTheme

function customTheme(themeName, colors)
Customs the theme. The length of colors should be 12.
Array index Theme type 
0 Backgournd1 
1 Text1 
2 Backgournd2 
3 Text2 
4 Accent1 
5 Accent2 
6 Accent3 
7 Accent4 
8 Accent5 
9 Accent6 
10 Hyperlink 
11 Followed Hyperlink 
Parameters:
themeName: String - The theme name
colors: Color[] - The theme colors

copyTheme

function copyTheme(source)
Copies the theme from another workbook.
Parameters:
source: Workbook - Source workbook.

hasExernalLinks

function hasExernalLinks()
Indicates whether this workbook contains external links to other data sources.
Returns:
Whether this workbook contains external links to other data sources.

updateLinkedDataSource

function updateLinkedDataSource(exteralWorkbooks)
If this workbook contains external links to other data source, Aspose.Cells will attempt to retrieve the latest data. If the method is not called before calculating formulas, Aspose.Cells will use the previous information(cached in the file); Please set CellsHelper.StartupPath,CellsHelper.AltStartPath,CellsHelper.LibraryPath. And please set Workbook.FilePath if this workbook is from a stream, otherwise Aspose.Cells could not get the external link full path sometimes.
Parameters:
exteralWorkbooks: Workbook[] - External workbooks are referenced by this workbook. If it's null, we will directly open the external linked files.. If it's not null, we will check whether the external link in the array first; if not, we will open the external linked files again.

importXml

function importXml(url, sheetName, row, col)
Imports an xml file into the workbook.
Parameters:
url: String - the path of the xml file.
sheetName: String - the destination sheet name .
row: Number - the destination row of the xml.
col: Number - the destination column of the xml.

exportXml

function exportXml(mapName, path)
Export XML data.
Parameters:
mapName: String - name of the XML map that need to be exported
path: String - the export path

setDigitalSignature

function setDigitalSignature(digitalSignatureCollection)
Sets digital signature to an spreadsheet file (Excel2007 and later). Only support adding Xmldsig Digital Signature
Parameters:
digitalSignatureCollection: DigitalSignatureCollection -

addDigitalSignature

function addDigitalSignature(digitalSignatureCollection)
Adds digital signature to an OOXML spreadsheet file (Excel2007 and later). Only support adding Xmldsig Digital Signature to an OOXML spreadsheet file
Parameters:
digitalSignatureCollection: DigitalSignatureCollection -

getDigitalSignature

function getDigitalSignature()
Gets digital signature from file.

dispose

function dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

See Also:
          Aspose.Cells Documentation - the home page for the Aspose.Cellss Product Documentation.
          Aspose.Cells Support Forum - our preferred method of support.