水晶报表开发之常用代码以及注意事项

摘要:
第一个是返回ReportClass对象实例,第二个是从ReportDocument返回报告文件。第二种方法是ReportDocumentoRpt=newReportDocument();将根据路径加载报告文件。您可以单独修改报告文件,但加载速度稍低。以下是在开发crystal报告时遇到的一些有用代码,方便以后的注释。代码中有注释:privatevoidfrmReport_Load{ReplaceExportButton();try{SuperReportFactoryfa=newSuperReportFactory();oRpt=fa.GetReportObject;this.crViewer.ReportSource=oRpt;this.crViewer.Zoom;}catch{MessageBox.Show;AppLogger.LogErrException;}//晶体报表已关闭,子报表已卸载,以防止“reportloadfail”privatevoid frmReportView_FormClosing{if(oRpt!

    水晶报表查看器CrystalReportViewer的ReportSource属性是object,目前对其赋值一般采用两种方式。第一种是采用返回ReportClass对象实例的方式,第二种是采用ReportDocument返回报表文件的方式。他们各有优劣。采用ReportClass oRpt = new ReportClass();的方式最终的发布方式可以不用报表文件(即后缀为*.rpt)的方式,这种方式的特定是加载很快,但是如果要修改报表文件的话就要打开工程修改编译后才能起作用。而第二种方式ReportDocument oRpt = new ReportDocument();则是根据路径加载了报表文件,可以独立修改报表文件,但是加载速度稍逊一筹。

  下面是水晶报表开发中碰到的一些比较有用的代码,方便以后备忘,代码中都有备注:

    private void frmReport_Load(object sender, EventArgs e)
        {

            ReplaceExportButton();
            try
            {
                SuperReportFactory fa = new SuperReportFactory();
                oRpt = fa.GetReportObject(ReportSort.Blood, printAllParam);
                this.crViewer.ReportSource = oRpt;
                 this.crViewer.Zoom(2);
            }
            catch (Exception ex)
            {
                MessageBox.Show("报表数据异常:" + ex.Message +
                    "\r\n详细信息:" + "\r\n" + ex.StackTrace);
                AppLogger.LogErrException("报表数据异常:", ex);
            }
        } 

  //水晶报表关闭同时卸载子报表,防止出现“report load fail”的错误

   private void frmReportView_FormClosing(object sender, FormClosingEventArgs e)
        {

            if (oRpt != null)
            {
                if (oRpt.Subreports != null)
                {
                    for (int i = 0; i < oRpt.Subreports.Count; i++)
                    {
                        oRpt.Subreports[i].Close();
                        oRpt.Subreports[i].Dispose();
                    }
                }
                oRpt.Close();
                oRpt.Dispose();
                oRpt = null;
            }
            crViewer.Dispose();
            crViewer = null;
            GC.Collect();
        }

        //自定义导出报表事件
        private void CustomerExport_Click(object sender, EventArgs e)
        {

            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "Adobe Acrobat (*.pdf)|*.pdf|Microsoft Word (*.doc)|*.doc|RTF 格式 (*.rtf)|*.rtf";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                DiskFileDestinationOptions dfdo = new DiskFileDestinationOptions();
                dfdo.DiskFileName = dlg.FileName;
                oRpt.ExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
                switch (dlg.FilterIndex)
                {
                    case 1:
                        oRpt.ExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
                        break;
                    case 2:
                        oRpt.ExportOptions.ExportFormatType = ExportFormatType.WordForWindows;
                        break;
                    case 3:
                        oRpt.ExportOptions.ExportFormatType = ExportFormatType.RichText;
                        break;
                    default:
                        oRpt.ExportOptions.ExportFormatType = ExportFormatType.WordForWindows;
                        break;
                }
                oRpt.ExportOptions.DestinationOptions = dfdo;
                oRpt.Export();                     // 导出报表
                MessageBox.Show("导出成功!", "导出报表", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            dlg.Dispose();
        }

        /// <summary>
        /// 重写导出报表按钮
        /// </summary>
        private void ReplaceExportButton()
        {
            foreach (object ctl in crViewer.Controls)
            {
                string sControl = ctl.GetType().Name.ToString().ToLower();
                if (sControl == "toolstrip")
                {
                    ToolStrip tab1 = (ToolStrip)ctl;
                    for (int i = 0; i <= tab1.Items.Count - 1; i++)
                    {
                        if (tab1.Items[i].ToolTipText == "导出报表" || tab1.Items[i].ToolTipText == "Export Report")
                        {
                            ToolStripButton tbutton = new ToolStripButton();
                            Image img1 = tab1.Items[i].Image;
                            tab1.Items.Remove(tab1.Items[i]);
                            //设置新button属性 
                            tbutton.Image = img1;
                            tbutton.ToolTipText = "AVE导出报表";
                            //在原位置上插入新Button 
                            tab1.Items.Insert(0, tbutton);
                            //绑定自定义导出事件 
                            tbutton.Click += new System.EventHandler(this.CustomerExport_Click);
                            break;
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 直接打印报表
        /// </summary>
        /// <param name="sort"></param>
        /// <param name="paramHelper"></param>
        public void PrintReportObject(ReportSort sort, PrintAllParam paramHelper)
        {
            try
            {
                ReportClass oRpt = GetReportObject(sort, paramHelper);
                    oRpt.PrintToPrinter(1, true, 1, 99999);
                    if (oRpt != null)
                    {
                        if (oRpt.Subreports != null)
                        {
                            for (int i = 0; i < oRpt.Subreports.Count; i++)
                            {
                                oRpt.Subreports[i].Close();
                                oRpt.Subreports[i].Dispose();
                            }
                        }
                        oRpt.Close();
                        oRpt.Dispose();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("报表异常:" + ex.Message);
                AppLogger.LogErrException("报表数据异常:", ex);
            }
        }

   /// <summary>
        /// 设置报表表头对应的子报表数据
        /// </summary>
        /// <param name="ocRpt">子报表对象</param>
        private void SetSubHeadData(ReportDocument ocRpt)
        {
            TextObject title = (TextObject)ocRpt.ReportDefinition.ReportObjects["txtHospitalName"];
            title.Text = ParaHelper.uriPrintParam.HospitalName + "血常规检验报告单";

            string fieldValues = string.Empty;
            string fields = string.Empty;
            string spStr = "|";
            if (SickPrint.Rows.Count > 0)
            {
                if (SickPrint.Rows[0]["SickName"].ToString() == "True")
                {
                    fieldValues += "{SickBloodInfo.SickName}" + spStr;
                    fields += "姓名:" + spStr;
                }
                if (SickPrint.Rows[0]["SectionNum"].ToString() == "True")
                {
                    fieldValues += "{SickBloodInfo.SectionNum}" + spStr;
                    fields += "科别:" + spStr;
                }
                if (SickPrint.Rows[0]["InpatientNum"].ToString() == "True")
                {
                    fieldValues += "{SickBloodInfo.InpatientNum}" + spStr;
                    fields += "住院号:" + spStr;
                }
                if (SickPrint.Rows[0]["CheckDate"].ToString() == "True" ||
                    SickPrint.Rows[0]["CheckTime"].ToString() == "True")
                {
                    fieldValues += "{SickBloodInfo.CheckDate}" + spStr;
                    fields += "采样日期:" + spStr;
                }
                if (SickPrint.Rows[0]["Sex"].ToString() == "True")
                {
                    fieldValues += "{SickBloodInfo.Sex}" + spStr;
                    fields += "性别:" + spStr;
                }
                if (SickPrint.Rows[0]["SickbedNum"].ToString() == "True")
                {
                    fieldValues += "{SickBloodInfo.SickbedNum}" + spStr;
                    fields += "床号:" + spStr;
                }
                if (SickPrint.Rows[0]["UriSeqID"].ToString() == "True")
                {
                    fieldValues += "{SickBloodInfo.SickExamineID}" + spStr;
                    fields += "样液号:" + spStr;
                }
                if (SickPrint.Rows[0]["MedicalNum"].ToString() == "True")
                {
                    fieldValues += "{SickBloodInfo.MedicalNum}" + spStr;
                    fields += "病历号:" + spStr;
                }
                if (SickPrint.Rows[0]["Age"].ToString() == "True")
                {
                    fieldValues += "{SickBloodInfo.Age}" + spStr;
                    fields += "年龄:" + spStr;
                }
                if (SickPrint.Rows[0]["Result"].ToString() == "True")
                {
                    fieldValues += "{SickBloodInfo.Result}" + spStr;
                    fields += "诊断:" + spStr;
                }
            }
            string[] fldValueAry = fieldValues.Remove(fieldValues.Length - 1).Split(new string[] { spStr }, StringSplitOptions.None);
            string[] fldAry = fields.Remove(fields.Length - 1).Split(new string[] { spStr }, StringSplitOptions.None);

            for (int i = 1; i <= fldAry.Length; i++)
            {
                ocRpt.DataDefinition.FormulaFields["fld" + i].Text = "\"" + fldAry[i - 1] + "\"";
                ocRpt.DataDefinition.FormulaFields["fldValue" + i].Text = fldValueAry[i - 1];
            }
        }

/// <summary>
        /// 设置打印格式
        /// </summary>
        /// <param name="oRpt">当前要打印的主报表对象</param>
        protected void SetPrintFormat(ReportDocument oRpt)
        {
            switch (GetPrintFormatCond())
            {
                case 0:
                    oRpt.PrintOptions.PaperSize = PaperSize.PaperA4;
                    break;
                case 1:
                    oRpt.PrintOptions.PaperSize = PaperSize.PaperA5;
                    oRpt.PrintOptions.PaperOrientation = PaperOrientation.Landscape;
                    break;
                case 2:
                    if (ParaHelper.printInstantType == "0")
                    {
                        oRpt.PrintOptions.PaperSize = PaperSize.PaperA4;
                    }
                    else if (ParaHelper.printInstantType == "1")
                    {
                        oRpt.PrintOptions.PaperSize = PaperSize.PaperA5;
                        oRpt.PrintOptions.PaperOrientation = PaperOrientation.Landscape;
                    }
                    break;
                default:
                    oRpt.PrintOptions.PaperSize = PaperSize.PaperA4;
                    break;
            }
        }

/// <summary>
        /// 设置A4模型一报表的图片信息
        /// </summary>
        /// <param name="ocRpt">模型一对应的图片子报表对象</param>
        private void SetPictureInfoForA4T1(ReportDocument ocRpt)
        {
            int width = 3400;       //每张图片的宽度
            int height = 2550;      //每张图片的高度
            int vBlank = 60;        //垂直方向图片间的间隔
            int hBlank = 100;       //水平方向图片间的间隔
            int midLine = 5300;     //模型一中,中线的位置

            BlobFieldObject pic1 = (BlobFieldObject)ocRpt.ReportDefinition.ReportObjects["pic11"];
            BlobFieldObject pic2 = (BlobFieldObject)ocRpt.ReportDefinition.ReportObjects["pic22"];
            BlobFieldObject pic3 = (BlobFieldObject)ocRpt.ReportDefinition.ReportObjects["pic33"];
            BlobFieldObject pic4 = (BlobFieldObject)ocRpt.ReportDefinition.ReportObjects["pic44"];
            BlobFieldObject pic5 = (BlobFieldObject)ocRpt.ReportDefinition.ReportObjects["pic55"];
            BlobFieldObject pic6 = (BlobFieldObject)ocRpt.ReportDefinition.ReportObjects["pic66"];

            if (pic1 == null || pic2 == null || pic3 == null ||
                pic4 == null || pic5 == null || pic6 == null)
            {
                MessageBox.Show("图形模板图形对象不存在,格式渲染失败。", "警告");
                return;
            }

            if (GetIsPrintBmpCond() == 1)
            {
                #region 根据图片数不同,设置长宽变量的值
                switch (GetPrintBmpNumCond())
                {
                    case 1:
                        width = 5800;
                        height = 4350;
                        break;
                    case 2:
                        width = 4900;
                        height = 3675;
                        break;
                    case 4:
                        width = 3735;
                        height = 2800;
                        break;
                    case 3:
                    case 5:
                    case 6:
                        width = 3400;
                        height = 2550;
                        break;
                }
                #endregion

                #region 根据图片数不同,设置图片的长和宽,左锚点和上锚点

                switch (GetPrintBmpNumCond())
                {
                    case 1:
                        pic1.Width = width;
                        pic1.Height = height;
                        pic1.Left = midLine - (pic1.Width / 2);
                        pic1.Top = vBlank;
                        break;
                    case 2:
                        pic1.Width = pic2.Width = width;
                        pic1.Height = pic2.Height = height;
                        pic1.Left = midLine - pic1.Width - hBlank;
                        pic2.Left = midLine + hBlank;
                        pic1.Top = pic2.Top = vBlank;
                        break;
                    case 3:
                        pic1.Width = pic2.Width = pic3.Width = width;
                        pic1.Height = pic2.Height = pic3.Height = height;
                        pic1.Left = midLine - (pic1.Width + hBlank + pic2.Width / 2);
                        pic2.Left = midLine - pic2.Width / 2;
                        pic3.Left = midLine + (pic2.Width / 2 + hBlank);
                        pic1.Top = pic2.Top = pic3.Top = vBlank;
                        break;
                    case 4:
                        pic1.Width = pic2.Width = pic3.Width = pic4.Width = width;
                        pic1.Height = pic2.Height = pic3.Height = pic4.Height = height;
                        pic1.Left = pic3.Left = midLine - pic1.Width - hBlank;
                        pic2.Left = pic4.Left = midLine + hBlank;
                        pic1.Top = pic2.Top = vBlank;
                        pic3.Top = pic4.Top = vBlank + pic1.Height + vBlank;
                        break;
                    case 5:
                    case 6:
                        pic1.Width = pic2.Width = pic3.Width = pic4.Width = pic5.Width = pic6.Width = width;
                        pic1.Height = pic2.Height = pic3.Height = pic4.Height = pic5.Height = pic6.Height = height;
                        pic1.Left = pic4.Left = midLine - (pic1.Width + hBlank + pic2.Width / 2);
                        pic2.Left = pic5.Left = midLine - pic2.Width / 2;
                        pic3.Left = pic6.Left = midLine + (pic2.Width / 2 + hBlank);
                        pic1.Top = pic2.Top = pic3.Top = vBlank;
                        pic4.Top = pic5.Top = pic6.Top = vBlank + pic1.Height + vBlank;
                        break;
                    default:
                        break;
                }
                #endregion

                #region 根据图片数不同,设置图片的显示和隐藏属性
                switch (GetPrintBmpNumCond())
                {
                    case 1:
                        pic2.ObjectFormat.EnableSuppress = true;
                        pic3.ObjectFormat.EnableSuppress = true;
                        pic4.ObjectFormat.EnableSuppress = true;
                        pic5.ObjectFormat.EnableSuppress = true;
                        pic6.ObjectFormat.EnableSuppress = true;
                        break;
                    case 2:
                        pic3.ObjectFormat.EnableSuppress = true;
                        pic4.ObjectFormat.EnableSuppress = true;
                        pic5.ObjectFormat.EnableSuppress = true;
                        pic6.ObjectFormat.EnableSuppress = true;
                        break;
                    case 3:
                        pic4.ObjectFormat.EnableSuppress = true;
                        pic5.ObjectFormat.EnableSuppress = true;
                        pic6.ObjectFormat.EnableSuppress = true;
                        break;
                    case 4:
                        pic5.ObjectFormat.EnableSuppress = true;
                        pic6.ObjectFormat.EnableSuppress = true;
                        break;
                    case 5:
                        pic6.ObjectFormat.EnableSuppress = true;
                        break;
                    case 6:
                        break;
                    case 0:
                    default:
                        pic1.ObjectFormat.EnableSuppress = true;
                        pic2.ObjectFormat.EnableSuppress = true;
                        pic3.ObjectFormat.EnableSuppress = true;
                        pic4.ObjectFormat.EnableSuppress = true;
                        pic5.ObjectFormat.EnableSuppress = true;
                        pic6.ObjectFormat.EnableSuppress = true;
                        break;
                }
                #endregion
            }
            else
            {
                #region 不打印图片,设置图片长宽为0并隐藏
                pic1.Top = pic2.Top = pic3.Top = pic4.Top = pic5.Top = pic6.Top = 0;
                pic1.Left = pic2.Left = pic3.Left = pic4.Left = pic5.Left = pic6.Left = 0;
                pic1.Width = pic2.Width = pic3.Width = pic4.Width = pic5.Width = pic6.Width = 0;
                pic1.Height = pic2.Height = pic3.Height = pic4.Height = pic5.Height = pic6.Height = 0;
                pic1.ObjectFormat.EnableSuppress = true;
                pic2.ObjectFormat.EnableSuppress = true;
                pic3.ObjectFormat.EnableSuppress = true;
                pic4.ObjectFormat.EnableSuppress = true;
                pic5.ObjectFormat.EnableSuppress = true;
                pic6.ObjectFormat.EnableSuppress = true;
                #endregion

                LineObject line1 = (LineObject)ocRpt.ReportDefinition.ReportObjects["Line1"];
                line1.ObjectFormat.EnableSuppress = true;
            }
        }

/// <summary>
        /// 指定路径图片的二进制数组数据
        /// </summary>
        /// <param name="path">不带扩展名的完整图片路径</param>
        /// <returns>返回图片的二进制数组数据</returns>
        protected byte[] GetPictureData(string path)
        {
            byte[] bt;
            string fileName = string.Empty;
            string ext = string.Empty;

            path = path.Trim();
            ext = ".jpg";
            fileName = path + ext;
            if (!File.Exists(fileName))
            {
                ext = ".bmp";
                fileName = path + ext;
            }
            if (!File.Exists(fileName))
            {
                return null;
            }

            FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);
            bt = br.ReadBytes((int)fs.Length);

            return bt;
        }

     /// <summary>
        /// 判断字符串是否为数字,不包括负数
        /// </summary>
        /// <param name="value">传入的字符串</param>
        /// <returns>为数字则返回true</returns>
        public bool IsNumeric(string value)
        {
            return Regex.IsMatch(value.Trim(), @"^\d*[.]?\d*$");
        }

   /// <summary>
        /// 返回报表文件的相对目录全路径用于采用ReportDocument方式加载路径的方式
        /// </summary>
        /// <param name="oRptName">不带后最的报表文件名</param>
        /// <returns>返回报表文件的相对目录全路径</returns>
        protected string GetSuperReportFullPath(string oRptName)
        {
            string fullPath = string.Empty;
            if (!string.IsNullOrEmpty(oRptName))
                fullPath = Environment.CurrentDirectory + @"\Report\" + oRptName + ".rpt";
            else
                throw new ArgumentNullException("报表文件名参数不能为空");
            return fullPath;
        }

    ///<summary>
        /// 根据当前的打印参数获取对应的报表对象
        /// </summary>
        /// <returns>根据当前的打印参数获取对应的报表对象</returns>

    public override ReportDocument GetCurrentReport()
        {
            //ReportClass oRpt = new ReportClass();
            ReportDocument oRpt = new ReportDocument();
            SuperUriDataSet ds = this.GetCurrentReportData();

             //oRpt = new RptUriA4T1();
             oRpt.Load(GetSuperReportFullPath("RptUriA4T1"));
             this.SetSubHeadData(oRpt.Subreports["SubA4HeadLine.rpt"]);
             this.SetSubFootData(oRpt.Subreports["SubA4FootNone.rpt"]);
      this.SetPictureInfo(oRpt.Subreports["SubContPictT1.rpt"]);

     this.FillOtherFields(oRpt, ds);
             this.SetPrintFormat(oRpt);
             oRpt.SetDataSource(ds);

            return oRpt;
        }

免责声明:文章转载自《水晶报表开发之常用代码以及注意事项》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇ant design select 坑总结RocketMQ消息至少一次(At least Once)投递和消费下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

部署水晶报表的打包安装

  有水晶报表的程序打包时还需要做的事项: 参考网站:http://community.csdn.net/Expert/topic/3770/3770083.xml?temp=.9249536 安装部署时,把水晶报表的msm打进去,注册到服务器要部署使用 Crystal Reports for Visual Studio .NET 2003 的程序; 首先...

水晶报表中图片不显示解决方案

1、配置水晶报表组件首先,确认服务器上的报表组件的版本是否正确。如果你使用的是VisualStudio SDK 安装, 你可以看到CRRedist2005_x86.msi, 它所在的文件夹:C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\BootStrapper\Packages\CrystalRep...

在水晶报表中添加转换金额大写功能

在水晶报表中也是可以添加函数的 在报表空白出右键----报表----公式工作室 在报表自定义函数中就可以增加自己的函数了 Function AmountToWord2(dblAmount As Double) As String dim strValue as string strValue=cstr(dblAmount) strValue=repla...

水晶报表,不能在IE里预览。

如果未IE安装 pdf reader 插件, 将不能预览打印,只能下载: 安装 Adobe_Reader_XI_zh_CN 后,让IE启用插件即可。 1, 下载地址: 下载 2, 安装 3, 设置IE options, 启用插件: 4, 完成, 打开打印链接,即可正常预览:...

轻松加精确完成水晶报表对象的对齐、移动及大小调整——使用准线设计

目录 一、查看准线二、插入准线三、使对象与准线对齐四、使用准线定位对象五、使用准线调整对象大小----------------------------------一、查看准线可以通过在“选项”对话框中选择各个视图选项来查看“设计”和“预览”选项卡上的准线。 查看准线1、在“文件”菜单上,单击“选项”。 出现“选项”对话框。2、在“布局”选项卡中,选择“...

rdlc水晶报表在wpf里的使用

1引用程序集  Microsoft.ReportViewer.WinForms 2 xaml 命名空间 xmlns:rv="clr-namespace:Microsoft.Reporting.WinForms;assembly=Microsoft.ReportViewer.WinForms" 3 xaml 里用windowsFormsHost ...