上一篇,介绍了怎么导出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. PHP赋值运算

    1. 赋值运算:= ,意思是右边表达式的值赋给左边的运算数. $int1=10; $int1=$int1-6; //$int1=4 echo $int1,"<br>"; ...

  2. 使用Zabbix监控Oracle数据库

    Orabbix介绍 监控Oracle数据库我们需要安装第三方提供的Zabbix插件,我们先测试比较有名的Orabbix,http://www.smartmarmot.com/product/orabb ...

  3. python笔记(持续更新)

    1.编译python遇到下面的编码问题:     SyntaxError: Non-ASCII character '\xe9' in file E:\projects\learn.py on lin ...

  4. 防线修建 bzoj 2300

    防线修建(1s 512MB)defense [问题描述] 近来A国和B国的矛盾激化,为了预防不测,A国准备修建一条长长的防线,当然修建防线的话,肯定要把需要保护的城市修在防线内部了.可是A国上层现在还 ...

  5. StatePattern(状态模式)

    /** * 状态模式 * @author TMAC-J * 状态模式和策略模式很像,其实仔细研究发现完全不一样 * 策略模式各策略之间没有任何关系,独立的 * 状态模式各状态之间接口方法都是一样的 * ...

  6. 张高兴的 UWP 开发笔记:汉堡菜单进阶

    不同于Windows 8应用,Windows 10引入了"汉堡菜单"这一导航模式.说具体点,就拿官方的天气应用来说,左上角三条横杠的图标外加一个SplitView控件组成的这一导航 ...

  7. Mysql 学习之基础操作

    一.表复制 1.复制表结构    将表hello的结构复制一份为表hello3 2.复制数据 a.如果两张表的结构一样且你要复制所有列的数据 mysql> insert into hello3 ...

  8. linux的top命令参数详解

    简介 top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器. top显示系统当前的进程和其他状况,是一个动态显示过程,即可以通过用户按 ...

  9. [DJANGO] excel十几万行数据快速导入数据库研究

    先贴原来的导入数据代码: 8 import os os.environ.setdefault("DJANGO_SETTINGS_MODULE", "www.setting ...

  10. NYOJ 455

    1.应该交代清楚,参加宴会的人不知道一共有多少顶帽子.假如知道有n顶帽子的话,第一次开灯看见有n-1只,自然就知道自己是第n顶黑帽子,所以应该是这n个人在第一次关灯就打自己脸,不过这么一来就没意思了, ...