Font size

Exporting data to MS Excel workbook

To export data from AnyLogic database tables to MS Excel file

  1. In the Projects view, click the model’s  Database item.
  2. In the Properties view, expand the Export section.

  3. Before doing the export, you need to set it up. If you have not done this yet, click the Export settings... label.
  4. You will see the Export settings dialog. Using the Browse... button, select the MS Excel file where you want to write the information. The button to the right allows to switch between the absolute and relative file path. The path to the file will be displayed in the Export to field of the DB properties’ Export section.
  5. Below, in the Select tables you want to export list, select the check boxes for the database tables you want to export to MS Excel workbook.
  6. We have finished defining the export settings. Click the OK button.

  7. If you want to perform the data export, in the Database properties, click the label Export tables to Excel.

  8. If you want data to be exported every time when the model execution is finished or terminated by the user, select the check box Export tables at the end of model execution.

If the selected MS Excel file is currently opened, you will see the error message. Close the MS Excel application, and repeat the export scenario.

If the export was successful, you can open the MS Excel file, and see that AnyLogic added new worksheet(s), one per each exported database table, and wrote the data there.

To export data from the model’s database into an external Excel file programmatically

You can command AnyLogic to export data from its internal database to an external Excel file, using any of the available code fields, for example, a function body or agent action.

To do that:

  1. Use the Database class constructor to specify the location of the Excel file you need, for example:
    Database myFile = new Database(this, "A DB from Excel", "D:FilesmyDBFile.xls") In the Database constructor, the first argument (this) specifies the database owner (an agent or experiment), the second ("A DB from Excel") is the flavor text (an arbitrary name of the database for your convenience), and the last one is the file name.
    You don’t have to specify the absolute path all the time — the relative path will do, too. Moreover, you can use various UI elements to allow users to specify different files. Consider using File Chooser for that purpose.
  2. To access the model’s database, create an instance of the ModelDatabase object:
    ModelDatabase modelDB = getEngine.getModelDatabase();
  3. Finally, call the exportToExternalDB() function:
    modelDB.exportToExternalDB("Internal DB - Excel Sheet 1", myFile.getConnection(), "Sheet 1", true, false);
    This function uses a database Connection, which is retrieved by the getConnection() method call. The first argument ("Internal DB - Excel Sheet 1") specifies the name of the internal database table you want to export, while the third ("Sheet 1") specifies the name of the target table within the external data source (the Excel file). true in the 4th argument commands AnyLogic to wipe the existing contents from the target table before executing the call, and false identifies that the copy action would not be considered a proper database transaction (which may cause performance issues in some cases).
    If you need to export data from multiple tables, repeat the function call and specify a different source sheet in the same file, or a different file.
  4. That’s it — upon the execution of the code described above, the Excel sheet will be populated with values.
    You can call the myFile.disconnect() function to terminate the database connection and proceed with the model as usual.
How can we improve this article?