ExcelWorkSheetAccessor Bean Suite User's Guide

Table Of Contents

Copyright © 1998 IBM Corp. All rights reserved.

Java, JavaBeans and 100% Pure Java are trademarks of Sun Microsystems, Inc. All other trademarks are the property of their respective owners.


TOC

Overview

The ExcelWorkSheetAccessor bean suite can be used to visually program a Java application to access and modify the contents of a Microsoft Excel worksheet. This bean suite consists of three non-visual beans that can be easily wired with other beans in various IDE's to create applications.

The ExcelWorkSheetAccessor beans can be used to access the following properties of a range in a worksheet:

Note : This document assumes that you are familiar with Microsoft Excel. For details on Microsoft Excel, please refer to the Microsoft Excel documentation.

The ExcelWorkSheetAccessor bean suite consists of the following beans:

The ExcelRange or ExcelCell bean has to be used together with the ExcelWorkbook bean. However, you may want to work on multiple worksheets or ranges in a single workbook. Thus, you may have one instance of the ExcelWorkbook bean with multiple instances of the ExcelRange/ExcelCell bean, all accessing the single ExcelWorkbook.

Here's an example of how you can use the ExcelWorkSheetAccessor beans to access a range in a workbook:

The ExcelRange bean uses the ExcelWorkbook bean to access an Excel workbook file. After the ExcelRange bean is successfulyl initialized, the properties of the range are available as bound properties on the ExcelRange bean.


TOC

System Requirements

Hardware Requirements

The ExcelWorkSheetAccessor bean suite uses native code (using the Java Native Interface) and is not 100% pure Java. Since the suite uses ActiveX controls, it requires a Windows environment. These beans can be executed on any hardware platform that supports:

Approximately 170 KB of disk space is required to install the excelaccesor.jar file and 196 KB is required to install the excelaccessor.dll file.

Software Requirements

Note: The ExcelWorkSheetAccessor bean suite will not work in the current implementation of BeanBox (BDK March 1998). The jar file containing the bean has a class containing native calls. However, BeanBox's implementation of Class Loader (SimpleClassLoader), does not work with classes containing native code. Refer to the FAQ.txt for more details.


TOC

Installation Instructions

  1. After downloading excelaccessor.zip, unzip the file using the jar command or any unzip utility. It will automatically create a new directory called excelaccessor and extract files into it.
  2. Move the excelaccessor.dll file from the lib directory, to any of the following directories:

    Note: The ExcelWorkbook bean accesses native code that must be shipped as a Dynamic Link Library (DLL), along with the bean. This DLL needs to be present in the system path, when the bean is being used, either by the application developer or by the end-user.

  3. To use the component in an IDE, import the excelaccessor.jar file. If you are using VisualCafé, import the excelaccessor_vcafe.jar from the lib directory.

    Note: Please refer to the User's Guide of your IDE for details on how to import a bean into the IDE.

  4. To run the demo application provided with this package, follow the instructions in the Readme.txt file, in the demo directory.

TOC

Bean Usage

ExcelWorkbook Bean

The ExcelWorkbook bean represents the Excel workbook upon which the ExcelRange bean acts. It handles the opening and closing of an Excel file. It also enables you to unprotect a structure-protected workbook. Before closing a workbook, you can choose to save or discard any modifications that have been made. The bean interacts with the DLL through the Java Native Interface. It loads the DLL and invokes the Excel application for further use.

When the ExcelWorkbook bean accesses a workbook:

When the ExcelWorkbook bean opens a workbook:

Note: If the workbook is opened in the write mode (i.e. the readOnly flag is set to false), the bean ignores a "read only recommended" status for the file. The bean will also not run any Auto_Open macros that may be present in the workbook.

When the ExcelWorkbook bean attempts to unprotect a workbook structure:

When the ExcelWorkbook bean closes a workbook:


Properties

The following are the significant properties of the ExcelWorkbook bean:

Property

Description

Data Type Attributes* Default Value Valid Value

readOnly

flag to indicate whether the workbook is to be opened in the read only mode.

boolean

R, W, B

true

