This article walks you through the steps for create a report in Word or PDF format usinf Microsft Report without using Report Viewer.

STEP 1 – Create ASP.NET Web Application

Check the link below, to see all the steps to create 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 will need to install the Runtime of Microsoft Report Viewer.

Follow the link below and download it according with 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.

For that on the left menu select Reporting option and then select Report item like on the image below.

Call it Contacts.rdlc.

Create new datasource associated with our connection string defined on web.config.

Select next button.

On this demo we will use table Contacts defined on 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 like on the image below.

This will display the Name, Address and City of each contact existent on the database table.


STEP 4 – PDF Generate Class

Create new controller called ReportControllers and had the GetPDFreport method:

C#
Edit|Remove
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 
    { 
        // GET api/<controller> 
        [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#
Edit|Remove
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 could be found here: