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 NewASP.NETCore 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

    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 sales-order-detail.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.

    HomeController.cs file |Reporting Tools

    HomeController.cs file

  2. To read the report file from the application, use the IHostingEnvironment interface by importing the Microsoft.AspNetCore.Hostingnamespace 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. Create a variable named reportPath and assign the path value that has the report.

  2. Create an instance for the FileStream using the report path.

  3. Create the memory stream to copy the value from the file stream 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/sales-order-detail.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();
    
    1. Create an object for the Bold Reports Writer.

       BoldReports.Writer.ReportWriter writer = new BoldReports.Writer.ReportWriter();
      
      1. Create a list for Bold Reports parameters named userParameters and add a parameter for the “InvoiceID” with its value set to the provided invoiceID.

         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 }
         });
        

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

      2. Create a variable to store the file name, file type, and export format for the exported file.

         string fileName = null;
         WriterFormat format;
         string type = null;
        
      3. Add the following code in the Export method to set the file name, type, and formats when saving the invoice report. We also allow users to choose their preferred format, such as PDF, Excel, or Word, for downloading the report.

         Writer.LoadReport(reportStream);
        
      4. Set the report parameters using the userParameters list to customize or filter the report’s content based on given Invoice ID.

         writer.SetParameters(userParameters);
        
      5. To ensure that the generated report can be processed correctly by the Report Writer, it’s essential to set the memory stream’s position to zero (0). This ensures that the Report Writer can access, read properly, and generate the exported filewithout any issues.

         MemoryStream memoryStream = new MemoryStream();writer.Save(memoryStream, format);
         // Download the generated export document to the client side.memoryStrea    `m.Position = 0;
        
      6. Create the FileStreamResult with an exported report’s memory stream value to get the exported document on the client side. Assign the file name value of FileDownloadName to be used for download.

    FileStreamResult fileStreamResult = new FileStreamResult(memoryStream, "application/" + type);
    fileStreamResult.FileDownloadName = fileName;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.

    Index.cshtml File | Reporting Tools

    Index.cshtml File

    1. 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 add the Generate button.

         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

Register the license token to remove the license validation message. In this setup, we are using an online license token.

License tokens can be generated in the subscription section of the Bold Reports accounts page. Open the Program. cs and enter the code snippet to register the license token.

  //Register Bold license
    Bold.Licensing.BoldLicenseProvider.RegisterLicense("YOUR LICENSE KEY");
    // Code that runs on application startup
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}

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, and you will see the Generate button and the option to select the export file type. The invoice will be exported in PDF format when a user clicks the Generate button.

Report Exported in PDF Format| Reporting Tools

Report Exported in PDF Format

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

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.

fileName = "invoice-report.docx";
type = "docx";
format = WriterFormat.Word;

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 Twitter, Facebook, LinkedIn, Pinterest, and Instagram pages.