AnyLogic
Expand
Font size

Excel file

The  Excel File connectivity tool provides easy platform-independent access to MS Excel(.xls, .xlsx) files within AnyLogic models. With this object you can:

To add the Excel file access tool

  1. Drag the Excel File AnyLogic: Excel File element from the Connectivity palette onto the graphical diagram. You can also drag the Excel files from other applications directly onto the AnyLogic graphical diagram. In this case, AnyLogic automatically adds a new Excel File AnyLogic: Excel File element on the graphical diagram containing the added Excel file.
  2. In the Properties view, change the Name of the element. This name will be used to identify and access this object from the code.
  3. In the File field, specify the Excel file that this element will work with. Click the Browse button to locate the file.
    If the file is stored in the model folder, the File field displays the relative path; otherwise, it displays the absolute path. Once selected, the source file automatically appears in the model’s Resource folder in the Projects view. This way you will be able to track the current state of the source file, toggle between the absolute and relative file paths, and so on.
  4. Go to the Advanced section. Leave the Load on model startup check box selected. This will automatically load the workbook from the file on model startup and allow you to work with this Excel file.
  5. If you expect to save some data in the specified Excel file, leave the Save on model termination check box selected. This will save you from calling the writeFile() function of this access tool to commit all your changes into the Excel file: this will be done automatically at model exit..

To work with Excel files, use the API of the ExcelFile object. The most useful of API functions are listed below, in the API section. Most of these have several different argument sets and notations. Look for the operation you need, then expand the appropriate section and find the function that best suits your needs.

Call the readFile() function before performing any operations with the Excel file. Alternatively, select the Load on model startup option in the properties of this object: in this case, the workbook will be automatically loaded from the file when the model is started.

API

Reading from Excel file

