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):
Now you can select the added package source from the drop-down list and install the packages.
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:
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:
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.