本文转自:http://blog.csdn.net/ycz815/article/details/6934653
The following post shows you how to render an RDLC (Client Report Definition File) in a MVC project. For this tutorial, I am using VS 2008 with MVC 2 Beta. I will also be using the priceless Northwind database and the report will contain a list of customers in the Northwind database.
A sample project zip file is provided at the bottom of this post.
We start off by creating an ASP.NET MVC 2 Empty Web Application.
Add a new ADO.NET entity model and choose the option to “Generate from the database”.
Choose the connection string to use
Choose the database objects (Customers for our scenario) and hit finish. We see that a new entity data model has been created in the Models directory.
Create a folder called Content and a subfolder called Reports and add an RDLC into this folder. I hate working with Datasets and hence will not be using the “Report Wizard”. We choose the “Report” template instead.
Create a method that returns all the customers in the database through a Customer partial class and add this in the Model folder.
using System;
using System.Linq;
using System.Collections.Generic;
namespace RDLCRendering.Models
{
public partial class Customers
{
public static List<Customers> GetAllCustomers() {
var entities = new NorthwindEntities();
var x = from c in entities.Customers
select c;
return x.ToList();
}
}
}
Design your report by adding a table from the toolbox and dragging and dropping fields from the Website Data Sources (reference).
We will now construct our basic MVC web application. The app will have a home screen and a link to view the PDF report of Northwind customers. Lets start by adding a Home controller
We add the following code in our HomeController class
namespace RDLCRendering.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
string welcomeMessage = "Welcome to Northwind Traders. Enjoy the view!";
return View((object)welcomeMessage);
}
}
}
Open the Index view and add code to display the string returned and also add a link for the report in the view like so:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<string>" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Index</title>
</head>
<body>
<div>
<h2>
<%=Html.Encode(Model) %></h2>
<%=Html.ActionLink("Customers Report (PDF)", "DetailsReport", "Customers") %>
</div>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using Microsoft.Reporting.WebForms;
using RDLCRendering.Models;
namespace RDLCRendering.Controllers
{
public class CustomersController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult DetailsReport()
{
LocalReport localReport = new LocalReport();
localReport.ReportPath = Server.MapPath("~/Content/Reports/CustomerReport.rdlc");
ReportDataSource reportDataSource = new ReportDataSource("Customers", Customers.GetAllCustomers());
localReport.DataSources.Add(reportDataSource);
string reportType = "PDF";
string mimeType;
string encoding;
string fileNameExtension;
//The DeviceInfo settings should be changed based on the reportType
//http://msdn2.microsoft.com/en-us/library/ms155397.aspx
string deviceInfo =
"<DeviceInfo>" +
" <OutputFormat>PDF</OutputFormat>" +
" <PageWidth>8.5in</PageWidth>" +
" <PageHeight>11in</PageHeight>" +
" <MarginTop>0.5in</MarginTop>" +
" <MarginLeft>1in</MarginLeft>" +
" <MarginRight>1in</MarginRight>" +
" <MarginBottom>0.5in</MarginBottom>" +
"</DeviceInfo>";
Warning[] warnings;
string[] streams;
byte[] renderedBytes;
//Render the report
renderedBytes = localReport.Render(
reportType,
deviceInfo,
out mimeType,
out encoding,
out fileNameExtension,
out streams,
out warnings);
//Response.AddHeader("content-disposition", "attachment; filename=NorthWindCustomers." + fileNameExtension);
return File(renderedBytes, mimeType);
}
}
}