简单实现Crystal Report的动态加载

来源:互联网 发布:网络宣传与洞察力 编辑:程序博客网 时间:2024/04/20 06:50
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

      Crystal reprot 为我们开发报表提供了很大的便利,但是它不能实现runtime时数据自定义,给开发带来了不完美。不过虽然我们不能runtime自定义数据,但我们还是可以实现runtime自定义加载报表。
       要实现自定义加载报表,要使用推模式报表生成。(拉模式我没有试过,哪位网友要是实现了可以告诉我:) )
       回顾一下推模式的操作过程,在利用生成报表的那一步中,我们选择空报表。这个时候在解决方案中生成一个Report.rpt(假定是这个名字),但是我们在方案目录下,我们还可以看到一个同名的Report.cs。这个文件就是我们这次讨论的关键。我们先来看看这个文件有什么。

namespace WebApp_Crystal_Dynametic {
using System;
using System.ComponentModel;
using CrystalDecisions.Shared;
using CrystalDecisions.ReportSource;
using CrystalDecisions.CrystalReports.Engine;

public class Report : ReportClass {

public Report() {
}

public override string ResourceName {
get {
return "Report.rpt";
}

}
……

       看到红色高亮的程序吗?当我们向Report推数据的时候Report类如何把数据绑定到合适报表中呢,就是靠这个代码了!既然如此,那么我们如果能够动态改变它的返回值就可以动态加载报表了,让我们来试试。下面是我修改后的代码:

namespace WebApp_Crystal_Dynametic {
using System;
using System.ComponentModel;
using CrystalDecisions.Shared;
using CrystalDecisions.ReportSource;
using CrystalDecisions.CrystalReports.Engine;


public class Report : ReportClass {
string resourcename = "Report.rpt";
public Report() {
}

public override string ResourceName {
get {
return resourcename;
}
set {
resourcename = value;
}
}
……

       好现在我们再新建几个不同的报表(都是推模式的),在程序中我添加了几个button,不同的button事件中加载不同的报表,并把不同的数据推向报表。程序编译运行通过(window server 2003 + vs.net 2003)。

       这样我们只要在程序中使用plugin模式,就可以在不改变源代码的情况下为程序添加新的报表文件。

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击