Saturday, August 10, 2019

How to Export Webgrid to PDF in MVC4 Application

Step - 1 : Create New Project.

Go to File > New > Project > Select asp.net MVC4 web application > Entry Application Name > Click OK > Select Internet Application > Select view engine Razor > OK

Step-2: Add a Database.

Go to Solution Explorer > Right Click on App_Data folder > Add > New item > Select SQL Server Database Under Data > Enter Database name > Add.

Step-3: Create table for fetch data.

Open Database > Right Click on Table > Add New Table > Add Columns > Save > Enter table name > Ok.
In this example, I have used one tables as below

Step-4: Add Entity Data Model.

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select ADO.net Entity Data Model under data > Enter model name > Add.
A popup window will come (Entity Data Model Wizard) > Select Generate from database > Next >

Chose your data connection > select your database > next > Select tables > enter Model Namespace > Finish.

Step-5: Add a new Controller.

Go to Solution Explorer > Right Click on Controllers folder form Solution Explorer > Add > Controller > Enter Controller name > Select Templete "empty MVC Controller"> Add.

Step-6: Add new action into your controller for data in webgrid.

Here I have added "Index" Action into "Webgrid" Controller. Please write this following code

  1. public ActionResult Index()
  2. {
  3. List<CustomerInfo> allCust = new List<CustomerInfo>();
  4. using (MyDatabaseEntities dc = new MyDatabaseEntities())
  5. {
  6. allCust = dc.CustomerInfoes.ToList();
  7. }
  8. return View(allCust);
  9. }

Step-7: Add view for the Action & design.

Right Click on Action Method (here right click on form action) > Add View... > Enter View Name > Select View Engine (Razor) > Check "Create a strong-typed view" > Select your model class > Add.
[N:B:Please Rebuild solution before add view.] 
Complete View 
  1. @model List<MVCExportWebgrid.CustomerInfo>
  2. @{
  3. ViewBag.Title = "Index";
  4. var grid = new WebGrid(source: Model, canPage: false);
  5. }
  6.  
  7. @* Here I will add some css for Looks webgrid better *@
  8.  
  9. <style type="text/css">
  10. table.gridtable {
  11. font-family: verdana,arial,sans-serif;
  12. font-size:11px;
  13. color:#333333;
  14. border-width: 1px;
  15. border-color: #666666;
  16. border-collapse: collapse;
  17. }
  18. table.gridtable th {
  19. border-width: 1px;
  20. padding: 8px;
  21. border-style: solid;
  22. border-color: #666666;
  23. background-color: #dedede;
  24. }
  25. table.gridtable td {
  26. border-width: 1px;
  27. padding: 8px;
  28. border-style: solid;
  29. border-color: #666666;
  30. background-color: #ffffff;
  31. }
  32. </style>
  33.  
  34.  
  35. <h2>Customer Info</h2>
  36. <div>
  37. @grid.GetHtml(
  38. tableStyle:"gridtable",
  39. columns:grid.Columns(
  40. grid.Column("CustomerID","Customer ID"),
  41. grid.Column("CustomerName","Customer Name"),
  42. grid.Column("Address","Address"),
  43. grid.Column("City", "City"),
  44. grid.Column("PostalCode","Postal Code")
  45. )
  46. )
  47. </div>
  48. <div>
  49. Export Data : @Html.ActionLink("Export to PDF","GETPdf","Webgrid")
  50. </div>
  51.  

Step-8: Add References itextsharp.dll & itextsharp.xmlworker.dll

Here I have added from NuGet.
Go to Solution Explorer > Right Click on References > Manage NuGet Packages...> Search with itextsharp text > Install this 2 dll


Step-9: Add another action into your controller for export webgrid in PDF format.

Here I have added "GETPdf" Action into "Webgrid" Controller. Please write this following code
import following...
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.IO;
  5. using System.Web.Helpers;
  6. using System.Web.Mvc;
  7. using iTextSharp.text;
  8. using iTextSharp.text.pdf;
  9. namespace MVCExportWebgrid.Controllers;
  10. public FileStreamResult GETPdf()
  11. {
  12. List<CustomerInfo> all = new List<CustomerInfo>();
  13. using (MyDatabaseEntities dc = new MyDatabaseEntities())
  14. {
  15. all = dc.CustomerInfoes.ToList();
  16. }
  17.  
  18. WebGrid grid = new WebGrid(source: all, canPage: false, canSort: false);
  19. string gridHtml = grid.GetHtml(
  20. columns: grid.Columns(
  21. grid.Column("CustomerID", "Customer ID"),
  22. grid.Column("CustomerName", "Customer Name"),
  23. grid.Column("Address", "Address"),
  24. grid.Column("City", "City"),
  25. grid.Column("PostalCode", "Postal Code")
  26. )
  27. ).ToString();
  28.  
  29. string exportData = String.Format("<html><head>{0}</head><body>{1}</body></html>", "<style>table{ border-spacing: 10px; border-collapse: separate; }</style>", gridHtml);
  30. var bytes = System.Text.Encoding.UTF8.GetBytes(exportData);
  31. using (var input = new MemoryStream(bytes))
  32. {
  33. var output = new MemoryStream();
  34. var document = new iTextSharp.text.Document(PageSize.A4, 50, 50, 50, 50);
  35. var writer = PdfWriter.GetInstance(document, output);
  36. writer.CloseStream = false;
  37. document.Open();
  38.  
  39. var xmlWorker = iTextSharp.tool.xml.XMLWorkerHelper.GetInstance();
  40. xmlWorker.ParseXHtml(writer, document, input, System.Text.Encoding.UTF8);
  41. document.Close();
  42. output.Position = 0;
  43. return new FileStreamResult(output, "application/pdf");
  44. }
  45. }
Step-10: Run Application.

No comments:

Post a Comment

How to register multiple implementations of the same interface in Asp.Net Core?

 Problem: I have services that are derived from the same interface. public interface IService { } public class ServiceA : IService { ...