Introduction
This article walks you through the steps for creating a report in Word or PDF format, using Microsoft Report without using Report Viewer.
Step 1
Create ASP.NET Web Application
Check the link given below to see all the steps to create a Web API with an Entity Framework Code First implementation.
http://social.technet.microsoft.com/wiki/contents/articles/26795.asp-net-webapi-entity-framework-code-first.aspx
Step 2 -
Install Microsoft Report Viewer Runtime into machine
To have the ability to create reports into your project, you will need to install the Runtime of Microsoft Report Viewer.
Follow the link given below and download it, as per your Visual Studio version.
http://www.microsoft.com/en-us/download/details.aspx?id=35747
Step 3
Create Report
Add new item to our project of type Report.
On the left menu, select Reporting option, followed by selecting Report item, as shown below.
Call it Contacts.rdlc.
Create new datasource associated with our connection string, which is defined on web.config.
Select Next button.
On this demo, we will use table Contacts, which is defined on our database.
Rename the dataset to DataSetContacts and click OK option.
After the creation of dataset, we need to design our report.
For this, create a table with the three columns, as shown below.
This will display the Name, Address and City of each contact existing on the database table.
Step 4
PDF Generate Class
Create new Controller called ReportControllers and use the GetPDFreport method.
C#
- using System;
- using System.Collections.Generic;
- using System.Data.Entity;
- using System.Data.Entity.Infrastructure;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Web.Http;
- using SampleEF6.Models;
- using System.Threading.Tasks;
- using System.Web;
- using System.IO;
- using System.Net.Http.Headers;
-
- namespace SampleEF6.Controllers
- {
- public class ReportController : ApiController
- {
-
- [HttpGet]
- public async Task<HttpResponseMessage> GetPDFReport()
- {
- string fileName = string.Concat("Contacts.pdf");
- string filePath = HttpContext.Current.Server.MapPath("~/Report/" + fileName);
-
- ContactController contact = new ContactController();
- List<Contact> contacList = contact.Get().ToList();
-
- await SampleEF6.Report.ReportGenerator.GeneratePDF(contacList, filePath);
-
- HttpResponseMessage result = null;
- result = Request.CreateResponse(HttpStatusCode.OK);
- result.Content = new StreamContent(new FileStream(filePath, FileMode.Open));
- result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
- result.Content.Headers.ContentDisposition.FileName = fileName;
-
- return result;
- }
- }
- }
Create the GeneratePDF method, as shown below.
C#
- using Microsoft.Reporting.WebForms;
- using SampleEF6.Models;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Threading.Tasks;
- using System.Web;
-
- namespace SampleEF6.Report
- {
- public class ReportGenerator
- {
- public static string Report = "SampleEF6.Report.Contacts.rdlc";
-
- public static Task GeneratePDF(List<Contact> datasource, string filePath)
- {
- return Task.Run(() =>
- {
- string binPath = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "bin");
- var assembly = Assembly.Load(System.IO.File.ReadAllBytes(binPath + "\\SampleEF6.dll"));
-
- using (Stream stream = assembly.GetManifestResourceStream(Report))
- {
- var viewer = new ReportViewer();
- viewer.LocalReport.EnableExternalImages = true;
- viewer.LocalReport.LoadReportDefinition(stream);
-
- Warning[] warnings;
- string[] streamids;
- string mimeType;
- string encoding;
- string filenameExtension;
-
- viewer.LocalReport.DataSources.Add(new ReportDataSource("DataSetContacts", datasource));
-
- viewer.LocalReport.Refresh();
-
- byte[] bytes = viewer.LocalReport.Render(
- "PDF", null, out mimeType, out encoding, out filenameExtension,
- out streamids, out warnings);
-
- using (FileStream fs = new FileStream(filePath, FileMode.Create))
- {
- fs.Write(bytes, 0, bytes.Length);
- }
- }
- });
- }
- }
- }
Step 5
Run the Application