Before AnyLogic 8.9, the getCellType() and getCellFormulaType() used to return values of type int. As of now, they return CellType values, which may affect your models that peruse Excel files.
For conversion tips, see CellType enum.
Reading Excel file: readFile()
Function Description
void readFile() Loads the workbook from the file.
All unsaved data (if any) in the workbook is lost after this function is called.
Getting the type of the specified cell: getCellType()
The getCellType() function returns the cell type (numeric, formula, string, …).
The function has 3 notations with different argument sets. They differ in the way they address the cell.
Function Description
CellType getCellType(int sheetIndex, int rowIndex, int columnIndex) The cell is specified by 3 numbers (one-based): sheet index, row index, column index.
CellType getCellType(String sheetName, int rowIndex, int columnIndex) The cell is specified by a sheet name and 2 (one-based) numbers, row index and column index.
CellType getCellType(String cellName) The cell is specified by its name in the following format: <sheet name>!<column name> <rownumber>, for example:Sheet1!A3.
The sheet name can be skipped, then the first sheet will be taken.
Reading a Boolean value from a cell: getCellBooleanValue()
The getCellBooleanValue() function returns the value of the cell as a boolean. For strings, numbers, and errors, throws an exception. For empty cells, returns false.
The function has 3 notations with different argument sets. They differ in the way they address the cell.
Function Description
boolean getCellBooleanValue(int sheetIndex, int rowIndex, int columnIndex) The cell is specified by 3 numbers (one-based): sheet index, row index, column index.
boolean getCellBooleanValue(String sheetName, int rowIndex, int columnIndex) The cell is specified by a sheet name and 2 (one-based) numbers, row index and column index.
boolean getCellBooleanValue(String cellName) The cell is specified by its name in the following format: <sheet name>!<column name> <rownumber>, for example:Sheet1!A3.
The sheet name can be skipped, then the first sheet will be taken.
Reading a numeric value from a cell: getCellNumericValue()
The getCellNumericValue() function returns the value of the cell as a number. For strings, it throws an exception. For empty cells, returns 0.
The function has 3 notations with different argument sets. They differ in the way they address the cell.
Function Description
double getCellNumericValue(int sheetIndex, int rowIndex, int columnIndex) The cell is specified by 3 numbers (one-based): sheet index, row index, column index.
double getCellNumericValue(String sheetName, int rowIndex, int columnIndex) The cell is specified by a sheet name and 2 (one-based) numbers, row index and column index.
double getCellNumericValue(String cellName) The cell is specified by its name in the following format: <sheet name>!<column name> <rownumber>, for example:Sheet1!A3.
The sheet name can be skipped, then the first sheet will be taken.
Reading a string value from a cell: getCellStringValue()
The getCellStringValue() function returns the value of the cell as a string. For numeric cells, it throws an exception. For empty cells, returns an empty string. For formula cells that are not string formulas, returns an empty string.
The function has 3 notations with different argument sets. They differ in the way they address the cell.
Function Description
String getCellStringValue(int sheetIndex, int rowIndex, int columnIndex) The cell is specified by 3 numbers (one-based): sheet index, row index, column index.
String getCellStringValue(String sheetName, int rowIndex, int columnIndex) The cell is specified by a sheet name and 2 (one-based) numbers, row index and column index.
String getCellStringValue(String cellName) The cell is specified by its name in the following format: <sheet name>!<column name> <rownumber>, for example:Sheet1!A3.
The sheet name can be skipped, then the first sheet will be taken.
Reading a Date value from a cell: getCellDateValue()
The getCellDateValue() function returns the value of the cell as a date. For strings, it throws an exception. For empty cells, returns null.
The function has 3 notations with different argument sets. They differ in the way they address the cell.
Function Description
Date getCellDateValue(int sheetIndex, int rowIndex, int columnIndex) The cell is specified by 3 numbers (one-based): sheet index, row index, column index.
Date getCellDateValue(String sheetName, int rowIndex, int columnIndex) The cell is specified by a sheet name and 2 (one-based) numbers, row index and column index.
Date getCellDateValue(String cellName) The cell is specified by its name in the following format: <sheet name>!<column name> <rownumber>, for example:Sheet1!A3.
The sheet name can be skipped, then the first sheet will be taken.
Getting the formula defined for a specified cell: getCellFormula()
The getCellFormula() function returns the formula for the cell, for example: SUM(C4:E4).
The function has 3 notations with different argument sets. They differ in the way they address the cell.
Function Description
String getCellFormula(int sheetIndex, int rowIndex, int columnIndex) The cell is specified by 3 numbers (one-based): sheet index, row index, column index.
String getCellFormula(String sheetName, int rowIndex, int columnIndex) The cell is specified by a sheet name and 2 (one-based) numbers, row index and column index.
String getCellFormula(String cellName) The cell is specified by its name in the following format: <sheet name>!<column name> <rownumber>, for example:Sheet1!A3.
The sheet name can be skipped, then the first sheet will be taken.
Getting the type of the formula defined for a specified cell: getCellFormulaType()
The getCellFormulaType() function returns the type of the formula cell. Valid for formula cells only.
The function has 3 notations with different argument sets. They differ in the way they address the cell.
Function Description
CellType getCellFormulaType(int sheetIndex, int rowIndex, int columnIndex) The cell is specified by 3 numbers (one-based): sheet index, row index, column index.
CellType getCellFormulaType(String sheetName, int rowIndex, int columnIndex) The cell is specified by a sheet name and 2 (one-based) numbers, row index and column index.
CellType getCellFormulaType(String cellName) The cell is specified by its name in the following format: <sheet name>!<column name> <rownumber>, for example:Sheet1!A3.
The sheet name can be skipped, then the first sheet will be taken.
Reading the value of the cell as an error code: getCellErrorValue()
The getCellErrorValue() function returns the value of the cell as an error code. For strings throws an exception. For empty cells, returns null.
The function has 3 notations with different argument sets. They differ in the way they address the cell.
Function Description
byte getCellErrorValue(int sheetIndex, int rowIndex, int columnIndex) The cell is specified by 3 numbers (one-based): sheet index, row index, column index.
byte getCellErrorValue(String sheetName, int rowIndex, int columnIndex) The cell is specified by a sheet name and 2 (one-based) numbers, row index and column index.
byte getCellErrorValue(String cellName) The cell is specified by its name in the following format: <sheet name>!<column name> <rownumber>, for example:Sheet1!A3.
The sheet name can be skipped, then the first sheet will be taken.
Reading data from the Excel file into AnyLogic table function: readTableFunction()
The readTableFunction() function reads the data from the Excel file into the specified table function.
If there is not enough data in the sheet to fill in the length, then the table function gets fewer points.
The 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.
The function has 3 notations with different argument sets. They differ in the way they address the cell.
Function Description
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 the column at columnIndex,
  • Values are read from the 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 the column at columnIndex,
  • Values are read from the 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 the column of the given cell
  • Values are read from the column next to the given cell