true, false

workbookName

the name of the Excel file.

java.lang. String

R, W,B

" "

any non-null string

thisWorkbook

refers to this Excel Workbook object itself.

com.ibm. excel Accessor. Excel Workbook

R, B

not applicable

not applicable

saveOnClose

flag to indicate whether the changes made to the workbook file must be saved before closing the file.

boolean

R, W, B

true

true, false

workbookProtectionPassword

the protection password for the workbook. This password is required while opening a workbook that is password protected. This property cannot be used to change the password on the file.

java.lang. String

W

" "

any non-null string

workbookWriteReservationPassword

the write reservation password for the workbook. This password is used while opening a write-protected workbook in the write mode. This property cannot be used to change the password on the file.

java.lang. String

W

" "

any non-null string

workbookStructureProtectionPassword

the workbook structure protection password for the workbook. If required, this password is used while removing structure protection from the workbook. This property cannot be used to change the password on the file.

java.lang. String

W

" "

any non-null string

worksheets

an array of worksheets in an open workbook. (This property is valid only when a workbook is open).

java.lang. String [ ]

R, B

null

not applicable

opened

indicates whether the workbook is open or not.

boolean

R, B

false

not applicable

* R = read, W = write, B = bound, E = expert


Events

The ExcelWorkbook bean fires the following events:


Methods

The ExcelWorkbook bean implements the following significant methods:


Wiring Instructions

The ExcelWorkbook and ExcelRange beans must be used together to access a range in a worksheet. (For more details, refer to Overview, above.)

This sub-section describes the wiring of the Excel Workbook bean. (The wiring of the ExcelRange bean is illustrated in Figure 2, below.)

Refer to Figure 1 for the wiring diagram of the ExcelWorkbook bean.

Wiring diagram of the ExcelWorkbook bean

Figure 1 - ExcelWorkbook bean wiring diagram

This wiring diagram illustrates how the ExcelWorkbook bean can be used to open and close an Excel Workbook, and describes the events that are fired when the workbook is opened and closed.

The ExcelWorkbook bean provides a trigger, openWorkbook() method. This method is used to open the Excel workbook specified by the workbookName property of the bean. If the workbook is opened successfully, the workbookOpened (ExcelWorkbookEvent)event is fired. To close an open workbook file, the ExcelWorkbook bean provides the closeWorkbook() method. This method is invoked when the workbook is closed.

A concrete scenario is defined as follows:

The wiring diagram consists of a frame containing:

Step-by-step wiring instructions are as follows:

  1. Customize properties for the ExcelWorkbook bean and set the saveOnClose property to false.
  2. Wire the actionPerformedEvent(java.awt.ActionEvent) of the Open Workbook button to the openWorkbook() method of the ExcelWorkbook bean (conn1).
  3. Wire the FetchArgumentsCalledEvent(com.ibm.excelaccessor.event.FetchArgumentsEvent) of the ExcelWorkbook bean to the workbookName property of the same bean (conn2). The parameter for this connection is obtained from the text property of the text field (conn3).
  4. Wire the ExcelWorkbook's workbookOpened(com.ibm.excelaccessor.event.ExcelWorkbookEvent) to the text property of the label. Set the parameter for this connection to "Workbook opened." (conn4).
  5. Wire the actionPerformedEvent(java.awt.ActionEvent)of the Close Workbook button to the closeWorkbook()method of the ExcelWorkbook bean (conn5).
  6. Wire the ExcelWorkbook's workbookClosed(com.ibm.excelaccessor.event.ExcelWorkbookEvent) to the text property of the label. Set the parameter for this connection to "Workbook closed." (conn6).

Note: When the ExcelWorkbook bean is used along with the ExcelRange bean, the openWorkbook() call need not be specifically wired. The Excel workbook is automatically opened by the ExcelWorkbook bean when the workbook is first accessed by the ExcelRange bean. Once the Excel workbook is opened successfully, the worksheets in that workbook file are available as an output bound property, i.e. worksheets (an array of Strings) on the ExcelWorkbook bean.


ExcelRange Bean

