Excel File connectivity tool provides easy platform-independent access to MS Excel (.xls, .xlsx) files within AnyLogic models. Using that object you can:
Demonstration model: Reading and Writing Excel Files
To add Excel file access tool
You work with Excel files using the corresponding API of the
ExcelFile
object. All the functions 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 function that suits your needs best.
void readFile()
- Loads the workbook from the file.
Warning!
All unsaved data (if any) in the workbook is lost after this function is called.
The function getCellType() returns the cell type (numeric, formula, string...)
The function 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 function getCellBooleanValue() returns the value of the cell as a boolean. For strings, numbers, and errors, throws an exception. For blank cells returns false.
The function 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 function getCellNumericValue() returns the value of the cell as a number. For strings throws an exception. For blank cells we return a 0.
The function 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 function 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 function 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 function getCellDateValue() returns the value of the cell as a date. For strings throws an exception. For blank cells returns null.
The function 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 function getCellFormula() returns a formula for the cell, for example, SUM(C4:E4)
The function 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 function getCellFormulaType() returns the type of the formula cell. Only valid for formula cells.
The function 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 function getCellErrorValue() returns the value of the cell as an error code. For strings throws an exception. For blank cells returns null.
The function 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 function
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.
Function 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
columnIndex 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
columnIndex 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 function
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 function 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 function.
The function createCell() creates a new cell at the given position.
The function 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 function setCellValue() sets a given value for the specified cell. The function can take values of all most used types: boolean, double, String, Date
The function 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 function
setCellFormula()
sets a formula for the specified cell. The formula is passed using the function's argument
formula, e.g.
"SUM(C4:E4)". If this argument is
null
then the current formula is removed.
This function only sets the formula string and does not calculate the formula value. To set the 'precalculated' value use
setCellValue(...)
function. To evaluate the formula, call
evaluateFormulas()
function.
The function 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 function 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 function
cellExists()
returns
true
if the cell at the given position exists in the workbook.
The function 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 function
getFirstRowNum()
returns number of the first logical row of the sheet (one-based indexing is used).
The function 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 function
getLastRowNum()
returns number of the last logical row of the sheet (one-based indexing is used).
The function 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 function
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 function 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 function
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 function 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 function
org.apache.poi.ss.usermodel.Cell.evaluateInCell(org.apache.poi.ss.usermodel.Cell)