The cell is specified by name in the following format: <sheet name>!<column name><row number>, for example: Sheet1!A3.
The sheet name can be skipped, then the first sheet will be taken.
Reading data from Excel file into AnyLogic hyperarray: readHyperArray()
The readHyperArray() function 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 (that is, in this mode data for one-dimensional array is loaded from the sheet column).
The function has 3 notations with different argument sets. They differ in the way they address the cell.
Function Description
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: <sheet name>!<column name><rownumber>, for example, Sheet1!A3.
The sheet name can be skipped, then the first sheet is assumed.

CellType enum

Before AnyLogic 8.9.0 and the Apache POI library update, there were 6 integer constants that indicated the type of an Excel cell. After the POI update, the integer values were replaced with an enumerator, so some constants in AnyLogic changed their type to CellType.

To identify errors in the model created with the previous version of Apache POI, select View > Log from the AnyLogic menu.

  • If integer constants are used in a switch statement, a compilation error will occur. Replace all constants in the cases with values from the CellType enum: STRING, NUMERIC, and so on.
  • If the results of the getCellType(…) and getCellFormulaType(…) function calls are assigned to int variables, an error will occur. The solution is to replace int in variable declaration with var, or add CellType to the list of agent imports by typing the following in the Imports section of the agent’s properties:
    import org.apache.poi.ss.usermodel.CellType;
    With this import set up, CellType can be explicitly declared as the type of the variable, replacing int.
  • In more complex cases, where the results of the above functions are compared to integers, or where a getWorkbook() function is used to interact with the Excel file, consider revising your model, or contact our support team for assistance.

Writing to Excel file

Call the writeFile() function to commit your changes to an Excel file. Alternatively, select the Save on model termination option in the properties of the Excel file object: in this case, the changes are automatically saved when the model is exited.
Saving changes into Excel file: writeFile()
Function Description
void writeFile() Stores the current workbook to the file. The workbook should be loaded. Unchanged workbooks (workbooks with unsaved data) aren’t saved. To save to a different location, call the setFileName(String ) function before writeFile().
Creating a cell with specified index: createCell()
The createCell() function creates a new cell at the given position.
The function has 3 notations with different argument sets. They differ in the way they address the cell.
Function Description
void createCell(int sheetIndex, int rowIndex, int columnIndex) The cell is specified by 3 numbers (one-based): sheet index, row index, column index.
void createCell(String sheetName, int rowIndex, int columnIndex) The cell is specified by a sheet name and 2 (one-based) numbers, row index and column index.
void createCell(String cellName) The cell is specified by its name in the following format: <sheet name>!<column name> <rownumber>, for example:Sheet1!A3.
The sheet name can be skipped, then the first sheet will be taken.
Writing a value into a cell: setCellValue()
The setCellValue() function sets a given value for the specified cell. The function can take values of all the most common types: boolean, double, String, Date.
The function has 3 notations with different argument sets. They differ in the way they address the cell.
Function Description
void setCellValue(<type> value, int sheetIndex, int rowIndex, int columnIndex) The cell is specified by 3 numbers (one-based): sheet index, row index, column index.
void setCellValue(<type> value, String sheetName, int rowIndex, int columnIndex) The cell is specified by a sheet name and 2 (one-based) numbers, row index and column index.
void setCellValue(<type> value, String cellName) The cell is specified by its name in the following format: <sheet name>!<column name> <rownumber>, for example:Sheet1!A3.
The sheet name can be skipped, then the first sheet will be taken.
Setting a formula for a specified cell: setCellFormula()
The setCellFormula() function sets a formula for the specified cell. The formula is passed using the function’s argument formula, that is, "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 evaluate the formula, call evaluateFormulas() function.
The function has 3 notations with different argument sets. They differ in the way they address the cell.
Function Description
void setCellFormula(String formula, int sheetIndex, int rowIndex, int columnIndex) The cell is specified by 3 numbers (one-based): sheet index, row index, column index.
void setCellFormula(String formula, String sheetName, int rowIndex, int columnIndex) The cell is specified by a sheet name and 2 (one-based) numbers, row index and column index.
void setCellFormula(String formula, String cellName) The cell is specified by its name in the following format: <sheet name>!<column name> <rownumber>, for example:Sheet1!A3.
The sheet name can be skipped, then the first sheet will be taken.
Writing data from AnyLogic dataset into Excel file: writeDataSet()
The writeDataSet() function writes the given data set to the sheet starting at the given cell. Data is written by rows in 2 columns: for X and Y components.
The function has 3 notations with different argument sets. They differ in the way they address the cell. The function returns the size of the data set.
Function Description
int writeDataSet(DataSet dataset, int sheetIndex, int rowIndex, int columnIndex) The cell is specified by 3 numbers (one-based): sheet index, row index, column index.
int writeDataSet(DataSet dataset, String sheetName, int rowIndex, int columnIndex) The cell is specified by a sheet name and 2 (one-based) numbers, row index and column index.
int writeDataSet(DataSet dataset, String cellName) The cell is specified by its name in the following format: <sheet name>!<column name> <rownumber>, for example:Sheet1!A3.
The sheet name can be skipped, then the first sheet will be taken.

