Producing Invoice Reports with ASP.NET Core Application
Tags: boldreports, aspnetcore, embeddedreporting, embeddedreportingtools
Published: 2023-11-15 07:26:16

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](https://www.boldreports.com/blog/create-an-invoice-report-with-bold-reports/?utm_source=dev&utm_dev=partner_blog&utm_campaign=dev_producinginvoicereportwithaspnetcore_cy23).” 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.

![Generate invoice reports in your ASP.NET Core application with our easy-to-use guide. Learn how to create and customize reports, export them to different formats, and more.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g4ppwofq4utwl8enxx5i.png)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**

![Generate invoice reports in your ASP.NET Core application with our easy-to-use guide. Learn how to create and customize reports, export them to different formats, and more.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pgj0kjpa1b4wbaq5z45b.png)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.

![Generate invoice reports in your ASP.NET Core application with our easy-to-use guide. Learn how to create and customize reports, export them to different formats, and more.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5gr9zusm20emevh4tbe8.png)Create Resources Folder

4. In this setup, we add the [sales-order-detail.rdl](https://raw.githubusercontent.com/boldreports/resources/master/video-tutorial/Reports/sales-order-detail.rdl) file to the Resources folder.
In this setup, we add the [sales-order-detail.rdl](https://raw.githubusercontent.com/boldreports/resources/master/video-tutorial/Reports/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.

![Generate invoice reports in your ASP.NET Core application with our easy-to-use guide. Learn how to create and customize reports, export them to different formats, and more.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mvmus45egwwrkxzw2c7k.png)HomeController.cs file

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

**Note**
This 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();

```

4. Create an object for the Bold Reports Writer.

```
BoldReports.Writer.ReportWriter writer = new BoldReports.Writer.ReportWriter();
```
5. 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 }
});
```
**Note**
The Name value should be the same as the parameter name in the report.

6. 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;
```
7. 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);
```
8. Set the report parameters using the userParameters list to customize or filter the report’s content based on given Invoice ID.

```
writer.SetParameters(userParameters);
```

9. 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;
```
10. 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.

![Generate invoice reports in your ASP.NET Core application with our easy-to-use guide. Learn how to create and customize reports, export them to different formats, and more.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/higxfnfyrolmv2o7jnu6.png)Index.cshtml File

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 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](https://help.boldreports.com/embedded-reporting/licensing/offline-license-key/#asp-net-core/?utm_source=dev&utm_dev=partner_blog&utm_campaign=dev_producinginvoicereportwithaspnetcore_cy23) to learn how to register a license token using online license tokens and [offline license keys](https://www.youtube.com/watch?v=lIiCfvenSzo&t=0s).

## 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.

![Generate invoice reports in your ASP.NET Core application with our easy-to-use guide. Learn how to create and customize reports, export them to different formats, and more.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/foujex8a06hakvw62qfg.png)Report Exported in PDF Format

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

![Generate invoice reports in your ASP.NET Core application with our easy-to-use guide. Learn how to create and customize reports, export them to different formats, and more.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ahzzd9qifkt32vi82wlv.png)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.

![Generate invoice reports in your ASP.NET Core application with our easy-to-use guide. Learn how to create and customize reports, export them to different formats, and more.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l9uj452eexhoprpjinq6.png)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](https://help.boldreports.com/embedded-reporting/aspnet-core-reporting/report-writer/export-ssrs-rdl-report/?utm_source=dev&utm_dev=partner_blog&utm_campaign=dev_producinginvoicereportwithaspnetcore_cy23) 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](https://help.boldreports.com/embedded-reporting/aspnet-core-reporting/?utm_source=dev&utm_dev=partner_blog&utm_campaign=dev_producinginvoicereportwithaspnetcore_cy23). To experience their features live, check out our [demo samples](https://demos.boldreports.com/home/?utm_source=dev&utm_dev=partner_blog&utm_campaign=dev_producinginvoicereportwithaspnetcore_cy23).

If you have any questions, please post them in the comments section. You can also contact us through our [contact page](https://www.boldreports.com/contact/?utm_source=dev&utm_dev=partner_blog&utm_campaign=dev_producinginvoicereportwithaspnetcore_cy23), or if you already have an account, you can [log in](https://boldreports.com/?utm_source=dev&utm_dev=partner_blog&utm_campaign=dev_producinginvoicereportwithaspnetcore_cy23) to submit your support question.

Bold Reports offers a 15-day [free trial](https://www.boldreports.com/pricing/?utm_source=dev&utm_dev=partner_blog&utm_campaign=dev_producinginvoicereportwithaspnetcore_cy23) 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.