Chat Icon
Webinar A Deep Dive into Report Parts for Accelerated and Reusable Reporting - Thursday, February 29, 10:00 A.M. ET SIGN UP NOW
Created with Sketch.
Login-icon
Producing Invoice Reports with ASP.NET Core Application
Producing Invoice Reports with ASP.NET Core Application

Producing Invoice Reports with ASP.NET Core Application

Welcome to our blog on producing invoice reports with your ASP.NET Core application. Reporting is an aspect of many business applications, but generating reports often involves writing complex code. In this blog post, we’ll demonstrate how to create invoice reports effortlessly.

While there are various ways to generate reports in ASP.NET Core, we’ll focus on utilizing Bold Reports, a powerful tool that streamlines the report generation process. Bold Reports provides a user-friendly designer for creating report templates in RDL (report definition language) format. It not only simplifies the report design but also allows for quick adjustments as needed. With the built-in Report Writer, you can easily produce reports in various formats such as PDF, Excel, and CSV, ensuring a seamless experience for your users.

Before we dive into the details, if you haven’t already, we recommend checking out our previous blog post, ”Creating Invoice Reports with Bold Report Designer”. In this blog, we explore the steps to integrate the Bold Reports Writer for your reporting needs in ASP.NET Core applications.

Let’s get started with producing invoices using Bold Reports in ASP.NET Core.

Create a New ASP.NET Core Web Application

I will be using Visual Studio 2022 to produce an invoice report as a PDF, but the steps should be similar for other versions of Visual Studio:

  1. First, open Visual Studio 2022. Click Create a new project.
  2. In the Create new project dialog, select the NET Core Web Application (Model-View-Controller) template. Then, click Next.
  3. In the Configure your new project dialog, change the project name, and then click
  4. In the Additional Information dialog, set the target framework to .NET 6.0. Then, click.
    Additional Information Dialog | Reporting Tools
    Additional Information Dialog

     

  5. The new ASP.NET Core web application will be created.

Install NuGet packages.

In this section, we will install the necessary NuGet packages in the application. To install the NuGet packages, follow these steps:

  1. Right-click the project in the Solution Explorer tab and choose Manage NuGet Packages
    Choose Manage NuGet Packages |Reporting Tools
    Choose Manage NuGet Packages
  2. In the Browse tab, search for the Boldreports.NET.Core package and install it in the core application. The dependent packages will be installed automatically when installing the Boldreports.NET.Core package.
  3. Create a folder named Resources in the wwwroot folder in this application. This is where the RDL reports will be kept.
    Create Resources Folder | Reporting Tools
    Create Resources Folder
  4. In this setup, we add the invoice-report.rdl file to the Resources folder.

­­­Add an API method to generate the expected document in the controller

  1. Open the HomeController.cs file from the Controllers folder.
  2. To read the report file from the application, use the IHostingEnvironment interface by importing the Microsoft.AspNetCore.Hosting namespace and injecting IHostingEnvironment through dependency injection
    // IWebHostEnvironment used with sample to get the application data from wwwroot.
    private Microsoft.AspNetCore.Hosting.IWebHostEnvironment _hostingEnvironment;
    
    // IWebHostEnvironment initialized with controller to get the data from application data folder.
    public HomeController(Microsoft.AspNetCore.Hosting.IWebHostEnvironment hostingEnvironment)
    {
        _hostingEnvironment = hostingEnvironment;
    }
    
  3. Next, add the Export method to the controller to export using the HTTP Post action.
    [HttpPost]
    public IActionResult Export (string invoiceID)
    {
    }