The ExcelRange bean can be used to access the properties of a range of cells in an Excel workbook. This bean uses the facilities provided by the ExcelWorkbook bean to access a range. The range is identified by either of the following:

You can use the ExcelRange bean to perform the following functions:

Note: It is not necessary to initialize the ExcelRange bean before you insert or remove worksheets from a workbook.

If errors or exceptions, not related to workbook operations, are encountered while performing any of these functions, the ExcelRangeErrorEvent is fired.

Note: There are two ways of referencing a cell. The "A1" style of reference identifies a cell by its name. i.e. a cell say, B4, is referenced by the name "B4". The "R1C1" style of reference, on the other hand, references a cell using the row and column number. In this case, the cell B4 would be referenced as "row 4, column 2". The ExcelRange bean allows you to select a range using either of these methods (it provides setters for both cases). In case you invoke both types of setters on the ExcelRange bean, the setter that was invoked last is used to identify the range to be accessed. (For more information refer to the Microsoft Excel documentation.)

Before it can be used for accessing range properties, the ExcelRange bean must be initialized. While initializing, the autoCreateSheet property can be set to create a worksheet of the specified name, if it does not already exist in the workbook file. The autoUnprotectSheet property can also be set to unprotect a worksheet during initialization. If the worksheet is password protected, the listener method, fetchWorkSheetProtectionPassword(FetchPasswordEvent), is invoked.

A single instance of the range bean can be used to access different ranges. Calling the setter for the range name (or selecting a different range using row and column numbers) results in the range bean getting uninitialized. initRange has to be invoked again to initialize the range.


Properties

Following are the significant properties of the ExcelRange bean:

Property

Description

Data Type

Attributes*

Default Value

Valid Value

autoCreateSheet

flag to indicate whether the worksheet should be created automatically, while initializing the range, if the worksheet does not already exist in the workbook.

boolean

R, W, B

false

true, false

autoUnprotectSheet

if the worksheet is protected, this flag indicates whether a worksheet should be automatically unprotected when the range is initialized.

boolean

R, W, B

false

true, false.

rowHeaders

a vector of strings containing the row headers, (represented as row numbers), for this range.

java.util.Vector

R, B

not applicable

not applicable

columnHeaders

a vector of strings containing the column headers, (represented as column numbers), for this range.

java.util.Vector

R, B

not applicable

not applicable

nRows

the total number of rows in the range.

int

R, B

not applicable

not applicable

nCols

the total number of columns in the range.

int

R, B

not applicable

not applicable

startRow

the row at which the range starts

int

R, B

not applicable

not applicable

endRow

the row at which the range ends

int

R, B

not applicable

not applicable

startCol

the column at which the range starts

int

R, B

not applicable

not applicable

endCol

the column at which the range ends

int

R, B

not applicable

not
applicable

xlWorkbook

a reference to the ExcelWorkbook bean. Operations are performed using this bean.

com.ibm. excelaccessor. ExcelWorkbook

R, B

null

an instance of ExcelWorkbook bean.

worksheetName

the name of the worksheet for this range.

java.lang.String

R, W, B

" "

any non-null string.

rangeName

the name of this range.

java.lang.String

R, W, B

" "

any non-null string.

worksheetProtectionPassword

the worksheet protection password. This password is used while removing protection from the worksheet, if required. This property cannot be used to change the password.

java.lang.String

W

" "

any non-null string.

value

the value property for this range. This property is returned as a vector of vectors. While reading, this property returns various types of values such as Strings, Double values, Dates and Boolean. Time and currency values are also returned as Double values.

java.util.Vector

R, W, B

not applicable

a one- dimensional vector

or

a rectangular two- dimensional vector.

formula

the formula property for this range. Formula is returned as a vector of vectors.

java.util.Vector

R, W, B

not applicable

a one- dimensional vector

or

a rectangular two- dimensional vector.

format

the format property for this range. The format property is returned as a string, if the format for all cells in the specified range is homogeneous. Else, null will be returned. While writing this property, all the cells in this range will be set to the format string supplied as input.

java.lang.String

R, W, B

not applicable

