With the number of online businesses soaring, being able to produce reports on how a business is performing is essential. The reports could vary from a sales report, to a customers report. And businesses want to be able to create these on demand. But how can you automate these reports in .NET?

Popular File Types

A file type that is popular for business is spreadsheets. Spreadsheets can be created using Excel, or you can create a Comma Seperated Values (CSV) file. Businesses also like reports being produced and this can be done with a Portable Document Format (PDF) file. PDF files makes it easy to view and share a report. But how do you create these in .NET and how can you do it for free? I'm going to show you how with some great NuGet packages.

CSVHelper

CSVHelper does what the name suggests. It helps read and write CSV files. One of the benefits of CSVHelper is that it can create a file by reading an IEnumerable object. As a result, if your data is in a list, you can just pass the list into CSVHelper without any additional coding.

In the following .NET Core console application example, we have created a CSV file inside a MemoryStream object and stored the bytes in a ByteArray object. We then read the CSV file and output it's contents into a console application. Of course, you can store the CSV file in a physical CSV file on your hard disk.

Before you can work with this example, you need to make sure that you add the CSVHelper NuGet package to your Visual Studio project. For the purposes of this example, we are using version 12.1.2.

The output of the console application reads:

---
Id - 1
Name - Hello
---
Id - 2
Name - Goodbye

EPPlus

EPPlus allows you to manage Excel files, allowing you to read or write them. Like CSVHelper, you can create a file by reading an IEnumerable object. But, unlike CSV files, you can also add multiple worksheets, meaning you can have more than one data grid in one single Excel file.

The .NET Core console application example below shows how to write and read an Excel File. Like with our CSV example, we have written the contents of the Excel file to a MemoryStream byte array and then we are reading it to output the results. You can also save your Excel document in a physical file.

You must remember to add the EPPlus NuGet package. The example below has been written using version 4.5.3.2 of EPPlus.

The output of the console application reads:


First Row, First Cell - 1
First Row, Second Cell - Hello
Second Row, First Cell - 2
Second Row, Second Cell - Goodbye

SelectPDF

PDF documents are good for sharing and viewing documents. With SelectPDF, you can create your own PDF document by passing in HTML code. You can also take advantage of CSS to style the document in the way that you want it.

And that's not all! You can also create a PDF of an active web page, just by typing in the URL of the website you wish to create. And that's what we are looking at in our .NET Core console application example below.

Just remember to add the Select.HtmlToPdf.NetCore NuGet package, and the example below is using version 19.1 of the Select.HtmlToPdf.NetCore package. Incidentally, there is also a Select.HtmlToPdf NuGet package, that supports .NET Framework.

This example generates an example HTML page by putting in HTML code. It also shows how the homepage of the Round The Code website looks on 22nd September 2019. You can view these files below.

Conclusion

As you can see from these examples, they are not too difficult to set up these reports, and could save your organisation loads of time by automating the them. If you are currently paying someone to produce your reports manually, then you really should take advantage of these tools.

About Dave

I am a .NET developer, building web applications in .NET Framework and .NET Core with a SQL Server database.

Some of the .NET packages I have used include Entity Framework and MVC.

I've also used many JavaScript frameworks such as React and jQuery, and have experience building CSS in SASS.

Related Tags

CSVHelper
EPPlus
SelectPDF

Twitter Feed