Validation and auxiliary operations

Getting a number of sheets in Excel file: getNumberOfSheets()
Function Description
int getNumberOfSheets() Returns the number of spreadsheets in the workbook.
Getting a name of the specified sheet: getSheetName()
Function Description
String getSheetName(int sheetIndex) Returns the sheet name for the sheet with specified index sheetIndex.
Checking whether the specified cell exists: cellExists()
The cellExists() function returns true if the cell at the given position exists in the workbook.
The function has 3 notations with different argument sets. They differ in the way they address the cell.
Function Description
boolean cellExists(int sheetIndex, int rowIndex, int columnIndex) The cell is specified by 3 numbers (one-based): sheet index, row index, column index.
boolean cellExists(String sheetName, int rowIndex, int columnIndex) The cell is specified by a sheet name and 2 (one-based) numbers, row index and column index.
boolean cellExists(String cellName) The cell is specified by its name in the following format: <sheet name>!<column name> <rownumber>, for example:Sheet1!A3.
The sheet name can be skipped, then the first sheet will be taken.
Getting a number of the first row of the sheet: getFirstRowNum()
The getFirstRowNum() function returns the 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.
Function Description
int getFirstRowNum(int sheetIndex) The sheet is specified by its index.
int getFirstRowNum(String sheetName) The sheet is specified by its name.
Getting a number of the last row of the sheet: getLastRowNum()
The getLastRowNum() function returns the 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.
Function Description
int getLastRowNum(int sheetIndex) The sheet is specified by its index.
int getLastRowNum(String sheetName) The sheet is specified by its name.
Getting a number of the first cell in the row: getFirstCellNum()
The getFirstCellNum() function 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 and row are addressed.
Function Description
int getFirstCellNum(int sheetIndex, int rowIndex) Both the sheet and the row are specified by indices.
int getFirstCellNum(String sheetName, int rowIndex) The sheet is specified by its name; the row — by its index.
Getting a number of the last cell in the row: getLastCellNum()
The getLastCellNum() function 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.
Function Description
int getLastCellNum(int sheetIndex, int rowIndex) Both the sheet and the row are specified by indices.
int getLastCellNum(String sheetName, int rowIndex) The sheet is specified by its name; the row — by its index.
Evaluating formulas in Excel file: evaluateFormulas()
Function Description
void evaluateFormulas() 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 the following function: org.apache.poi.ss.usermodel.Cell.evaluateInCell(org.apache.poi.ss.usermodel.Cell)
How can we improve this article?