Crystal Report在.net中的两种显示方式

编写人:CC阿爸

2014-7-29

近来在完成深圳一公司的项目,对方对各方面要求相当严格,一不满意就拒绝签收,为了对修正水晶报表显示及导出的一些小问题,无赖之下,仔细了解一下水晶报表的操作方法,逼苦我们这些苦逼的程序,虽说在以前的项目中,也常使用crystal report 来制作报表。并且针对web与winform 都各有不同的地方。

但总的来讲:显示水晶报表目前使用控件对象的有两种显示方式

1.      使用crystalReportViewer1 来显示报表

2.      使用Crystal ActiveX report viewer 来显示报表

在使用前,先废话一下有关水晶报表的一些版本的问题:

1.      我接触的第一个是7.0的版本。有一些vb程序的程序都在使用这个版本的报表

2.      后来使用上.net开发工具后,直接升到了crystal report 9.0。

3.      Vs 2008 内置了10.5的水晶报表。但这个版本在官方是没有的。

因此我制作报表时仍使用的是为10.0

4.      后来水晶报表先后推出了11  2008,现到13,14

5.      最后想说的,这中间sap收购了水晶报表,现查找技术文档只能在sap网站上查找了。

接下来。废话就不多讲了,将我们使用的代码贴出来供大家参考,发扬互联网的共享精神。让苦逼的程序猿们也少走点冤枉路了。

开发环境:vs 2008+crystal report 10

