上一篇,介绍了怎么导出Excel文件,这篇文章介绍在ASP.NET MVC中使用水晶报表。

项目源码下载:https://github.com/caofangsheng93/CrystalReportInMac

前提条件:你需要有VS,SQL Server 当然最重要的就是安装Crystal Report。

这里我提供我百度网盘的安装文件:http://pan.baidu.com/s/1bpcK3ZD,我这里是Crystal Report for VS2013的版本。需要其他的版本大家自己去搜去下载。

环境搭配好之后,就开始干,我们先创建数据库:

USE master
GO
IF EXISTS(SELECT * FROM sysdatabases WHERE name='CustomerDB')
DROP DATABASE CustomerDB
GO
CREATE DATABASE CustomerDB
GO
USE CustomerDB
GO
IF EXISTS(SELECT * FROM sysobjects WHERE name='Customers')
DROP TABLE Customers
GO
CREATE TABLE Customers
(
CustomerID INT NOT NULL PRIMARY KEY IDENTITY(,),
CustomerName NVARCHAR() NULL,
CustomerEmail NVARCHAR() NULL,
CustomerZipCode INT NULL,
CustomerCountry NVARCHAR() NULL,
CustomerCity NVARCHAR() NULL
) --插入测试数据
INSERT INTO dbo.Customers
SELECT N'李易峰','李易峰@163.com','',N'中国',N'深圳'
UNION ALL
SELECT N'孙尚香','孙尚香@163.com','',N'中国',N'上海'
UNION ALL
SELECT N'兰陵王','兰陵王@163.com','',N'中国',N'北京'
UNION ALL
SELECT N'孙悟空','孙悟空@163.com','',N'中国',N'武汉'
UNION ALL
SELECT N'曹操','曹操@163.com','',N'中国',N'杭州'

然后,我们的项目是这样的:

弄好数据库之后,我们新建一个ADO.NET实体数据模型:命名随便,我这里命名:DbContextCustomer

接着就是新建一个水晶报表了:

Next, window will pop up as given below, in this example we are going to choose "As a Blank Report" option, and click OK.

After clicking OK, our Crystal Report has been created with success. 
The next step is to right click on Database Fields > Database Expert…

Now, a new window will pop up as shown below. We need to select model which will be using to display data (in this case our model is Customer).

After clicking OK, we proceed to drag all fields to the report as shown below.

然后就是创建控制器:

 public class CustomerController : Controller
{
private CustomerDBEntities context = new CustomerDBEntities(); public ActionResult Index()
{
var customerList= context.Customers.ToList();
return View(customerList);
} public ActionResult ExportCustomers()
{
List<Customer> allCustomer = new List<Customer>(); allCustomer = context.Customers.ToList();
ReportDocument rd = new ReportDocument();
rd.Load(Path.Combine(Server.MapPath("~/CrystalReports"), "ReportCustomer.rpt"));
rd.SetDataSource(ToDataTable<Customer>(allCustomer));
Response.Buffer = false;
Response.ClearContent();
Response.ClearHeaders();
Stream stream = rd.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
stream.Seek(, SeekOrigin.Begin);
return File(stream, "application/pdf", "CustomerList.pdf"); } /// <summary>
/// 将泛型集合类转换成DataTable
/// </summary>
/// <typeparam name="T">集合项类型</typeparam>
/// <param name="list">集合</param>
/// <param name="propertyName">需要返回的列的列名</param>
/// <returns>数据集(表)</returns>
public static DataTable ToDataTable<T>(IList<T> list, params string[] propertyName)
{
List<string> propertyNameList = new List<string>();
if (propertyName != null)
propertyNameList.AddRange(propertyName);
DataTable result = new DataTable();
if (list.Count > )
{
PropertyInfo[] propertys = list[].GetType().GetProperties();
foreach (PropertyInfo pi in propertys)
{
if (propertyNameList.Count == )
{
result.Columns.Add(pi.Name, pi.PropertyType);
}
else
{
if (propertyNameList.Contains(pi.Name))
result.Columns.Add(pi.Name, pi.PropertyType);
}
} for (int i = ; i < list.Count; i++)
{
ArrayList tempList = new ArrayList();
foreach (PropertyInfo pi in propertys)
{
if (propertyNameList.Count == )
{
object obj = pi.GetValue(list[i], null);
tempList.Add(obj);
}
else
{
if (propertyNameList.Contains(pi.Name))
{
object obj = pi.GetValue(list[i], null);
tempList.Add(obj);
}
}
}
object[] array = tempList.ToArray();
result.LoadDataRow(array, true);
}
}
return result;
}
}

视图代码:

@model IEnumerable<CryStalReportInMvc.Customer>

@{
ViewBag.Title = "Index";
} <h2>Index</h2> <p>
@Html.ActionLink("Create New", "Create")
<div><a href="@Url.Action("ExportCustomers")">Report PDF</a></div>
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.CustomerName)
</th>
<th>
@Html.DisplayNameFor(model => model.CustomerEmail)
</th>
<th>
@Html.DisplayNameFor(model => model.CustomerZipCode)
</th>
<th>
@Html.DisplayNameFor(model => model.CustomerCountry)
</th>
<th>
@Html.DisplayNameFor(model => model.CustomerCity)
</th>
<th></th>
</tr> @foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.CustomerName)
</td>
<td>
@Html.DisplayFor(modelItem => item.CustomerEmail)
</td>
<td>
@Html.DisplayFor(modelItem => item.CustomerZipCode)
</td>
<td>
@Html.DisplayFor(modelItem => item.CustomerCountry)
</td>
<td>
@Html.DisplayFor(modelItem => item.CustomerCity)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.CustomerID }) |
@Html.ActionLink("Details", "Details", new { id=item.CustomerID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.CustomerID })
</td>
</tr>
} </table>

