您现在的位置是:首页 >技术杂谈 >Crystal Report 水晶报表实例MVC网站首页技术杂谈
Crystal Report 水晶报表实例MVC
using CrystalDecisions.CrystalReports.Engine;
using CrystalReportDemo.Models;
using System; using System.Collections.Generic;
using System.IO; using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace CrystalReportDemo.Controllers
{
public class HomeController : Controller
{
MVCTutorialEntities db = new MVCTutorialEntities();
public ActionResult EmployeeList()
{
return View(db.EmployeeInfoes.ToList());
}
public ActionResult exportReport()
{
ReportDocument rd = new ReportDocument();
rd.Load(Path.Combine(Server.MapPath("~/Report"), "CrystalReport.rpt"));
rd.SetDataSource(db.EmployeeInfoes.ToList());
Response.Buffer = false;
Response.ClearContent();
Response.ClearHeaders();
try
{
Stream stream = rd.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
stream.Seek(0, SeekOrigin.Begin);
return File(stream, "application/pdf", "Employee_list.pdf");
}
catch
{
throw;
}
}
}
}
@model IEnumerable<CrystalReportDemo.Models.EmployeeInfo>
@{
ViewBag.Title = "EmployeeList";
}
<h2>EmployeeList</h2>
<p>
<a class="btn btn-success" href="@Url.Action("exportReport")">Download Report</a>
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.EmployeeId)
</th>
<th>
@Html.DisplayNameFor(model => model.EmployeeName)
</th>
<th>
@Html.DisplayNameFor(model => model.Email)
</th>
<th>
@Html.DisplayNameFor(model => model.Phone)
</th>
<th>
@Html.DisplayNameFor(model => model.Experience)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.EmployeeId)
</td>
<td>
@Html.DisplayFor(modelItem => item.EmployeeName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Email)
</td>
<td>
@Html.DisplayFor(modelItem => item.Phone)
</td>
<td>
@Html.DisplayFor(modelItem => item.Experience)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}
</table>