Excel File connectivity tool provides easy platform-independent access to MS Excel 1997-2007 (.xls) files within AnyLogic models. Using that object you can:
Demonstration model: Reading and Writing Excel Files
To
add Excel file access tool
element from
the Connectivity
stencil of the Palette onto
the diagram of active object class (or experiment). You work with Excel files
using the corresponding
API of the
ExcelFile object.
All the
methods are listed below. Please note that nearly all the
functions have
several different argument sets
and notations. Therefore we recommend you to search for the operation
you need, then expand the corresponding section and find the method
that suits your needs best.
Please note that you should call the method readFile() before trying to perform any operations with the Excel file. The alternative way is to select the Load on startup checkbox in the properties of this object - in this case the workbook will be automatically loaded from the file on model startup.
void readFile()
- Loads the workbook from the file.
Warning! All
unsaved data
(if any) in the workbook is lost after this method is called.
The method getCellType() returns the cell type (numeric, formula, string...)
The method has three notations with different argument sets. They differ in the way the cell is addressed.
int getCellType(int sheetIndex, int rowIndex, int columnIndex) - the cell is specified using 3 numbers (one-based): sheet index, row index, column index.
int getCellType(String sheetName, int rowIndex, int columnIndex) - the cell is specified using a sheet name and 2 (one-based) numbers row index and column index.
int getCellType(String cellName) - the cell is specified by name in the following format: <sheetName>!<columnName><rowNumber>, e.g. Sheet1!A3 (The sheet name can be skipped, then the first sheet is assumed)
The method getCellBooleanValue() returns the value of the cell as a boolean. For strings, numbers, and errors, throws an exception. For blank cells returns false.
The method has three notations with different argument sets. They differ in the way the cell is addressed.
boolean getCellBooleanValue(int sheetIndex, int rowIndex, int columnIndex) - the cell is specified using 3 numbers (one-based): sheet index, row index, column index.
boolean getCellBooleanValue(String sheetName, int rowIndex, int columnIndex) - the cell is specified using a sheet name and 2 (one-based) numbers row index and column index.
boolean getCellBooleanValue(String cellName) - the cell is specified by name in the following format: <sheetName>!<columnName><rowNumber>, e.g. Sheet1!A3 (The sheet name can be skipped, then the first sheet is assumed)
The method getCellNumericValue() returns the value of the cell as a number. For strings throws an exception. For blank cells we return a 0.
The method has three notations with different argument sets. They differ in the way the cell is addressed.
double getCellNumericValue(int sheetIndex, int rowIndex, int columnIndex) - the cell is specified using 3 numbers (one-based): sheet index, row index, column index.
double getCellNumericValue(String sheetName, int rowIndex, int columnIndex) - the cell is specified using a sheet name and 2 (one-based) numbers row index and column index.
double getCellNumericValue(String cellName) - the cell is specified by name in the following format: <sheetName>!<columnName><rowNumber>, e.g. Sheet1!A3 (The sheet name can be skipped, then the first sheet is assumed)
The method getCellStringValue() returns the value of the cell as a string - for numeric cells throws an exception. For blank cells returns an empty string. For formula cells that are not string formulas, returns empty string
The method has three notations with different argument sets. They differ in the way the cell is addressed.
String getCellStringValue(int sheetIndex, int rowIndex, int columnIndex) - the cell is specified using 3 numbers (one-based): sheet index, row index, column index.
String getCellStringValue(String sheetName, int rowIndex, int columnIndex) - the cell is specified using a sheet name and 2 (one-based) numbers row index and column index.
String getCellStringValue(String cellName) - the cell is specified by name in the following format: <sheetName>!<columnName><rowNumber>, e.g. Sheet1!A3 (The sheet name can be skipped, then the first sheet is assumed)
The method getCellDateValue() returns the value of the cell as a date. For strings throws an exception. For blank cells returns null.
The method has three notations with different argument sets. They differ in the way the cell is addressed.
Date getCellDateValue(int sheetIndex, int rowIndex, int columnIndex) - the cell is specified using 3 numbers (one-based): sheet index, row index, column index.
Date getCellDateValue(String sheetName, int rowIndex, int columnIndex) - the cell is specified using a sheet name and 2 (one-based) numbers row index and column index.
Date getCellDateValue(String cellName) - the cell is specified by name in the following format: <sheetName>!<columnName><rowNumber>, e.g. Sheet1!A3 (The sheet name can be skipped, then the first sheet is assumed)
The method getCellFormula() returns a formula for the cell, for example, SUM(C4:E4)
The method has three notations with different argument sets. They differ in the way the cell is addressed.
String getCellFormula(int sheetIndex, int rowIndex, int columnIndex) - the cell is specified using 3 numbers (one-based): sheet index, row index, column index.
String getCellFormula(String sheetName, int rowIndex, int columnIndex) - the cell is specified using a sheet name and 2 (one-based) numbers row index and column index.
String getCellFormula(String cellName) - the cell is specified by name in the following format: <sheetName>!<columnName><rowNumber>, e.g. Sheet1!A3 (The sheet name can be skipped, then the first sheet is assumed)
The method getCellFormulaType() returns the type of the formula cell. Only valid for formula cells.
The method has three notations with different argument sets. They differ in the way the cell is addressed.
int getCellFormulaType(int sheetIndex, int rowIndex, int columnIndex) - the cell is specified using 3 numbers (one-based): sheet index, row index, column index.
int getCellFormulaType(String sheetName, int rowIndex, int columnIndex) - the cell is specified using a sheet name and 2 (one-based) numbers row index and column index.
int getCellFormulaType(String cellName) - the cell is specified by name in the following format: <sheetName>!<columnName><rowNumber>, e.g. Sheet1!A3 (The sheet name can be skipped, then the first sheet is assumed)
The method getCellErrorValue() returns the value of the cell as an error code. For strings throws an exception. For blank cells returns null.
The method has three notations with different argument sets. They differ in the way the cell is addressed.
byte getCellErrorValue(int sheetIndex, int rowIndex, int columnIndex)- the cell is specified using 3 numbers (one-based): sheet index, row index, column index.
byte getCellErrorValue(String sheetName, int rowIndex, int columnIndex)- the cell is specified using a sheet name and 2 (one-based) numbers row index and column index.
byte getCellErrorValue(String cellName)- the cell is specified by name in the following format: <sheetName>!<columnName><rowNumber>, e.g. Sheet1!A3 (The sheet name can be skipped, then the first sheet is assumed)
The method readTableFunction()
reads the data from Excel file into specified table function.
If there is not enough data in the sheet to fill in the length, then
table function gets less points.
Method returns the actual number of table function points read from the
sheet.
Arguments:
tableFunction -
the table function to
fill
length - the
number of table function
points to read
int
readTableFunction(TableFunction
tableFunction, int sheetIndex, int rowIndex, int columnIndex, int
length)
- Reads the table function from the sheet with index sheetIndex starting at the row
with index rowIndex:
- arguments are read from column at columnIndex
- values are read from column at columnndex
+ 1
int
readTableFunction(TableFunction
tableFunction, String sheetName, int rowIndex, int columnIndex, int
length) - Reads the table function from the sheet with
name sheetName
starting at the row with index rowIndex:
- arguments are read from column at columnIndex
- values are read from column at columnndex
+ 1
int
readTableFunction(TableFunction
tableFunction, String cellName, int length)
- Reads the table function from the sheet starting at the row of the
given cell:
- arguments are read from column of the given cell
- values are read from column next to the given cell
The cell is specified by name in the following format: <sheetName>!<columnName><rowNumber>,
e.g. Sheet1!A3
(The sheet name can be skipped, then the first sheet is assumed)
The method readHyperArray() reads
one- or two-dimensional
HyperArray data from the sheet starting at the given cell.
Arguments:
array -
the HyperArray to write
data to, should have 1 or 2 dimensions.
dim1AcrossRows -
use true to read the
data corresponding to the
first dimension, across the sheet rows (e.g. in this mode data for
one-dimensional array is loaded from the sheet column)
The method has three notations with different
argument sets. They differ in the way the cell is addressed.
void readHyperArray(HyperArray
array, int sheetIndex,
int
rowIndex, int columnIndex, boolean dim1AcrossRows)
- the cell is specified using 3 numbers (one-based): sheet
index, row index, column index.
void readHyperArray(HyperArray array, String sheetName, int rowIndex, int columnIndex, boolean dim1AcrossRows) - the cell is specified using a sheet name and 2 (one-based) numbers row index and column index.
void readHyperArray(HyperArray array, String cellName, boolean dim1AcrossRows) - the cell is specified by name in the following format: <sheetName>!<columnName><rowNumber>, e.g. Sheet1!A3 (The sheet name can be skipped, then the first sheet is assumed)
void writeFile() - Stores the current workbook to the file. Workbook should be loaded. Unchanged workbooks (workbooks with unsaved data) aren't saved. For saving to another location please call setFileName(String) before this method.
The method createCell() creates a new cell at the given position.
The method has three notations with different argument sets. They differ in the way the cell is addressed.
void createCell(int sheetIndex, int rowIndex, int columnIndex) - the cell is specified using 3 numbers (one-based): sheet index, row index, column index.void createCell(String sheetName, int rowIndex, int columnIndex) - the cell is specified using a sheet name and 2 (one-based) numbers row index and column index.
void createCell(String cellName) - the cell is specified by name in the following format: <sheetName>!<columnName><rowNumber>, e.g. Sheet1!A3 (The sheet name can be skipped, then the first sheet is assumed)
The method setCellValue() sets a given value for the specified cell. The method can take values of all most used types: boolean, double, String, Date
The method has three notations with different argument sets. They differ in the way the cell is addressed.
void setCellValue(<type> value, int sheetIndex, int rowIndex, int columnIndex) - the cell is specified using 3 numbers (one-based): sheet index, row index, column index.
void setCellValue(<type> value, String sheetName, int rowIndex, int columnIndex) - the cell is specified using a sheet name and 2 (one-based) numbers row index and column index.
void setCellValue(<type> value, String cellName) - the cell is specified by name in the following format: <sheetName>!<columnName><rowNumber>, e.g. Sheet1!A3 (The sheet name can be skipped, then the first sheet is assumed)
The method setCellFormula() sets a formula for
the specified cell.
The formula is passed using the method's argument formula, e.g. "SUM(C4:E4)".
If this argument is null
then the
current formula is removed.
This method only sets the formula string and does not calculate the
formula value. To set the 'precalculated' value use setCellValue(...) method. To
evaluate the
formula, call evaluateFormuals()
method.
The method has three notations with different argument sets. They differ in the way the cell is addressed.
void setCellFormula(String formula, int sheetIndex, int rowIndex, int columnIndex) - the cell is specified using 3 numbers (one-based): sheet index, row index, column index.
void setCellFormula(String formula, String sheetName, int rowIndex, int columnIndex) - the cell is specified using a sheet name and 2 (one-based) numbers row index and column index.
void setCellFormula(String formula, String cellName) - the cell is specified by name in the following format: <sheetName>!<columnName><rowNumber>, e.g. Sheet1!A3 (The sheet name can be skipped, then the first sheet is assumed)
The method has three notations with different argument sets. They differ in the way the cell is addressed:
int writeDataSet(DataSet dataset, int sheetIndex, int rowIndex, int columnIndex) - the cell is specified using 3 numbers (one-based): sheet index, row index, column index.
int writeDataSet(DataSet dataset, String sheetName, int rowIndex, int columnIndex) - the cell is specified using a sheet name and 2 (one-based) numbers row index and column index.
int writeDataSet(DataSet dataset, String cellName)- the cell is specified by name in the following format: <sheetName>!<columnName><rowNumber>, e.g. Sheet1!A3 (The sheet name can be skipped, then the first sheet is assumed)
int getNumberOfSheets() - Returns the number of spreadsheets in the workbook.
String getSheetName(int sheetIndex) - Returns the sheet name for the sheet with specified index sheetIndex.
The method cellExists()
returns true
if the cell at the given position exists in the workbook.
The method has three notations with different
argument sets. They differ in the way the cell is addressed:
boolean cellExists(int sheetIndex,
int
rowIndex, int columnIndex)- the
cell is specified using 3 numbers (one-based): sheet index, row
index, column index.
boolean cellExists(String sheetName, int rowIndex, int columnIndex)- the cell is specified using a sheet name and 2 (one-based) numbers row index and column index.
boolean cellExists(String cellName)- the cell is specified by name in the following format: <sheetName>!<columnName><rowNumber>, e.g. Sheet1!A3 (The sheet name can be skipped, then the first sheet is assumed)
The method getFirstRowNum() returns number of
the first
logical row of the sheet (one-based indexing is used).
The method has two notations with different
argument sets. They differ in the way the sheet is addressed:
int getFirstRowNum(int sheetIndex)
- the sheet is specified by its index.
int getFirstRowNum(String sheetName) - the sheet is specified by its name.
The method getLastRowNum() returns number of
the last logical row
of the sheet (one-based indexing is used).
The method has two notations with different
argument sets. They differ in the way the sheet is addressed:
int getLastRowNum(int sheetIndex)
- the sheet is specified by its index.
int getLastRowNum(String sheetName) - the sheet is specified by its name.
The method getFirstCellNum() returns the number
of the first cell
contained in the specified row. In particular, it returns the number of
the column (one-based indexing is used) containing the first
logical cell in the row, or 0 if the row does not contain any
cells.
The method has two notations with different
argument sets. They differ in the way the sheet is addressed:
int getFirstCellNum(int sheetIndex,
int
rowIndex)
- both the sheet and the row are specified by indexes.
int getFirstCellNum(String sheetName, int rowIndex) - the sheet is specified by its name; the row - by its index.
The method getLastCellNum() returns
the number of the last cell contained in the specified row. In
particular, it returns the number of the column (one-based indexing is
used) containing the last logical cell in the row, or 0 if the
row
does not contain any cells.
The method has two notations with different
argument sets. They differ in the way the sheet is addressed:
int getLastCellNum(int sheetIndex,
int
rowIndex)
- both the sheet and the row are specified by indexes.
int getLastCellNum(String sheetName, int rowIndex) - the sheet is specified by its name; the row - by its index.
void
evaluateFormulas(int sheetIndex,
int rowIndex)
- Evaluates formulas and saves the results for all the cells containing
formulas in this workbook.
The cells are left as formula cells. Be aware that your cells will hold
both the formula, and the result. If you want the cell replaced with
the result of the formula, use method org.apache.poi.ss.usermodel.Cell.evaluateInCell(org.apache.poi.ss.usermodel.Cell)