To export data from AnyLogic database tables to MS Excel file
- In the Projects view, click the model’s Database item.
In the Properties view, expand the Export section.
- Before doing the export, you need to set it up. If you have not done this yet, click the Export settings... label.
- 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.
- 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.
We have finished defining the export settings. Click the OK button.
If you want to perform the data export, in the Database properties, click the label Export tables to Excel.
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 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
To do that:
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.
To access the model’s database, create an instance of the ModelDatabase object:
ModelDatabase modelDB = getEngine.getModelDatabase();
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.
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?