vs2005中水晶报表实现 【说明】 水晶报表在应用时分两种方法,分别是拉模式(PULL)、推模式(PUSH)。拉模式:在水晶报表生成时的数据源是从水晶报表文件中的SQL语句从数据库中提取的,在编程时不用重写SQL语句,但要加上登录信息(具体方法,后面介绍)。推模式:在水晶报表生成时的数据源,是用编程时重写水晶报表中SQL语句而生成的dataset对像。也就是说,推模式是用dataset组装水晶报表。 水晶报表组件介绍。水晶报表在VS2005中有两种组件,在WEB项目是分别是CrystalReportSource,CrystalReportViewer。在FORM项目里是分别是crystalReport,CrystalReportViewer。 CrystalReportSource,crystalReport是水晶报表的数据提供者;CrystalReportViewer是水晶报表的浏览器。另外还要介绍一下水的报表的文件是以rpt为扩展名的文件,该文件可以用VS2005生成。 下面分别介绍具体操作方法: 拉模式(PULL): 在拉模式中如要在水晶报表中的SQL语句加上条件参数时要用{?参数名}方式给出。例:“Select T1, T2, T3 FROM T Where T1=''{?parm}''” parm就是参数名 以下例子中所用到的水晶报表文件中使用的SQL语句是“Select T1, T2, T3 FROM T Where T1=''{?parm}''” parm就是参数名。 【WEB方式下】 using CrystalDecisions.Shared; using CrystalDecisions.CrystalReports.Engine; /// <summary> ///功能:拉模式提取水晶报表 /// </summary> /// <param name="sender"> </param> /// <param name="e"> </param> protected void Button_pull_Click(object sender, EventArgs e) { // CrystalReport.rpt是水晶报表文件的名称;CrystalReportSource1是从工具箱加到页面上的水晶报表数据源对像。 CrystalReportSource1.ReportDocument.Load(Server.MapPath("CrystalReport.rpt")); // SetDatabaseLogon 拉模式中必须用这个方法来设置登录信息,参数一:用户名;参数二:密码;参数三:服务器;参数四:数据库名 CrystalReportSource1.ReportDocument.SetDatabaseLogon("sa", "123456", @"SYWZSWL/SQLEXPRESS", "Test"); //给水晶报表传参数,参数一:是参数名,参数二:参数值; CrystalReportSource1.ReportDocument.SetParameterValue("Title", "这是一个测试报表"); CrystalReportSource1.ReportDocument.SetParameterValue("Parm", "1"); //绑定水晶报表数据源。 CrystalReportSource1.DataBind(); // CrystalReportViewer1是水晶报表浏览器,下面是给该浏览器赋上对像 CrystalReportViewer1.ReportSource = CrystalReportSource1; CrystalReportViewer1.DataBind(); } 【FORM方式下】 //在FORM方式下代码同WEB方式,用crystalReport控件换掉了CrystalReportSource;用crystalReportViewer换掉了CrystalReportViewer;这两个控件都可以在工具箱里找到。同时在编程时去掉DataBind()方法。 private void Form1_Load(object sender, EventArgs e) { crystalReport1.Load(Application.StartupPath + "CrystalReport.rpt"); crystalReport1.SetDatabaseLogon("sa", "123456", @"SYWZSWL/SQLEXPRESS", "Test"); crystalReport1.SetParameterValue("Title", "这是一个测试报表"); crystalReport1.SetParameterValue("Parm", "1"); crystalReportViewer1.ReportSource = crystalReport1; } 推模式(PUSH): 在推模式中编程组装的Dataset里的SQL语句中的字段要与水晶报表里的SQL语句字段一致。简单的说,推模式中的水晶报表是个模板,把在设计器里报表的格式设好后,再组装DataSet就可以生成报表了。 【WEB方式下】 using CrystalDecisions.Shared; using CrystalDecisions.CrystalReports.Engine; using System.Data.SqlClient; protected void Button_push_Click(object sender, EventArgs e) { string sql = "Select T1, T2, T3 FROM T where T1=''a''"; string DBConfig_sql =@"Data Source=SYWZSWL/SQLEXPRESS;Initial Catalog=Test;User ID=sa;Password=123456"; DataSet ds = new DataSet(); SqlConnection sqlCon = new SqlConnection(DBConfig_sql); SqlCommand sqlCmd = new SqlCommand(sql, sqlCon); SqlDataAdapter sqlAd = new SqlDataAdapter(); sqlAd.SelectCommand = sqlCmd; sqlAd.Fill(ds, "sql"); CrystalReportSource1.ReportDocument.Load(Server.MapPath("CrystalReport.rpt")); //注意此处必需指明Dataset中的表的名称,否则会提示“您请求的报表需要更多信息.” CrystalReportSource1.ReportDocument.SetDataSource(ds.Tables["sql"]); //{?}中的参数可以不用赋值,即使赋了值也不起作用。 // CrystalReportSource1.ReportDocument.ParameterFields["Parm"].CurrentValues.AddValue("1234567"); CrystalReportSource1.ReportDocument.ParameterFields["Title"].CurrentValues.AddValue("这时推模式的报表样例!"); CrystalReportSource1.DataBind(); CrystalReportViewer1.ReportSource = CrystalReportSource1; CrystalReportViewer1.DataBind(); } 【FORM方式下】 private void Form1_Load(object sender, EventArgs e) { //推模式 string sql = "Select T1, T2, T3 FROM T where T1=''a''"; string DBConfig_sql = @"Data Source=SYWZSWL/SQLEXPRESS;Initial Catalog=Test;User ID=sa;Password=123456"; DataSet ds = new DataSet(); SqlConnection sqlCon = new SqlConnection(DBConfig_sql); SqlCommand sqlCmd = new SqlCommand(sql, sqlCon); SqlDataAdapter sqlAd = new SqlDataAdapter(); sqlAd.SelectCommand = sqlCmd; sqlAd.Fill(ds, "sql"); crystalReport1.Load(Application.StartupPath + "CrystalReport.rpt"); crystalReport1.SetDataSource(ds.Tables["sql"]); //{?}中的参数可以不用赋值,即使赋了值也不起作用。 // CrystalReportSource1.ReportDocument.ParameterFields["Parm"].CurrentValues.AddValue("1234567"); crystalReport1.ParameterFields["Title"].CurrentValues.AddValue("这时推模式的报表样例!"); crystalReportViewer1.ReportSource = crystalReport1 报表的数据来源已经不是报表模板使用拉模式获得了,是推模式的一种实现 这样实现的好处是这里的sql是由页面输入条件动态构造出的,可以实现按条件的动态查询。不使用强类型dataset,用datatable非常方便。 这种方式和以前用pb构造动态datawindow的方式一样,我使用起来觉的还是很顺手的。 容易实现的优点从安全性上看就变成了缺点,页面输入拼装sql,安全性不高。 如果涉及多个表,sql很长构造麻烦的情况,使用第二种可能不是很方便。那么在第一种方法下能否实现where条件的动态变化呢 这时可以使用公式筛选记录的方法实现: ReportDocument.DataDefinition.RecordSelectionFormula = "{**}= " textBox1.Text ; 筛选条件也可以根据页面输入动态构造 我的理解是数据从数据库中拉来后再使用过滤条件进行过滤的方式,性能会不好控制。 当然也可以使用CrystalReportViewer控制过滤,比如 crystalReportViewer1.SelectionFormula = "{**}= " textBox1.Text; 但是按照表示层和基础业务逻辑分开的原则,用ReportDocument更加自然。在vs2005的帮助中有完整的帮助和示例。 要按照使用环境限制,适当选择实现方式。 去年有个多维报表的项目用sqlserver2005的reportservice做的,现在来看水晶报表也完全可以,而且水晶报表也能实现切片和钻取的效果。建立交叉表,两个数据在两个维度变化。尤其是数据源非sqlserver2005的情况下,还是非常好的选择。 技巧: 1.非交叉表实现单元格合并,网上的意见都是无法实现,后来在一个网站上看到了实现方法,虽然不自然,但是效果可以出来,需要合并的单元格都设置为组,并在“节专家”,选择你的组页眉,右边“公用”中选中“延伸到后续节”。设置对象格式,上边框为单线。 在设计时预览都没有问题,但是嵌入网页运行后,不知道为什么线都丢失了。没办法,再插入线,在设计时预览线条会粗些,但是运行时刚好。 这是野路子的实现。