a non-null string that is a valid format string for Excel.

noteText

the note text property for this range. While reading, the note text for the top-left cell of the range is returned. While writing, the note text for the top left corner of the range is set.

java.lang.String

R, W, B

not applicable

any String

text

the text property for this range. Accessing this property returns a two dimensional vector containing the text strings for all cells in this range.

java.util.Vector

R, B

not applicable

not applicable

defaultText

if the text for all cells in the specified range is homogeneous, the text is returned as a string, else null is returned.

java.lang.String

R, B

not applicable

not applicable

initialized

indicates whether the ExcelRange bean has been initialized or not.

boolean

R, B

false

not applicable

* R = read, W = write, B = bound, E = expert

Note: The following properties of the ExcelRange bean are valid only after a range has been initialized:
rowHeaders, columnHeaders, nRows, nCols, startRow, endRow startCol, endCol, value, formula, format, noteText, text, defaultText


Events

The ExcelRange bean fires the following events:


Methods

The ExcelRange bean implements the following significant methods:


Wiring Instructions

The ExcelRange bean must be used together with the ExcelWorkbook bean to access a range in a worksheet. (For an explanation, refer to Overview, above.) This sub-section describes the wiring of the ExcelRange bean. (The wiring of the ExcelWorkbook bean is illustrated in Figure 1, above.)

Refer to Figure 2 for the wiring diagram of the ExcelRange bean.

Wiring diagram of the ExcelRange bean

Figure 2 - ExcelRange bean wiring diagram

This wiring diagram describes how the ExcelRange bean can be used to access the properties of a range in an Excel workbook.

The ExcelRange bean provides a trigger, i.e. initRange() that needs to be called before the ExcelRange bean can be used to access or set any range properties. Once the ExcelRange bean has been initialized, the properties of the range are available as bound properties on the bean. Once the ExcelRange bean is successfully initialized, the rangeInitialized(com.ibm.excelaccessor.event.ExcelRangeEvent) is fired.

The following scenario demonstrates the getting and setting of the "format" property of an Excel range.

The wiring diagram consists of a frame containing:

  1. Customize properties for the ExcelWorkbook bean by setting:
  2. Customize properties for the ExcelRange bean by setting:
  3. Set the enabled property of the Set Format button to false. (This is an expert feature, so the "Show expert features" checkbox should be ticked.)
  4. Wire the this property of the ExcelWorkbook bean to the xlWorkbook property of the ExcelRange bean (conn1).
  5. Wire the actionPerformed(java.awt.event.ActionEvent) event of the Get Format button to the initRange() method of the ExcelRange bean (conn2).
  6. Wire the ExcelRange bean's format property to the setText(java.lang.String) method of the text field (conn3). The parameter for this connection comes from the format property of the ExcelRange (conn4).
  7. Wire the rangeInitialized(com.ibm.excelaccessor.event.ExcelRangeEvent) of the ExcelRange bean to the enabled property of the Set Format button (conn5). Set the parameter for this connection as true.
  8. Wire the actionPerformed(java.awt.event.ActionEvent)event of the Set Format button to the format property of the ExcelRange bean (conn6). The parameter for this connection comes from the text property of the text field (conn7).
  9. Wire the windowClosing(java.awt.event.WindowEvent) event of the frame to the closeWorkbook() method of the ExcelWorkbook bean (conn8).

The other properties of the range, i.e. formula, value and noteText can be accessed in a similar fashion. After the range has been initialized, these properties are available as bound properties on the ExcelRange bean.

Note: The openWorkbook()method of the ExcelWorkbook bean need not be explicitly connected in such a scenario. However, a closeWorkbook()call is recommended, otherwise changes made to the workbook may be lost.

The above scenario assumes that the workbook and worksheet being accessed are not protected. In case the workbook is protected, the ExcelWorkbook bean fires a FetchPasswordEvent before opening the file. This event can be used to set the workbookProtectionPassword or workbookWriteReservationPassword properties on the ExcelWorkbook bean. The specified password is then used to open the file.

