Exporting Reports To Excel With DotVVM Business Pack

In this tutorial, we’ll see how to export data in a DotVVM web application on ASP.NET to Excel.

To achieve this goal, we are going to use DotVVM Business Pack, a premium extension of DotVVM, in this case, to use a GridView control, and export data to excel.

Steps to follow,

  1. Install NuGet package: DotVVM.BusinessPack.Export.Excel.
  2. Implement Excel export functionality: GridView and an Export() method.
  3. Run the application and view the results.

Install NuGet package

To use the export to an Excel file functionality, we need to install a NuGet package in addition to the DotVVM Business Pack:

Install-Package DotVVM.BusinessPack.Export.Excel

Note
For CSV files, we’ll need to install DotVVM.BusinessPack.Export.Csv package instead.

Implement Excel export functionality

To implement the export data to excel functionality, in a view we could have something like this:

First, we need an instance of a GridView control, and it must be identified with an ID:

<bp:GridView ... ID="grid">

And then, we need to call the method that will be responsible for exporting the data to Excel, we can do that by adding a button:

<bp:Button Text="Export" Click="{command: Export()}" />

Now, we have to implement the Export() method with the functionality to export excel. For this goal, we need to define the following:

  • Get GridView control by its Id.
  • Create the data set for export.
  • Load data in the data set.
  • Export data configurations (for example: cells format, colors, etc).
  • Configure export settings (if needed).
  • Return the file to the user

The method would be something like this:

With that in mind, that will be all we need to do.

Run the application and view the results.

To see in action this functionality on DotVVM, we will see an example application, where the data is randomly generated and displayed in a GridView with a button to export the data with the process previously shown.

The source code of this example can be viewed and downloaded here (BPSamples.GridViewExporting): https://github.com/riganti/dotvvm-samples-businesspack4.

Thanks for reading.

I hope you liked the article. If you have any questions or ideas in mind, it'll be a pleasure to be able to communicate with you and together exchange knowledge with each other.

See you on Twitter / esDanielGomez.com!

Regards!