使用crystalReportViewer1 来显示报表

  1  public partial class ShowRPT : Form
  2     {
  3         private XOS.Admin.ShowForm pParentWin = null;
  4         protected string FileState = "";
  5         WinBase.Common W1 = new WinBase.Common();
  6          //这里必须事先申明一个ReportDocument对象 Report,同时加载数据报表
  7         ReportDocument oRpt = new ReportDocument();
  8         public ShowRPT(XOS.Admin.ShowForm WinMain)
  9         {
 10             InitializeComponent();
 11             pParentWin = WinMain;
 12         }
 13 
 14         private void ShowRPT_Load(object sender, EventArgs e)
 15         {
 16             ShowForm form1 = Application.OpenForms["ShowForm"] as ShowForm;
 17             TableLogOnInfo logOnInfo = new TableLogOnInfo();
 18             ReplaceExportButton();//新增一个工具栏自定义导出excel
 19            
 20             try
 21             {
 22                 string strg = pParentWin.ReportPath + "\\" + pParentWin.ReportName;
 23                 oRpt.Load(strg);
 24                 FileState = "YES";
 25             }
 26             catch (System.Exception err)
 27             {
 28                 FileState = "NO";
 29                 MessageBox.Show(err.Message, "错误提示:读取报表文件错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
 30 
 31                 //return;
 32 
 33             }
 34             if (FileState == "YES")
 35             {
 36                 logOnInfo.ConnectionInfo.ServerName = W1.LoadXmlFileValue("config.xml", "Sys", "HostName");
 37                 logOnInfo.ConnectionInfo.DatabaseName = W1.LoadXmlFileValue("config.xml", "Sys", "DataBase");
 38                 logOnInfo.ConnectionInfo.UserID = W1.Decrypt(W1.LoadXmlFileValue("config.xml", "Sys", "User"));
 39                 logOnInfo.ConnectionInfo.Password = W1.Decrypt(W1.LoadXmlFileValue("config.xml", "Sys", "Password"));
 40                 oRpt.Database.Tables[].ApplyLogOnInfo(logOnInfo);
 41                 //建立.rpt文件与CryStalReportviewer文件之间的连接
 42                 //参数
 43                 try
 44                 {
 45                     DataSet ds = new DataSet();
 46                     string _strSql = "SELECT  P.*,RP.* FROM ReportParameter RP,Report P where RP.ReportName=P.ReportName AND P.ReportName='" + pParentWin.ReportName + "' order by RP.ID ";
 47                     ds = W1.DS(_strSql, "Sys");
 48                     //动态修WinForm的Text[Report表中ReportDescription]
 49                     this.Text = this.Text + ds.Tables[].Rows[]["ReportName"].ToString() + " " + ds.Tables[].Rows[]["ReportDescription"].ToString();
 50                     for (int i = ; i < ds.Tables[].Rows.Count; i++)
 51                     {
 52                         oRpt.SetParameterValue(i, form1.str[i]);
 53 
 54                     }
 55                 }
 56                 catch (System.Exception err)
 57                 {
 58                     FileState = "NO";
 59                     MessageBox.Show(err.Message, "错误提示:读取报表参数错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
 60                     //return;
 61 
 62                 }
 63                 ParameterFields parameterFields = crystalReportViewer1.ParameterFieldInfo;
 64                 crystalReportViewer1.ReportSource = oRpt;
 65                 crystalReportViewer1.ShowRefreshButton = false;
 66 
 67             }
 68         }
 69 
 70         private void btnExportExcel_Click(object sender, EventArgs e)
 71         {          
 72 
 73                 // 声明变量并获取导出选项。
 74                 ExportOptions exportOpts = new ExportOptions();
 75                 ExcelFormatOptions excelFormatOpts = new ExcelFormatOptions();
 76                 DiskFileDestinationOptions diskOpts = new DiskFileDestinationOptions();
 77                 exportOpts = oRpt.ExportOptions;
 78                 // 设置 Excel 格式选项。
 79                 excelFormatOpts.ExcelUseConstantColumnWidth = true;
 80                 exportOpts.ExportFormatType = ExportFormatType.Excel;
 81                 exportOpts.FormatOptions = excelFormatOpts;
 82 
 83                 // 设置磁盘文件选项并导出。
 84                 exportOpts.ExportDestinationType = ExportDestinationType.DiskFile;
 85               SaveFileDialog sf = new SaveFileDialog();
 86               string FileName ="";
 87                sf.Filter = "Microsoft Excel(*.xls)|*.xls" ;
 88             // ……
 89 
 90             /* sf.DefaultExt = "rtf";
 91              * 这么设起不了作用,还不知道原因何在
 92              * 所以只好手动调整顺序 */
 93 
 94             //用sf.FilterIndex调整
 95 
 96                if (DialogResult.OK == sf.ShowDialog())
 97                {
 98                    FileName = sf.FileName;
 99                    diskOpts.DiskFileName = FileName;
                    exportOpts.DestinationOptions = diskOpts;
                    try
                    {
                        oRpt.Export();
                        MessageBox.Show("导出excel成功!" + diskOpts.DiskFileName, "成功提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                       
                    }
                    catch (System.Exception err)
                    {
                        MessageBox.Show(err.Message, "错误提示:导出excel失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
             }
         //核心
         private void ReplaceExportButton()
         {
             //遍历crystalReportViewer1控件里的控件
             foreach (object ctl in crystalReportViewer1.Controls)
             {
                 //取得控件名称
                 string sControl = ctl.GetType().Name.ToString().ToLower();
                 //取得工具条
                 if (sControl == "toolstrip")
                 {
                     ToolStrip tab1 = (ToolStrip)ctl;
                     //遍历工具条Item
                     for (int i = ; i <= tab1.Items.Count - ; i++)
                     {
                         //MessageBox.Show(tab1.Items[i].ToolTipText);
                         //如果是导出按钮
                         if (tab1.Items[i].ToolTipText == "导出报表" || tab1.Items[i].ToolTipText == "Export Report")
                         {
                             //先创建一个ToolStripButton准备替代现有Button
                             ToolStripButton tbutton = new ToolStripButton();
                             //获取原导出按钮的按钮图片
                             Image img1 = tab1.Items[i].Image;
                             //移除原导出按钮
                             //tab1.Items.Remove(tab1.Items[i]);
                             //设置新button属性
                             tbutton.Image = img1;
                             tbutton.ToolTipText = "自定义导出Execl报表按钮";
                             //在原位置上插入新Button
                             tab1.Items.Insert(,tbutton);                            
 
                             //绑定自定义事件
                             tbutton.Click += new System.EventHandler(this.btnExportExcel_Click);
                             break;
                         }
 
                     }
                 }
 
             }
         }
 
 
     }

 1 public partial class ShowRPT2 : Form
 2     {
 3         private XOS.Admin.ShowForm pParentWin = null;
 4         protected string FileState = "";
 5         WinBase.Common W1 = new WinBase.Common();      
 6         public ShowRPT2(XOS.Admin.ShowForm WinMain)
 7         {
 8             InitializeComponent();
 9             pParentWin = WinMain;
         }
 
         private void ShowRPT2_Load(object sender, EventArgs e)
         {
             ShowForm form1 = System.Windows.Forms.Application.OpenForms["ShowForm"] as ShowForm;
             TableLogOnInfo logOnInfo = new TableLogOnInfo();
             CRAXDDRT.ParameterValues crPara = new CRAXDDRT.ParameterValues();
             string strg = pParentWin.ReportPath + "\\" + pParentWin.ReportName;
             System.Windows.Forms.Application.UseWaitCursor = true;
             ApplicationClass applicationClass = new ApplicationClass();
             CRAXDDRT.Report report = new  CRAXDDRT.Report();
            
             try
             {
                 report = applicationClass.OpenReport(strg, null);
                 FileState = "YES";
             }
             catch (System.Exception err)
             {
                 FileState = "NO";
                 MessageBox.Show(err.Message, "错误提示:读取报表文件错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
 
                 //return;
 
             }
             if (FileState == "YES")
             {
                 logOnInfo.ConnectionInfo.ServerName = W1.LoadXmlFileValue("config.xml", "Sys", "HostName");
                 logOnInfo.ConnectionInfo.DatabaseName = W1.LoadXmlFileValue("config.xml", "Sys", "DataBase");
                 logOnInfo.ConnectionInfo.UserID = W1.Decrypt(W1.LoadXmlFileValue("config.xml", "Sys", "User"));
                 logOnInfo.ConnectionInfo.Password = W1.Decrypt(W1.LoadXmlFileValue("config.xml", "Sys", "Password"));
                 report.Database.Tables[].SetLogOnInfo(logOnInfo.ConnectionInfo.ServerName, logOnInfo.ConnectionInfo.DatabaseName, logOnInfo.ConnectionInfo.UserID, logOnInfo.ConnectionInfo.Password);
                 //建立.rpt文件与CryStalReportviewer文件之间的连接
                 //参数
                
                 try
                 {
                     DataSet ds = new DataSet();
                     string _strSql = "SELECT  P.*,RP.* FROM ReportParameter RP,Report P where RP.ReportName=P.ReportName AND P.ReportName='" + pParentWin.ReportName + "' order by RP.ID ";
                     ds = W1.DS(_strSql, "Sys");
                     //动态修WinForm的Text[Report表中ReportDescription]
                     this.Text = this.Text + ds.Tables[].Rows[]["ReportName"].ToString() + " " + ds.Tables[].Rows[]["ReportDescription"].ToString();
                     for (int i = ; i < ds.Tables[].Rows.Count; i++)
                     {
                         report.ParameterFields.GetItemByName(ds.Tables[].Rows[i]["ParaName"].ToString(), null).ClearCurrentValueAndRange();
                         report.ParameterFields.GetItemByName(ds.Tables[].Rows[i]["ParaName"].ToString(), null).AddCurrentValue(form1.str[i]);
                        // report.ParameterFields[i].AddCurrentValue(form1.str[i]);
 
                     }
                 }
                 catch (System.Exception err)
                 {
                     FileState = "NO";
                     MessageBox.Show(err.Message, "错误提示:读取报表参数错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                     //return;
 
                 }
                
                
             }
             axCrystalActiveXReportViewer1.ReportSource = report;
             axCrystalActiveXReportViewer1.ViewReport();
             System.Windows.Forms.Application.UseWaitCursor = false;
         }
 
   
 
 
     }

Crystal Report在.net中的两种显示方式的更多相关文章

  1. MySQL中的两种临时表

    MySQL中的两种临时表 伯乐在线2016-07-06 05:16:52阅读(4556)评论(3) 声明:本文由入驻搜狐公众平台的作者撰写,除搜狐官方账号外,观点仅代表作者本人,不代表搜狐立场.举报 ...

  2. ORACLE 查询一个数据表后通过遍历再插入另一个表中的两种写法

    ORACLE 查询一个数据表后通过遍历再插入另一个表中的两种写法 语法 第一种: 通过使用Oracle语句块  --指定文档所有部门都能查看 declare cursor TABLE_DEPT and ...

  3. Linux中的两种守护进程stand alone和xinetd

    Linux中的两种守护进程stand alone和xinetd --http://www.cnblogs.com/itech/archive/2010/12/27/1914846.html#top 一 ...

  4. validate插件:验证密码没有空格 用户名是5-10位 至少包含数字和大小写字母中的两种字符

    //校验密码是否含有空格 jQuery.validator.addMethod("notblank", function(value, element) { var pwdblan ...

  5. C#中的两种debug方法

    这篇文章主要介绍了C#中的两种debug方法介绍,本文讲解了代码用 #if DEBUG 包裹.利用宏定义两种方法,需要的朋友可以参考下   第一种:需要把调试方法改成debug代码用 #if DEBU ...

  6. eclipse中的两种Jre 及 Jre与Jdk的区别

    分类: ——————————区分eclipse中的两种Jre———————- (Eclipse也是一个普通的Java程序,因此必须有一个JRE做为运行环境.如果你的机器上没有安装任何JRE(或者JDK ...

  7. 在netty3.x中存在两种线程:boss线程和worker线程。

    在netty 3.x 中存在两种线程:boss线程和worker线程.

  8. JavaScript中的两种全局对象

    这里总结的东西特别适合先学习c/c++, Java这类标准语言再学JS的童鞋们看,因为JS在程序执行之前就会初始化一个全局对象,这个全局对象到底是什么是跟JS程序运行环境有关的. 根据JavaScri ...

  9. Java中的两种异常类型及其区别?

    Java中的两种异常类型是什么?他们有什么区别? Throwable包含了错误(Error)和异常(Excetion两类) Exception又包含了运行时异常(RuntimeException, 又 ...

随机推荐

  1. C++学习17派生类的构造函数

    基类的构造函数不能被继承,在声明派生类时,对继承过来的成员变量的初始化工作也要由派生类的构造函数来完成.所以在设计派生类的构造函数时,不仅要考虑派生类新增的成员变量,还要考虑基类的成员变量,要让它们都 ...

  2. testng.xml创建及解析

    项目右键---TestNG -----> Convert to TestNG 会自动产生一个testng.xml的文件 http://www.cnblogs.com/choosewang/art ...

  3. 如何实现Oracle修改用户权限 .

    这里将介绍Oracle修改用户权限的实现过程,包括一些权限管理方面的东西.希望通过本文能对大家了解Oracle修改用户权限有所帮助. ORACLE数据库用户与权限管理 ORACLE是多用户系统,它允许 ...

  4. WPF更新数据源

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windo ...

  5. 自定义View的基本流程

    1.明确需求,确定你想实现的效果2.确定是使用组合控件的形式还是全新自定义的形式,组合控件即使用多个系统控件来合成一个新控件,你比如titilebar,这种形式相对简单,参考:http://blog. ...

  6. Android杂谈--网络状态判断

    许多联网应用都在开始运行的时候检查当前网络状态,如果没有开启则去开启它,记录一下以前写程序时的网络检查,发现人的记忆力真是有限,总是隔段时间久忘记,所以记录下来是最好的记忆. 我们可以在一开始启动程序 ...

  7. 算法杂货铺——分类算法之朴素贝叶斯分类(Naive Bayesian classification)

    算法杂货铺——分类算法之朴素贝叶斯分类(Naive Bayesian classification) 0.写在前面的话 我个人一直很喜欢算法一类的东西,在我看来算法是人类智慧的精华,其中蕴含着无与伦比 ...

  8. jboss7访问日志功能及使用goaccess工具分析

    网络上虽然很多文章分别讲到jboss7的访问日志如何配置,goaccess工具怎么分析nginx/tomcat等日志.但将两者放在一起即“通过goaccess分析jboss访问日志”的倒是没搜索到. ...

  9. 【小错误】起归档是遇到ORA-00265: instance recovery required, cannot set ARCHIVELOG mode

    今天在起归档时遇到ORA-00265: instance recovery required, cannot set ARCHIVELOG mode的错误 从错误我们能够看到是由于datafile,c ...

  10. EF,ADO.NET Entity Data Model简要的笔记

    1. 新建一个项目,添加一个ADO.NET Entity Data Model的文件,此文件会生成所有的数据对象模型,如果是用vs2012生的话,在.Designer.cs里会出现“// Defaul ...