How to create a report from web application code

2022-02-16

How to create a report from web application code

Sometimes creating a report can turn into to be a real headache. For example, when you need to manage the content of the report based on the conditions in the program. It is easier to do this in the program code than to transfer parameters to the report and develop the report script logic.

Today we will look at how to create a report from the application code, not a simple desktop application, but the ASP.NET Core MVC web application. We have already looked at how to create a report in the code of a WinForms application. However, web applications are much more popular now and many FastReport users would like to use all the features of this report generator in them.

Let's take a look at how to create a report template and run it without a report designer. First, we need an ASP.NET Core MVC application project. To work with the FastReport.NET report generator, you need to install its libraries using the NuGet package manager.

Since the library packages are located on the local disk (in the directory of the installed program in the Nuget folder), we need to add a local package source. To do this, click on the gear icon in the upper right corner of the package manager and add a new source that will link to the local folder with your packages (with the .nupkg extension):

Adding a new local package source

Now you can select the added package source from the drop-down list and install the packages.

Packages installed from a local source

To use the added libraries in the program code, you must first connect them in the Startup.cs file. Let's add a string in the configuration method:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
 {

app.UseFastReport();

}

Now you can go to programming. Our MVC project has:

  1. Controllers that contain the logic of the server-side of the application.
  2. Views display information for the client.
  3. Models are responsible for working with data (reception, storing, transformation and recording).

Our simple example will only require a controller and a view. Let's change the Index method in the controller. Our application, which is automatically generated from a template, already has a ready controller - HomeController:

using FastReport.Web;
using FastReport;
using FastReport.Table;
using System.Data;
using FastReport.Data;
using System.Drawing;
using FastReport.Utils;
 
 public IActionResult Index()
 {
 //Create a web report object 
 WebReport report = new WebReport();
 
// Create a web page object
 ReportPage page = new ReportPage();
 page.Name = "Page1";
 
//Add a page to the report 
 report.Report.Pages.Add(page);
 
 //Create a data source
 DataSet ds = new DataSet();
 ds.ReadXml("C:/Users/FR/source/repos/WebApp/WebApp/Reports/nwind.xml");
 
 //Register the data source in the report
 report.Report.RegisterData(ds);
 
 //Include the table in the report
 report.Report.GetDataSource("Products").Enabled = true;
 
//Create a data band
 DataBand dataBand = new DataBand();
 dataBand.Name = "DataBand";
 
 //Get the table from the report data source
 DataSourceBase data = report.Report.GetDataSource("Products");
 // Initialize the data source 
 data.Init();
 //And we get the first row of data
 data.First();
 
 //Add a data band to the page 
 page.Bands.Add(dataBand);
 
 //Create a Table object
 TableObject table = new TableObject();
 table.Name = "Table1";
// Set the number of rows - the number of rows in the source + title
 table.RowCount = data.RowCount+1;
 // Set the number of columns
 table.ColumnCount = 2;
 
// We set the autosize of the first column where the names of the products will be displayed
 table.Columns[0].AutoSize = true;
 // Set the titles in the first row of the table
 table[0, 0].Text = "Product Name";
 table[0, 0].Border.Lines = BorderLines.All;
 table[1, 0].Text = "Unit Price";
 table[1, 0].Border.Lines = BorderLines.All;
 
 int i = 1;
 //Loop through all rows in the data source
 while(data.HasMoreRows)
 { 
 //Display product names
 table[0, i].Text = data["ProductName"].ToString();
 table[0, i].Border.Lines = BorderLines.All;
 //Display prices
 table[1, i].Text = data["UnitPrice"].ToString();
 table[1, i].Border.Lines = BorderLines.All;
 
//Create a picture object
 PictureObject picture = new PictureObject();
 picture.Bounds = new RectangleF(40, 0, Units.Centimeters * 0.5f, Units.Centimeters * 0.5f);
 picture.CreateUniqueName();
 //We choose a picture to display in the table cell depending on the value of the price
 if ((decimal)data["UnitPrice"] < 20)
 {
 picture.Image = Image.FromFile("C:/Users/FR/Downloads/28.png");
 }
 else 
 {
 picture.Image = Image.FromFile("C:/Users/FR/Downloads/29.png");
 }
 
 picture.LoadImage(); 
//Load the image into the object
 picture.Parent = table[1, i]; 
//Assign the parent object for the picture - the table cell 
 i++;
 data.Next(); 
//Take the next record from the source
 }
 
 dataBand.Objects.Add(table); 
//Add the table object to the report page
 
 ViewBag.WebReport = report; 
//Return the report to the view
 return View();
 }

It is clear from the comments to the code that we create report objects manually and build their hierarchy. Now we have created a tabular report with pictures in cells. The desired picture is selected depending on the price value. This is just a typical example when you may need to create a report from the program code.

Now let's change the Index.cshtml view. Let's leave just one line:

@await ViewBag.WebReport.Render()

That is all. You can run the application and see our report:

This is what a finished report looks like

Let’s summarize. In fact, creating a report from the code of a web application is no different from a regular desktop application. On the one hand, it requires a deep knowledge of the structure of the report and its objects, which means that a qualified specialist should create it. On the other hand, sometimes this is the only way to create a report with complex logic. So, don’t take this method as the main one, but it will help you in the hour of need.

August 12, 2024

How to build and install the Postgres plugin in FastReport .NET

This article describes how to connect to the database using the FastReport .NET plugin for the report designer from Visual Studio via the NuGet server.
August 08, 2024

How to install FastReport .NET and its components on Windows

Step-by-step instructions for online and manual installation via the FastReport registration code.NET and its components in Windows.
November 09, 2023

How to make a report from C# to FastReport Cloud

In this article, we will look at how to create reports in FastReport Cloud using SDK and export them to any convenient format.
Fast Reports
  • 800-985-8986 (English, US)
  • +4930568373928 (German)
  • +55 19 98147-8148 (Portuguese)
  • info@fast-report.com
  • 66 Canal Center Plaza, Ste 505, Alexandria, VA 22314

© 1998-2024 Fast Reports Inc.