In the Export method, we are going to read the report file as a stream and use it with the Report Writer. Here are the steps to do that:

      1. Initialize the report path with the RDL report location.
      2. Read the report template in the application using FileStream and the report path.
      3. Create a memory stream to copy the report template content and use it with the Report Writer.

        NoteThis copy process will be helpful to avoid file access problems in the server while running the application on the server

        string reportPath = Path.Combine(_hostingEnvironment.WebRootPath, “Resources/invoice-report.rdl”); 
        FileStream fileStream = new FileStream(reportPath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
        MemoryStream reportStream = new MemoryStream(); 
        fileStream.CopyTo(reportStream); 
        reportStream.Position = 0; 
        fileStream.Close();
      4. Create an instance for the Bold Reports Report Writer.
        BoldReports.Writer.ReportWriter writer = new BoldReports.Writer.ReportWriter();
      5. Next, load the report in the Writer.
        writer.LoadReport(reportStream);
      6. We need to generate the report based on the Invoice ID number. So, create a parameter list for the report and assign the InvoiceID parameter value to the report using the set parameters.
        List<BoldReports.Web.ReportParameter>userParameters = new 
        List<BoldReports.Web.ReportParameter>()
        
        // Add the desired parameters
        userParameters.Add(new BoldReports.Web.ReportParameter()
        {
            Name = "InvoiceID",
            Values = new List() { invoiceID }
        });
        writer.SetParameters(userParameters);
        

        NoteThe Name value should be same as the parameter name in the report.

      7. Generate the PDF file using the Save option by specifying the PDF format.
        MemoryStream memoryStream = new MemoryStream();
        writer.Save(memoryStream, WriterFormat.PDF);
        // Download the generated export document to the client side.
        memoryStream.Position = 0;

        NoteThe generated PDF report is saved as a stream, which will be provided for the FileResultStream to download for the client. The FileResultStream will read the stream again for the process, so the memory stream’s position is set back to ‘0’ after the Save action of the Report Writer.

      8. Next, we need to return the generated file with FileStreamResult. For that, create the FileStreamResult with the exported report’s memory stream value to get the exported document on the client..
        FileStreamResult fileStreamResult = new FileStreamResult(memoryStream, "application/" + "pdf");
        fileStreamResult.FileDownloadName = "invoice-report.pdf";
        return fileStreamResult;

Implementation of API on the client side

On the client side, add an option to select the export file type and a Generate button to get do­­cuments based on the selection of the export type.

    1. Open the Index.cshtml file from the Views -> Home folder.
    2. Next, replace the existing code snippet in the application with the following code. This code will create an interface that allows users to enter the invoice ID and a Generate button that will download the PDF file when it’s clicked. The Export API is mapped with the Form using the Post action.
      @{
          Html.BeginForm("Export", "Home", FormMethod.Post);
          {
              <div class="Common">
                  <div class="tablediv">
                      <div class="rowdiv">
                          <label id="design">
                              Provide an Invoice ID to export the report with the specific Invoice ID.
                              <br />
                              <br />
                          </label>
                      </div>
                      <div class="rowdiv">
                          <div class="celldiv" style="padding:10px">
                              <label>
                                  <strong> Invoice ID :</strong>
                              </label>
                              <input id="invoiceID" type="text" name="invoiceID" style="margin-left: 15px" />
                              <br />
                              <br />
                              <input class="buttonStyle" type="submit" name="button" value="Generate" style="width:150px;" />
                          </div>
                      </div>
                  </div>
              </div>
              Html.EndForm();
          }
      }

Register the license key

By default, the report generated using the Report Writer will show a license message. To remove this message, we need to register either an online license token or an offline license key.

Click here to learn how to register a license token using online license tokens and offline license keys.

Build and run the application

Now, everything is ready to load the Report Writer. Build and run the application. You will see the text box to enter the Invoice ID and the Generate button. When you provide the Invoice ID to the application and click the Generate button, the PDF file will be generated with the give an Invoice ID.

Report Exported in PDF Format| Reporting Tools
Report Exported in PDF Format

When opening the downloaded file, you will see the exported invoice PDF file for the provided Invoice ID.

Rendered RDL Reports from the ASP.NET Core Application | Reporting Tools
Rendered RDL Reports from the ASP.NET Core Application

Export in different formats

If you want the report in Word format, you can replace with the below code in the IActionResult export method. You will see the report is exported.

...
writer.Save(memoryStream, WriterFormat.Word);
...
FileStreamResult fileStreamResult = new FileStreamResult(memoryStream, "application/" + "docx");
fileStreamResult.FileDownloadName = "invoice-report.docx";
...

When opening the downloaded file, you will see the exported Word file.

Exported File | Reporting Tools
Exported File

Conclusion

Exporting RDL invoices in an ASP.NET Core application is a simple process using the Report Writer component. Bold Reports offers a comprehensive guide for exporting RDL files and linked reports in your ASP.NET Core reporting application.

To learn more about the ASP.NET Core tools in Bold Reports, look through our documentation. To experience their features live, check out our demo samples.

If you have any questions, please post them in the comments section. You can also contact us through our contact page, or if you already have an account, you can log in to submit your support question.

Bold Reports offers a 15-day free trial with no credit card information required. We welcome you to start a free trial and experience Bold Reports. Try it and let us know what you think!

Stay tuned for announcements about new releases by following us on our TwitterFacebookLinkedInPinterest, and Instagram pages.

Tags:

Share this blog

Leave a Reply

Your email address will not be published. Required fields are marked *