Downloading the report as an Excel document in MVC
Previously, we already examined an example of displaying a printing dialog of a report in HTML and PDF formats. The aim of this article is to show how to save reports in the desired format, providing an example of Excel.
We use the MVC web application.
There is a button, which is provided to save a report. Add it to the web page Home. For this, open the Index.cshtml file in the Views folder.
Place the following code in the desired location:
There "Save" - is the name of the handler in the controller. "Home" - is the controller.
Now add the handler "Save" to the Home page controller. For this, open the HomeController.cs file in the Controllers folder:
The handler will look like this:
Let us take a look at the procedure:
- Create an instance of an object of WebReport;
- Create an instance of an object of DataSet to work with date;
- Load the xml datebase file;
- Register the source of the date in the report object;
- Load the report template in WebReport object;
- Save the report in Excel format.
Now you need to add the handler in the Web.config file, which is located in the root of the project:
Run the application to see the button:
Click on it. The program will display a dialog to save the report file in xlsx format.
After downloading the report, it will be open:
Summing up, we can conclude that using the shown application, it is possible to save your report in different formats, available for exporting reports. Using the shown code allows users of the web application to download a report in the desired format without displaying the report itself.