Introduction
This article walks you through the steps of 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 below, to see all the steps of creating a Web API wtih 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 need to install the Runtime of Microsoft Report Viewer.
Follow the link below and download it according to your Visual Studio version:
http://www.microsoft.com/en-us/download/details.aspx?id=35747
STEP 3 - Create Report
Add new item to your project of type Report.
For that, select Reporting option on the left menu and then select Report item as shown in the image below.
Let's call it "Contacts.rdlc".
Create new datasource associated with your connection string defined in web.config.
Select "Next" button.
In this demo, we will use table "Contacts" defined in our database.
Rename the dataset to DataSetContacts, and press the OK option.
After the creation of dataset, we need to design our report. For that, create a table with three columns, as in the image below.
This will display the Name, Address, and City of each contact that exists in the database table.
STEP 4 - PDF Generate Class
Create new Controller called ReportControllers and have 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 like this.
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 Application
Resources
Some good resources about Windows Azure can be found here,
- My personal blog: http://joaoeduardosousa.wordpress.com/