This section describes an example of importing data from an Excel spreadsheet into a system database. All sources and a working example can be found in the A2v10.Web.Sample repository on Github.
In order for the system to know how to process the downloaded files, their format must be predefined in advance. To do this, it is recommended to offer users to download sample files.
We will overview importing goods. Therefore, all the main files are in the /sample/catalog/product.
The first step is to add the Import button to the product list toolbar.
index.view.xaml file fragment
The button simply calls the import dialog.
Let's not forget to indicate this dialog in the file /catalog/product/model.json.
model.json file fragment
We don't need a data model for this dialog, so we specify an empty string in the
model
property. Other scenarios are possible, which, for example, will
show the imported records first. In this case, the model will be needed.
For the rest, this is the standard platform dialog. It uses the import.template
template.
import.dialog.xaml file fragment
In this dialogue, two controls are interesting. Firstly UploadFile.
This item will display a field for uploading a file (by clicking or dragging). The content of the selected file will be sent
to the server and processed by the /catalog/product/import
command from the files section of the
model.json. After downloading and processing the file, the delegate specified in the
Delegate
property will be called.
The second control is a link that will allow the user to download a sample file for upload. Download command allows downloading a file from a special _files folder.
After uploading the file to the server, the command from the files section of the model.json file will be called.
The parse
command type indicates that the file needs to be processed, and the type
of processing (parse
) indicates that the system expects a Microsoft Excel file.
Please note! Only .xlsx files (Excel 2007 and older) are processed. Old versions of tables in .xls format are not supported.
After the system receives the file from the client, it will try to parse it and receive a simple table from it. If the command specifies a model (the model property), then an object is created with a single Rows property (the name is predefined!) which represents an array of rows from an Excel file. After that the system will attempt to save this object as a standard model, by calling the .Metadata и .Update procedures.
As a result of processing, a model, which will be returned from the .Update procedure, will be returned to the client.
Stored procedures and data types can be examined by looking at the import.sql file.
After the data is imported into the database, it is necessary to have some way to notify the calling code of this.
The easiest way to do this is to use custom events. In the dialog's Model.load event handler, it is possible to store a reference to the calling context in a variable. In the delegate that will be called after the download completes, you can initialize an event in the calling context.
Fragment of the import.template.ts dialog template file
When the $product.import.done event is received, the index page is simply reloaded.
Fragment of the import.template.ts dialog template file
In order to import data from a single context (in which case no events are needed), you can use the InlineDialog control.
We will leave the implementation of this method as an exercise.