效果图:

点击Report PDF之后:

这样就实现了报表的功能。

2.ASP.NET MVC 中使用Crystal Report水晶报表的更多相关文章

  1. [转]解决crystal report水晶报表在浏览器提示bobj未定义的错误

    网上的中文文章(比如这篇文章)都是写的部署到服务器后出现的问题,同时也指出要把crystal report的aspnet_client文件夹拷贝到对应项目的根目录里,这样就可以正常显示了,但是具体到我 ...

  2. 解决crystal report水晶报表在浏览器提示bobj未定义的错误

    网上的中文文章(比如这篇文章)都是写的部署到服务器后出现的问题,同时也指出要把crystal report的aspnet_client文件夹拷贝到对应项目的根目录里,这样就可以正常显示了,但是具体到我 ...

  3. C# WinForm开发系列 - Crystal Report水晶报表

    转自:ttp://www.cnblogs.com/peterzb/archive/2009/07/11/1521325.html 水晶报表(Crystal Report)是业内最专业.功能最强的报表系 ...

  4. Crystal Report - 水晶报表导出文件的格式设置

    水晶报表中自带的导出和打印功能用起来确实很方便,只不过有时候需要导出的文件并不需要那么多种类型,在网上找到一些朋友的代码总结了一下,可以通过代码实现自定义导出文件类型 首先需要定义一个枚举: publ ...

  5. Crystal Report水晶报表碰到的一些纠结问题

    1.插入PNG文件时,透明的背景会变成黑色.试了矢量图WMF文件,是可以正常显示的,不过毕竟得到矢量图比较困难.   后来找到个方法,就是把JPG图片放在子报表里,调整子报表在父报表的位置并且保持JP ...

  6. 在asp.net mvc中如何使用Grid++ Report (锐浪报表)

    在asp.net mvc中如何使用Grid++ Report (锐浪报表) 在cshtml,razor中的处理方法 以官方的asp.net(csharp)中的第一个示例"1a.简单表格&qu ...

  7. 在ASP.NET MVC 中使用ActiveReports报表控件

    随着MVC模式的广泛运用,对Web应用系统的开发带来了巨大的影响,我们好像又回到了原来的ASP时代,视乎这是一种后退而不是一种进步,不过MVC模式给我们带来的影响不仅限于我们所看到的这一点..MVC看 ...

  8. 关于 ASP.NET MVC 中的视图生成

    在 ASP.NET MVC 中,我们将前端的呈现划分为三个独立的部分来实现,Controller 用来控制用户的操作,View 用来控制呈现的内容,Model 用来表示处理的数据. 从控制器到视图 通 ...

  9. 在Asp.Net MVC 中配置 Serilog

    Serilog 是一种非常简便记录log 的处理方式,使用Serilog可以生成本地的text文件, 也可以通过 Seq 来在Web界面中查看具体的log内容. 接下来就简单的介绍一下在Asp.Net ...

随机推荐

  1. JavaScript var关键字、变量的状态、异常处理、命名规范等介绍

    本篇主要介绍var关键字.变量的undefined和null状态.异常处理.命名规范. 目录 1. var 关键字:介绍var关键字的使用. 2. 变量的状态:介绍变量的未定义.已定义未赋值.已定义已 ...

  2. 当web.config文件放置在共享目录下(UNC),启动IIS会提示有错误信息500.19,伴随有错误代码0x80070003和错误代码0x80070005的解决办法

    最近遇到一个很有意思的使用环境,操作人员将所有的网站应用内容投放到共享存储里面,并且使用微软的SMB协议将其以CIFS的方式共享出来,使用Windows Server 2008 R2的IIS将其连接起 ...

  3. 谈谈JS中的函数节流

    好吧,一直在秋招中,都没怎么写博客了...今天赶紧来补一补才行...我发现,在面试中,讲到函数节流好像可以加分,尽管这并不是特别高深的技术,下面就聊聊吧! ^_^ 备注:以下内容部分来自<Jav ...

  4. 不懂CSS的后端难道就不是好程序猿?

    由于H5在移动端的发展如日中天,现在大部分公司对高级前端需求也是到处挖墙角,前端薪资也随之水涨船高,那公司没有配备专用的前端怎么办呢? 作为老板眼中的“程序猿” 前端都不会是非常无能的表现,那作为后端 ...

  5. [原] KVM虚拟机网络闪断分析

    背景 公司云平台的机器时常会发生网络闪断,通常在10s-100s之间. 异常情况 VM出现问题时,表现出来的情况是外部监控系统无法访问,猜测可能是由于系统假死,OVS链路问题等等.但是在出现网络问题的 ...

  6. 【JavaScript】javascript中伪协议(javascript:)使用探讨

    javascript:这个特殊的协议类型声明了URL的主体是任意的javascript代码,它由javascript的解释器运行. 比如下面这个死链接: <a href="javasc ...

  7. Android之解析XML

    1.XML:可扩展标记语言. 可扩展标记语言是一种很像超文本标记语言的标记语言. 它的设计宗旨是传输数据,而不是显示数据. 它的标记没有被预定义.需要自行定义标签. 它被设计为具有自我描述性. 是W3 ...

  8. git 命令总结

    1 删除分支 git push origin :branch name(Task_******) //删除远程分支 git branch -D branch name(Task_******)     ...

  9. atitit.细节决定成败的适合情形与缺点

    atitit.细节决定成败的适合情形与缺点 1. 在理论界有两种观点:一种是"细节决定成败",另一种是"战略决定成败".1 1.1. 格局决定成败,方向决定成败 ...

  10. WebAPI

    WebAPI的Host OWIN IIS WebAPI 的handler和Filter有啥区别? WebAPI  常用 Filters Exception Filter Timer Filter Lo ...