Export data into PDF in ASP.NET MVC

Export data into PDF in ASP.NET MVC

In this article we will discuss how to export PDF in ASP .NET MVC. Here we discuss export into PDF without using any 3rd party libraries. Please go through our previous article Export data into Excel in ASP.NET MVC. If are new to in ASP .NET MVC for step by step access please explore ASP .NET MVC Series.

How to export PDF in ASP.NET MVC

Create the Project in ASP .NET MVC and connect database and fetch the record using Dapper ORM and export the data into PDF.

  • Create a ASP.NET MVC application using Visual Studio.
  • We select as Empty project with targeting to ASP.NET MVC.
  • Create a Controller inside the controllers folder, let’s name as “EmployeeController.cs”
  • The Employee Db contains a employee table that holds the employee information. The script are find below;
Database Script
Employee table script
Employee.cs

Add a employee property class in Models folder.

Employee Property Class
Connection string

Declare the connection string in Web.config like below;

 <connectionStrings>
    <add name="ConnectionString" connectionString="Data Source=corespider;Initial Catalog=EmployeeDb;Persist Security Info=True;User ID=sa;password=**********;Pooling=false;Connect Timeout=300000" providerName="System.Data.SqlClient" />
  </connectionStrings>

To connect with database and fetch the record, her ewe use Dapper ORM.

What is Dapper ?

Dapper is an object-relational mapping product for the Microsoft .NET platform: it provides a framework for mapping an object-oriented domain model to a traditional relational database. Its purpose is to relieve the developer from a significant portion of relational data persistence-related programming tasks.

Add Dapper package in Nuget
export data into PDF in ASP.NET MVC-Dapper
Dapper Package
EmployeeRepository.cs

Add another class in Models folder to retrieve the employee information from database.

EmployeeRepository.cs
Code Explanation
  • Here We create the IDbConnection that hold the connection string information.
  • Using Dapper ORM fetch the employee tables.
  • In real-time application you may use Stored procedure instead of text.
  • Dapper ORM directly return the list of employee.
EmployeeController.cs
Employee information
Code Explanation
  • Here we create object of the employee repository class.
  • In the Index() we fetch the employee list and return to the view.
Index.cshtml
Code Explanation
  • Here we use scaffold technique to create list of employees.
  • Added a button to Export the data into PDF.
  • Export is the action method and Employee is the Controller.

Export Action method to convert PDF

Code Explanation
  • In the export Post method we fetch the employee information.
  • To export data into PDF we use Gridview controls that are in System.Web.UI.WebControls namespace.
  • In Gridview controls hold the employee list.
  • In the string writer and string Reader st the PDF using HTMLWorker.
  • Response content type as PDF and finally append the PDFdoc that export into PDF.

Run the application and see the output

export data into PDF in ASP.NET MVC-export pdf
Export Data

After click the export button it convert the above data into PDF like below;

export data into PDF in ASP.NET MVC-PDF
Export PDF
Export PDF
Export to PDF in ASP NET MVC

Conclusion

This article explained how to export PDF in ASP.NET MVC . In this example we use Dapper ORM to fetch the data from database and using the data we export into PDF using Gridview controls that are available in System.Web.UI.WebControl namespace. Please free to feel if have any suggestion or feedback about this article.

Export data into Excel in ASP.NET MVC
Export-data-into-Excel

In this example we learn how to export the data into MS Excel in ASP.NET MVC without using any 3rd Read more

Export data into Excel in ASP.NET MVC

Export data into Excel in ASP.NET MVC

In this article we learn how to export the data into MS Excel in ASP.NET MVC without using any 3rd party libraries. If you are new to ASP.NET MVC to learn the step by step process please follow the ASP.NET MVC Series.


We take an example to print the dummy data into a ASP .NET MVC page and there we apply to export data into MS Excel.

  • Create a ASP.NET MVC application using Visual Studio.
  • We select as Empty project with ASP.NET MVC
  • Create a Controller inside the controllers folder, let’s name as “CustomerController.cs”
  • Add a customer model class in Models folder.
  • In the “CustomerController” we create a dummy list that contains multiple customers. You can connect with database and fetch the real-time data.
Customer Model Class
Customer Model Class
Customer Controller
Customer Controller
Code Explanation
  • We take a dummy data to return the customer details inside the view page.
  • GetCustomer() holds the customer information.
  • When Index() action method is called it collect the customer information and send to the view.
  • We use the scaffolding technique to generate the list of HTML using customer model class
Customer View
Customer View Page
Code Explanation
  • The customer information is printed using scaffold.
  • We create here a Export button and when it click it generate the Excel.
  • The view screen look like below;
Customer Information
Create Export Post method to convert the data into Excel

As we already discuss we are not using any 3rd party libraries to export excel, here we can simply GridView control of the System.Web.UI.WebControls namespace that is part of ASP.NET Web Form and it do conversion of MS Excel following below steps.

CoreSpider- Export to Excel in ASP.NET MVC
Code Explanation
  • On Export() we fetch the data from customer list.
  • Using help of GridView control of the System.Web.UI.WebControls namespace append the customer data into Gridview.
  • Rendering the GridView conent using HtmlTextWriter and StringWriter.

Everything is okay now, so let’s run the application and see the output like below;

CoreSpider.com - Export Data to Excel
CoreSpider.com – Export Data to Excel

You can see like above image we do code for export data into excel format in ASP.NET MVC without using any 3rd part library, we just use the Grid-view Controls that are present in System.Web.UI.WebControls.

Conclusion

So far this article we discuss how to export the data into MS Excel in ASP .NET MVC without using any 3rd party libraries. If have any suggestion about this article please give your feedback.

Export data into PDF in ASP.NET MVC
Export-Data-into-PDF-SERVICE

In this example we use Dapper ORM to fetch the data from database and using the data we export into Read more

CRUD Operation in ASP.NET Web API with Entity Framework
CRUD-Operation-in-asp.net-Web-api

This blog explain CRUD Operation in ASP.NET Web API with Entity framework and then the web API check using POSTMAN Read more

Worker Service in .NET Core
worker-service

A worker service is a .NET project built using a template which supplies a few useful features that turn a Read more