Exporting data to MS Excel workbook
- 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 switches the path between absolute and relative. 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 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.
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:\Files\myDBFile.xls")
ModelDatabase modelDB = getEngine().getModelDatabase();
modelDB.exportToExternalDB("Database table for exporting", myFile.getConnection(), "Sheet 1", false , false );
- The first argument ( "Database table for exporting" ) specifies the name of the internal database table you want to export.
- The second argument specifies a database Connection , which is retrieved by the getConnection() function call.
- The third argument ( "Sheet 1" ) specifies the name of the target table within the external data source (the Excel file). Note the table (the sheet) must exist within the database file and have the specified name, and its header row must name columns in the same way they are named within the table in the AnyLogic model database.
- The fourth argument may be used to command AnyLogic to wipe the existing contents from the target table before executing the call, though as of now, this functionality is not supported for the Excel table. So, in our case, we are specifying false , so the new records will complement the existing content of the external database rather than overwrite it.
- false as the fifth argument identifies that the copy action would not be considered a proper database transaction (which may cause performance issues in some cases).
For more information regarding this function, as well as the other internal database functions, see the following reference: Database functions.