水晶报表Pull模式的两种方法和Push模式的实现
水晶报表Pull模式的两种方法和Push模式的实现
Pull模式:
//生成报表的方法
private void GenerateReport() { /************************************************************************* * CrystalReport(Pull Mode First) * ***********************************************************************/ ReportDocument objReportDocument = new ReportDocument(); //获取数据库信息 TableLogOnInfo objTLOI = new TableLogOnInfo(); objTLOI.ConnectionInfo.ServerName = "cfox"; objTLOI.ConnectionInfo.UserID = "sa"; objTLOI.ConnectionInfo.Password = "sa"; objTLOI.ConnectionInfo.DatabaseName = "ETMS"; //为水晶报表绑定数据库 string filePath = Server.MapPath("..\\Reports\\PullModeTest.rpt"); objReportDocument.Load(filePath); objReportDocument.Database.Tables[0].ApplyLogOnInfo(objTLOI); //给水晶报表传参数 objReportDocument.SetParameterValue("OrderID",orderID); //给控件绑定数据源 this.crPullModeTest.ReportSource = objReportDocument; this.crPullModeTest.DataBind(); //导出文件 ExportOptions crExportOption = new ExportOptions(); DiskFileDestinationOptions crDiskFileDestOption = new DiskFileDestinationOptions(); crDiskFileDestOption.DiskFileName = Server.MapPath("..\\TempReports\\" + "PullModeTest.pdf"); crExportOption = objReportDocument.ExportOptions; crExportOption.DestinationOptions = crDiskFileDestOption; crExportOption.ExportDestinationType = ExportDestinationType.DiskFile; crExportOption.ExportFormatType = ExportFormatType.PortableDocFormat; objReportDocument.Export(); FileInfo file = null; string filename = null; filename = Server.MapPath("..\\TempReports\\" + "PullModeTest.pdf"); file = new FileInfo(filename); Response.Clear(); Response.AddHeader("Content-Disposition", "attachment; filename=" + "PullModeTest.pdf"); Response.AddHeader("Content-Length", file.Length.ToString()); Response.ContentType = "application/octet-stream"; Response.WriteFile(filename); Response.Flush(); System.IO.File.Delete(filename); Response.End(); /*************************************************************************** * CrystalReport(Pull Mode Second) * 注意:此种模式下才使用下面的"InitialCode" * **************************************************************************/ //实例化报表 Report = new PullModeTest(); //给报表传参数 ParameterValues OrderID = new ParameterValues(); AddDiscreteValue(OrderID, orderID); Report.DataDefinition.ParameterFields["OrderID"].ApplyCurrentValues(OrderID); try { string filename = "PullModeTest.pdf"; Export(filename, ExportFormatType.PortableDocFormat); } catch (Exception ex) { Response.Write(ex.ToString()); } } #region InitialCode //获取水晶报表名字 protected ReportClass Report { get { return rpt; } set { rpt = value; } } protected void LogOnReport() { LogOnReport(ServerName, DatabaseName, UserID, UserPassword); } protected void LogOnReport(string serverName, string databaseName, string uid, string pwd) { //连接数据库 ConnectionInfo crConnInfo = new ConnectionInfo(); crConnInfo.ServerName = serverName; crConnInfo.DatabaseName = databaseName; crConnInfo.UserID = uid; crConnInfo.Password = pwd; foreach (CrystalDecisions.CrystalReports.Engine.Table crTable in rpt.Database.Tables) { // Apply the connection info for each table in the report TableLogOnInfo crTableLogonInfo = crTable.LogOnInfo; crTableLogonInfo.ConnectionInfo = crConnInfo; crTable.ApplyLogOnInfo(crTableLogonInfo); // Check if the table source is database or DataSet // NOTE: Do not set location of table if source is DataSet if (crTable.Location != "") { // Removing qualifying prefixes will assume the database and owner info from your connection in the report. crTable.Location = crTable.Location.Substring(crTable.Location.LastIndexOf(".") + 1); } } logon = true; } protected void Export(string filename) { Export(filename, DefaultFormatType); } protected void Export(string filename, ExportFormatType formatType) { lock (CrystalReportLock) { // Apply logon info to report tables if (!logon) { LogOnReport(); } /************************************************************************ * Export report to stream *************************************************************************/ // Stream exportStream = rpt.ExportToStream(formatType); // byte[] buffer = new byte[exportStream.Length]; // int offset = 0; // // NOTE: This is to handle export of content greater than 2GB which is max int // int count = (int) Math.Min(exportStream.Length, (long) Int32.MaxValue); // while (offset < exportStream.Length) // { // offset += exportStream.Read(buffer, offset, count); // } // exportStream.Close(); // Response.ClearContent(); // Response.ClearHeaders(); // Response.AppendHeader("Content-Disposition", String.Format("download;filename=\"{0}\"", filename)); // Response.ContentType = GetExportContentType(formatType); // Response.BinaryWrite(buffer); // Response.Flush(); // Response.Close(); // Response.End(); /************************************************************************** * Export report to disk **************************************************************************/ string fileName = Server.MapPath("..\\TempReports\\" + filename); rpt.ExportToDisk(formatType, fileName); FileInfo file = new FileInfo(fileName); Response.Clear(); Response.AddHeader("Content-Disposition", "attachment; filename=" + filename); Response.AddHeader("Content-Length", file.Length.ToString()); Response.ContentType = "application/octet-stream"; Response.WriteFile(fileName); Response.Flush(); File.Delete(fileName); Response.End(); } } protected ParameterDiscreteValue AddDiscreteValue(ParameterValues values, object val) { ParameterDiscreteValue dValue = new ParameterDiscreteValue(); dValue.Value = val; values.Add(dValue); return dValue; } // protected void AddDiscreteValue(ParameterValues values, IList vals) // { // foreach (object val in vals) // { // ParameterDiscreteValue dValue = new ParameterDiscreteValue(); // dValue.Value = val; // values.Add(dValue); // } // } // // protected ParameterRangeValue AddRangeValue(ParameterValues values, object startVal, object endVal) // { // ParameterRangeValue rValue = new ParameterRangeValue(); // rValue.StartValue = startVal; // rValue.EndValue = endVal; // values.Add(rValue); // return rValue; // } private string GetExportContentType(ExportFormatType formatType) { string contentType; switch (formatType) { case ExportFormatType.HTML32: contentType = "application/html"; break; case ExportFormatType.HTML40: contentType = "application/html"; break; case ExportFormatType.Excel: contentType = "application/vnd.ms-xls"; break; case ExportFormatType.WordForWindows: contentType = "application/msword"; break; case ExportFormatType.RichText: contentType = "application/rtf"; break; case ExportFormatType.PortableDocFormat: contentType = "application/pdf"; break; default: contentType = "application/html"; break; } return contentType; } #endregion
private void GenerateReport()
{ /******************************************************************************** * CrystalReport(Push Mode) * 注意:使用水晶报表的Push模式,只能先建立一个DataSet.xsd文件,然后在其中建立一个新的 * element元素,然后连接数据库,把要用到的表放到DataSet.xsd文件中,看都用到了哪些字段, * 然后把用到的字段都放到新建立的element中,然后删除那些从数据库中拖过来的表,然后点击 * 右键生成数据集,并且在程序中从数据库中取得的数据集的表的名称应该命名成与element表的 * 名称相同,这样就可以了。 * ***********************************************************************************/ string fileName = Server.MapPath("..\\Reports\\PushModeTest.rpt"); ReportDocument objReportDocument = new ReportDocument(); objReportDocument.Load(fileName); DataSet ds = new DataSet("OrderAndOrderDetails"); ds = objMyTest.GetOrderDetailsByOrderID(10248); if(ds.Tables[0].Rows.Count > 0) { objReportDocument.SetDataSource(ds); } else { return; } //给水晶报表绑定数据源 this.crPushMode.ReportSource = objReportDocument; this.crPushMode.DataBind(); //导出设置 string newFileName = Server.MapPath("..\\TempReports\\PushModeTest.pdf"); ExportOptions objExportOption = new ExportOptions(); DiskFileDestinationOptions objDiskFileOption = new DiskFileDestinationOptions(); objDiskFileOption.DiskFileName = newFileName; objExportOption = objReportDocument.ExportOptions; objExportOption.DestinationOptions = objDiskFileOption; objExportOption.ExportDestinationType = ExportDestinationType.DiskFile; objExportOption.ExportFormatType = ExportFormatType.PortableDocFormat; objReportDocument.Export(); //导出PDF文件 FileInfo objFileInfo = new FileInfo(newFileName); Response.Clear(); Response.AddHeader("Content-Disposition","GenerateFileName:FileName = PushModeTest.pdf"); Response.AddHeader("Content-Length",objFileInfo.Length.ToString()); Response.ContentType = "Application/pdf"; Response.WriteFile(newFileName); Response.Flush(); File.Delete(newFileName); Response.End(); }
参考文档:
开发应用-命令模式(C#实现POS收银功能) 开发应用-观察者模式之C#实现缓存数据更新 企业数据库管理应用系统实现多种登录方式(策略模式) 水晶报表pull与push方法 C#设计模式-窗体基类模板方法定义 详述IComparer,IComparable接口,实现自定义方法比较对象大小并排序(C#) 有否支持客户自己用execl做模板实现打印报表的功能? 支持两种格式的流水号码,在DAL层重载GetNumber方法 C#.NET获取程序工作路径的几种方法 WebApi接口使用HttpGet方式实现接受实体类参数(原创) 数据字典BLL.CreateDataBinder方法实现新增记录给主键赋值 数据字典BLL不依赖WCF连接策略实现三层架构的直连模式(ADO-Direct) C#实现接口的两种方式:显示实现和隐式实现接口 清除SQL数据库日志(ldf文件)的几种方法 C# Winform 使用SUM CASE WHEN实现动态列交叉数据报表
其它资料:
什么是C/S结构? | C/S框架核心组成部分 | C/S框架-WebService部署图 | C/S框架-权限管理 | C/S结构系统框架 - 5.1旗舰版介绍 | C/S结构系统框架 - 功能介绍 | C/S结构系统框架 - 产品列表 | C/S结构系统框架 - 应用展示(图) | 三层体系架构详解 | C/S架构轻量级快速开发框架 | C/S框架网客户案例 | WebApi快速开发框架 | C/S框架代码生成器 | 用户授权注册软件系统 | 版本自动升级软件 | 数据库底层应用框架 | CSFramework.CMS内容管理系统 | |