If the worksheet is password protected and the autoUnprotect flag is set to true, a FetchPasswordEvent is fired by the ExcelRange bean during range initialization. This can be used to set the worksheetProtectionPassword property, which is then used to unprotect the worksheet.


ExcelCell Bean

The ExcelCell bean can be used to access the properties of a cell in an Excel workbook. If the range specified contains more than one cell then this bean operates on the first cell (i.e. the cell in the top-left corner) of the range. This bean uses the facilities provided by the ExcelWorkbook bean to access a cell. The cell/range is identified by either of the following:

You can use the ExcelCell bean to perform the following functions:

Note: It is not necessary to initialize the ExcelCell bean before you insert or remove worksheets from a workbook.

If errors or exceptions, not related to workbook operations, are encountered while performing any of these functions, the ExcelCellErrorEvent is fired.

Note: There are two ways of referencing a cell. The "A1" style of reference identifies a cell by its name. i.e. a cell say, B4, is referenced by the name "B4". The "R1C1" style of reference, on the other hand, references a cell using the row and column number. In this case, the cell B4 would be referenced as "row 4, column 2". The ExcelCell bean allows you to select a range using either of these methods (it provides setters for both cases). In case you invoke both types of setters on the ExcelCell bean, the setter that was invoked last is used to identify the range to be accessed. (For more information refer to the Microsoft Excel documentation.)

Before it can be used for accessing range properties, the ExcelCell bean must be initialized. While initializing, the autoCreateSheet property can be set to create a worksheet of the specified name, if it does not already exist in the workbook file. The autoUnprotectSheet property can also be set to unprotect a worksheet during initialization. If the worksheet is password protected, the listener method, fetchWorkSheetProtectionPassword(FetchPasswordEvent), is invoked.

A single instance of the cell bean can be used to access different cells. Calling the setter for the range name (or selecting a different range using row and column numbers) results in the cell bean getting uninitialized. initCell has to be invoked again to initialize the cell bean.


Properties

Following are the significant properties of the ExcelCell bean:

Property

Description

Data Type

Attributes*

Default Value

Valid Value

autoCreateSheet

flag to indicate whether the worksheet should be created automatically, while initializing the cell, if the worksheet does not already exist in the workbook.

boolean

R, W, B

false

true, false

autoUnprotectSheet

if the worksheet is protected, this flag indicates whether a worksheet should be automatically unprotected when the cell bean is initialized.

boolean

R, W, B

false

true, false.

rowHeaders

a vector of strings containing the row headers, (represented as row numbers), for this range.

java.util.Vector

R, B

not applicable

not applicable

columnHeaders

a vector of strings containing the column headers, (represented as column numbers), for this range.

java.util.Vector

R, B

not applicable

not applicable

nRows

the total number of rows in the range.

int

R, B

not applicable

not applicable

nCols

the total number of columns in the range.

int

R, B

not applicable

not applicable

startRow

the row at which the range starts

int

R, B

not applicable

not applicable

endRow

the row at which the range ends

int

R, B

not applicable

not applicable

startCol

the column at which the range starts

int

R, B

not applicable

not applicable

endCol

the column at which the range ends

int

R, B

not applicable

not
applicable

xlWorkbook

a reference to the ExcelWorkbook bean. Operations are performed using this bean.

com.ibm. excelaccessor. ExcelWorkbook

R, B

null

an instance of ExcelWorkbook bean.

worksheetName

the name of the worksheet for this range.

java.lang.String

R, W, B

" "

any non-null string.

rangeName

the name of this range. If the range contains multiple cells then the ExcelCell bean operates on the cell in the top-left corner of the range.

java.lang.String

R, W, B

" "

any non-null string.

worksheetProtectionPassword

the worksheet protection password. This password is used while removing protection from the worksheet, if required. This property cannot be used to change the password.

java.lang.String

W

" "

any non-null string.

value

the value property for this cell. This property is returned as an Object. While reading, this property returns various types of values such as Strings, Double values, Dates and Boolean. Time and currency values are also returned as Double values.

java.lang.Object

R, W, B

not applicable

any object.

formula

the formula property for this cell. Formula is returned as a String.

