CSV (Comma Separated Values) files are widely used for exchanging data among applications. When you want this data to be translated into presentable and meaningful information, you need to convert it into some other format. In one of our posts, we have seen how to convert CSV data in Reports using Java. This article will guide you to convert CSV data into PDF and MS Word DOC/DOCX reports using C# using a simple template.
The following topics are covered below:
Report Generation .NET API
GroupDocs.Assembly provides the .NET reporting API to automate the report generation. In this article, I have used this GroupDocs.Assembly for .NET for generating reports from the selected CSV data and a TXT format template. It also supports multiple data sources like JSON, XML, and also from MS Word, Excel, and PowerPoint files as data files.
You can download the DLLs or MSI installer from the downloads section or install the API in your .NET application via NuGet.
PM> Install-Package GroupDocs.Assembly
Generate PDF Report from CSV Data in C#
Let’s begin by transforming the comma-separated data into a presentable PDF. The following steps will guide you to convert the CSV data into a formatted PDF report.
- Load CSV data source.
- Define template according to the CSV data.
- Provide CSV data source and template to simple method to generate PDF report.
CSV Data
To get the PDF report, I will be using the following sample CSV data of different individuals along with their respective data of ages and date of birth.
Name,Age,Birth
John Doe,32,4/1/1989 16:00
Jane Doe,29,1/31/1992 7:00
John Smith,53,3/8/1968 13:00
Template
The next step would be to define the template in TXT or DOCX format. The following is the template that is used in this example and allows iterating the list of persons with their details.
<<foreach [in persons]>>Name: <<[Name]>>, Age: <<[Age]>>, Date of Birth: <<[Birth]:"dd.MM.yyyy">> <</foreach>> Average age: <<[persons.Average(p => p.Age)]>>
Steps to Generate PDF Report from CSV in C#
The following steps guide about converting the CSV data into a PDF report according to the defined template using C# with the .NET Reporting API.
- Define CSV data file, template file, and the PDF output file paths.
- Instantiate CsvDataSoure with CSV data file and loading options.
- Create DataSourceInfo with the defined data source.
- Using the DocumentAssembler, call the AssembleDocument method with defined template file, output file and DataSourceInfo to get the PDF report as output.
The following code shows how to convert CSV data to PDF report in C#.
Generate MS Word Report from CSV data in C#
If you want any manual editing in the automated generated report, you can also get the output as an MS Word document. The process will be very similar to the above PDF report generation. The following steps will guide to generate the DOC/DOCX report from the CSV data:
- Load the CSV data from file.
- Defining the template in TXT or DOCX format.
- Set the output report document format as DOC/DOCX.
- Call the AssembleDocument method to generate MS Word DOCX report from the CSV data.
The following code shows how to convert CSV data into a DOCX report using C#.
Get a Free API License
You can get a free temporary license in order to use the API without the evaluation limitations.
Conclusion
To conclude, you have learned to convert the CSV data into PDF and MS Word reports using C#. You must now be confident building your own .NET report generator application by converting CSV data into PDF format. Similarly, you can also generate reports using other data sources like JSON and XML.
For more about the API, you can visit documentation and the GitHub repository. In case of further queries and ambiguities, contact the free support on the forum.