C#调用显示水晶报表是我们开发中经常会遇到的实用需求,那么C#调用水晶报表具体的操作是什么呢?C#调用水晶报表需要注意什么以及涉及到的方法的使用情况是什么呢?下面让我们看看具体的开发实例:
C#调用水晶报表实实例演示:
- /// ﹤summary﹥
- /// CS下在C#调用水晶报表
- /// 水晶报表在CrystalReport9中定义,
- ///数据源可以是表、视图、存储过程(最后一句必须是select语句),可以包含子报表
- /// 调用实例:LoadReport
- ///(this.crystalReportViewer1,"D:\\66.rpt","kys","hrdb","sa","kys");
- ///
- /// ﹤/summary﹥
- /// ﹤param name="ReportViewer"﹥
- 报表浏览器CrystalDecisions.Windows.Forms.CrystalReportViewer
- ﹤/param﹥
- /// ﹤param name="StrRptPaht"﹥报表文件的路径﹤/param﹥
- /// ﹤param name="StrServer"﹥服务器﹤/param﹥
- /// ﹤param name="StrDatabase"﹥数据库﹤/param﹥
- /// ﹤param name="StrUser"﹥登陆名称﹤/param﹥
- /// ﹤param name="StrPassword"﹥密码﹤/param﹥
- public void LoadReport(
- CrystalDecisions.Windows.Forms.CrystalReportViewer
- ReportViewer,string StrRptPaht,
- string StrServer, string StrDatabase,
- string StrUser, string StrPassword)
- {
- string StrParaName="";
- string StrRptTableName="";
- string StrLocation="";
- ReportDocument Rpt =new ReportDocument();
- //表示一个报表,并且包含定义、格式化、加载、导出和打印该报表的属性和方法
- SubreportObject SubRptObj;
- //表示放在报表中的子报表。子报表是主报表内的独立或链接的报表。
- ReportDocument SubRpt;
- ParameterFields ParamFields=new ParameterFields();
- ParameterField ParamField ;
- ParameterDiscreteValue ParamDiscreteValue;
- TableLogOnInfo LogOnInfo;
- try
- {
- //C#调用水晶报表之加载报表
- Rpt.Load(StrRptPaht);
- //报表参数的赋值*******C#调用水晶报表**
- foreach(CrystalDecisions.CrystalReports.
- Engine.ParameterFieldDefinition ParaFieldDef
- in Rpt.DataDefinition.ParameterFields )
- {
- //只为主报表的参数进行赋值
- if(ParaFieldDef.ReportName=="")
- {
- StrParaName=ParaFieldDef.Name;
- ParamField=new ParameterField();
- ParamDiscreteValue=new ParameterDiscreteValue();
- ParamField.ParameterFieldName = StrParaName;
- //注:用户自定义为参数赋值!!!!!!!!!!!!!!!!!!!!!!!!!
- if(StrParaName.ToLower()=="depid")
- ParamDiscreteValue.Value = "dg00";
- else
- ParamDiscreteValue.Value = "ky0001";
- //C#调用水晶报表
- ParamField.CurrentValues.Add (ParamDiscreteValue);
- ParamFields.Add (ParamField);
- }
- }
- //C#调用显示水晶报表**
- // 将参数字段集合放入查看器控件。
- if(ParamFields.Count﹥0)
- ReportViewer.ParameterFieldInfo = ParamFields;
- //设置数据库连接参数
- ConnectionInfo CnInfo = new ConnectionInfo();
- CnInfo.ServerName = StrServer;
- CnInfo.DatabaseName = StrDatabase;
- CnInfo.UserID = StrUser;
- CnInfo.Password = StrPassword;
- foreach( CrystalDecisions.CrystalReports.
- Engine.Table DTable in Rpt.Database.Tables)
- {
- LogOnInfo=DTable.LogOnInfo;
- LogOnInfo.ConnectionInfo=CnInfo;
- DTable.ApplyLogOnInfo(LogOnInfo);
- //更换服务器数据库验证
- StrRptTableName=DTable.Location.Substring(
- DTable.Location.LastIndexOf(".") + 1);
- StrLocation=StrDatabase+".dbo."+StrRptTableName;
- DTable.Location=StrLocation;
- }
- #region 对所有子报表更换服务器数据库验证
- //获取主报表的所有子报表
- foreach (ReportObject obj in Rpt.
- ReportDefinition.ReportObjects)
- {
- //判断是否为子报表对象
- if (obj.Kind == ReportObjectKind.SubreportObject)
- {
- SubRptObj = (SubreportObject) obj;
- SubRpt = Rpt.OpenSubreport(SubRptObj.SubreportName);
- //读取子报表的所有数据库表
- foreach (CrystalDecisions.CrystalReports.
- Engine.Table DTable in SubRpt.Database.Tables)
- {
- LogOnInfo = DTable.LogOnInfo;
- LogOnInfo.ConnectionInfo = CnInfo;
- //加载数据库连接信息
- DTable.ApplyLogOnInfo(LogOnInfo);
- //更换服务器和数据库
- StrRptTableName=DTable.Location.Substring(
- DTable.Location.LastIndexOf(".") + 1);
- StrLocation=StrDatabase+".dbo."+StrRptTableName;
- DTable.Location=StrLocation;
- }
- }
- }
- #endregion
- //将报表赋给报表浏览器
- ReportViewer.ReportSource=Rpt;
- //ReportViewer.RefreshReport();
- }
- catch(Exception ee)
- {
- string StrMsg=ee.Source+ee.Message;
- System.Windows.Forms.MessageBox.Show(StrMsg);
- }
- }
C#调用水晶报表的基本内容就向你介绍到这里,希望那个对你了解和学习C#调用水晶报表有所帮助。
【编辑推荐】