java.lang.String

R, W, B

not applicable

any string.

format

the format property for this cell. The format property is returned as a string.

java.lang.String

R, W, B

not applicable

a non-null string that is a valid format string for Excel.

noteText

the note text property for this cell.

java.lang.String

R, W, B

not applicable

any String

text

the text property for this cell.

java.lang.String

R, B

not applicable

not applicable

initialized

indicates whether the ExcelCell bean has been initialized or not.

boolean

R, B

false

not applicable

* R = read, W = write, B = bound, E = expert

Note: The following properties of the ExcelCell bean are valid only after a range has been initialized:
rowHeaders, columnHeaders, nRows, nCols, startRow, endRow startCol, endCol, value, formula, format, noteText, text


Events

The ExcelCell bean fires the following events:


Methods

The ExcelCell bean implements the following significant methods:


Wiring Instructions

The ExcelCell bean must be used together with the ExcelWorkbook bean to access a cell in a worksheet. (For an explanation, refer to Overview, above.) This sub-section describes the wiring of the ExcelCell bean. (The wiring of the ExcelWorkbook bean is illustrated in Figure 1, above.)

Refer to Figure 3 for the wiring diagram of the ExcelCell bean.

Wiring diagram of the ExcelCell bean

Figure 3 - ExcelCell bean wiring diagram

This wiring diagram describes how the ExcelCell bean can be used to access the properties of a cell in an Excel workbook.

The ExcelCell bean provides a trigger, i.e. initCell() that needs to be called before the ExcelCell bean can be used to access or set any range properties. Once the ExcelCell bean has been initialized, the properties of the cell are available as bound properties on the bean. Once the ExcelCell bean is successfully initialized, the cellInitialized(com.ibm.excelaccessor.event.ExcelCellEvent) is fired.

The following scenario demonstrates the getting and setting of the "format" property of a cell.

The wiring diagram consists of a frame containing:

  1. Customize properties for the ExcelWorkbook bean by setting:
  2. Customize properties for the ExcelCell bean by setting:
  3. Set the enabled property of the Set Format button to false. (This is an expert feature, so the "Show expert features" checkbox should be ticked.)
  4. Wire the this property of the ExcelWorkbook bean to the xlWorkbook property of the ExcelCell bean (conn1).
  5. Wire the actionPerformed(java.awt.event.ActionEvent) event of the Get Format button to the initCell() method of the ExcelCell bean (conn2).
  6. Wire the ExcelCell bean's format property to the setText(java.lang.String) method of the text field (conn3). The parameter for this connection comes from the format property of the ExcelCell bean (conn4).
  7. Wire the cellInitialized(com.ibm.excelaccessor.event.ExcelCellEvent) of the ExcelCell bean to the enabled property of the Set Format button (conn5). Set the parameter for this connection as true.
  8. Wire the actionPerformed(java.awt.event.ActionEvent)event of the Set Format button to the format property of the ExcelCell bean (conn6). The parameter for this connection comes from the text property of the text field (conn7).
  9. Wire the windowClosing(java.awt.event.WindowEvent) event of the frame to the closeWorkbook() method of the ExcelWorkbook bean (conn8).

The other properties of the cell, i.e. formula, value and noteText can be accessed in a similar fashion. After the cell bean has been initialized, these properties are available as bound properties on the ExcelCell bean.

Note: The openWorkbook()method of the ExcelWorkbook bean need not be explicitly connected in such a scenario. However, a closeWorkbook()call is recommended, otherwise changes made to the workbook may be lost.

The above scenario assumes that the workbook and worksheet being accessed are not protected. In case the workbook is protected, the ExcelWorkbook bean fires a FetchPasswordEvent before opening the file. This event can be used to set the workbookProtectionPassword or workbookWriteReservationPassword properties on the ExcelWorkbook bean. The specified password is then used to open the file.

If the worksheet is password protected and the autoUnprotect flag is set to true, a FetchPasswordEvent is fired by the ExcelCell bean during cell initialization. This can be used to set the worksheetProtectionPassword property, which is then used to unprotect the worksheet.