.NET水晶报表优劣谈 女儿童双肩背包钩法
创始人
2024-06-06 09:50:56
0

.NET水晶报表首先要从概念入手,水晶报表(Crystal Report)是业内最专业、功能最强的报表系统,它除了强大的报表功能外,最大的优势是实现了与绝大多数流行开发工具的集成和接口。

1、.NET水晶报表的好处

1)利用水晶报表可以进行数值求平均值,画图等

2)利用水晶报表可以把文件导出不同的格式(word等)

2、.NET水晶报表的两种格式

1)pull模式,不利用DataSet,直接从数据库中取出数据

2) push模式,使用DataSet,利用它进行数据的加载和处理等

3. .NET水晶报表使用的库

1)水晶报表的引擎(CREnging.dll),作用:合并数据,装换格式

2)水晶报表设计器(CRDesigner.dll),作用:设计标题,插入数据等

3)水晶报表查看控件(CRWebFormViewer.DLL)

4)需要引入的命名空间

  1. using CrystalDecisions.CrystalReports.Engine;   
  2. using CrystalDecisions.Shared;  

4、Pull模式下使用水晶报表

1)创建rpt文件

2)拖放CrystalReportViewer

3)绑定

5、读取.NET水晶报表文件

  1. private void ReadCRV(cryatalReportViewer crv)  
  2.    {  
  3.      openFileDialog dlg=new OpenFileDialog();  
  4.      dlg.Title="打开水晶报表文件";  
  5.      dlg.Filter="水晶报表文件(*.rpt)|*.rpt|所有文件|*.*";  
  6.      if(dlg.showDialog()==DialogResult.OK)  
  7.      {  
  8.        crv.ReportSource=dlg.FileName;  
  9.      }  
  10.    } 

6. B/S下读取报表的文件

  1. private void ReadCRV(cryatalReportViewer crv,File file)  
  2.    {  
  3.      string strName=file.PostedFile.FileName;  
  4.      if(strName.Trim()!="")  
  5.      {  
  6.        crv.ReportSource=strName 
  7.        Session["fileName"]=strName;  
  8.      }  
  9.    } 

在B/S中要防止数据源的丢失

  1. priavte void Page_Load(object sender,System.EventArgs e)  
  2.     {  
  3.       if(Session["fileName"]!=null)  
  4.       {  
  5.         crv.ReportSource=Session["fileName"].ToString();  
  6.       }  
  7.     } 

7. 假如直接从数据库中读取数据

采用PULL模式可能出现错误(登录的用户名和密码不对)

  1. private void ReadCRV(CrystalReportViewer crv,CrystalReport cr)  
  2.    {  
  3.       ReportDocument reportDoc=new ReportDocument();  
  4.       reportDoc.Load(Server.MapPath(cr));//要加载的rpt文件的名字  
  5.       //解决登录的问题  
  6.       TableLogOnInfo logonInfo = new TableLogOnInfo();  
  7.       foreach(Table tb in ReportDoc.Database.Tables)  
  8.       {  
  9.         logonInfo=tb.LogOnInfo;  
  10.         logonInfo.ConnectionInfo.ServerName="(loacl)";  
  11.         logonInfo.ConnectionInfo.DatabaseName="Pubs";  
  12.         logonInfo.ConnectionInfo.UserId="sa";  
  13.         logonInfo.ConnectionInfo.Password="";  
  14.         tb.ApplyLogOnInfo(logonInfo);  
  15.       }  
  16.       crv.ReportSource=reportDoc;  
  17.    } 

8. 采用Push模式,直接在数据源读取

  1. private void BindReport(CrystalReportViewer crv)  
  2.   {  
  3.     string strProvider="Server=(local);DataBase=pubs;uid=sa;pwd=";  
  4.     CrystalReport cr=new CrystalReport();  
  5.     DataSet ds=new DataSet();  
  6.     SqlConnection conn=new SqlConnection(strProvider);  
  7.     conn.open();  
  8.     string strSql="select * from jobs";  
  9.     SqlDataAdapter dap=new SqlDataAdapter(strSql,conn);  
  10.     adp.Fill(ds,"jobs");  
  11.     cr.SetDataSource(ds);  
  12.     crcrv.ReportSource=cr;  
  13.   } 

9. 导出水晶报表的文件

  1. private void ExportCrv(CrystalReport cr)  
  2.    {  
  3.       DiskFileDestionOptions dOpt=new DiskFileDestionOptions();  
  4.       cr.ExportOptions.ExportDestinationType=ExportDestinationType.DiskFile();  
  5.       cr.ExportOptions.ExportFormatType= ExportFormatType.PortableDocFormat;  
  6.       dOpt.DiskFileName="C:\output.pdf";  
  7.       cr.ExportOptions.DestinationOptions=dOpt;  
  8.       cr.Export();  
  9.         
  10.    }  
  11.    private void ExportCrv(CrystalReport cr,string strType,string strPath)  
  12.    {  
  13.       DiskFileDestionOptions dOpt=new DiskFileDestionOptions();  
  14.       cr.ExportOptions.ExportDestinationType=ExportDestinationType.DiskFile();  
  15.       switch(strType)  
  16.       {  
  17.          case "RTF":  
  18.            cr.ExportOptions.ExportFormatType=ExportFormatType.RichText;  
  19.            dOpt.DiskFileName=strPath;  
  20.            break;  
  21.          case "PDF":  
  22.            cr.ExportOptions.ExportFormatType=ExportFormatType.PortableDocFormat;  
  23.            dOpt.DiskFileName=strPath;  
  24.            break;  
  25.          case "DOC":  
  26.            cr.ExportOptions.ExportFormatType=ExportFormatType.WordForWindows;  
  27.            dOpt.DiskFileName=strPath;  
  28.            break;  
  29.          case "XLS":  
  30.            cr.ExportOptions.ExportFormatType=ExportFormatType.Excel;  
  31.            dOpt.DiskFileName=strPath;  
  32.            break;  
  33.          default;  
  34.          break;  
  35.              
  36.       }  
  37.       cr.ExportOptions.DestinationOptions=dOpt;  
  38.       cr.Export();  
  39.  
  40.    } 

10 B/S下水晶报表的打印

  1. priavte void PrintCRV(CrystalReport cr)  
  2.    {  
  3.      string strPrinterName=@"printName";  
  4.      PageMargins margins=cr.PrintOptions.PageMargins;  
  5.      margins.bottomMargin = 250;  
  6.      margins.leftMargin = 350;  
  7.      margins.rightMargin = 350;  
  8.      margins.topMargin = 450;  
  9.      cr.PrintOptions.ApplyPageMargins(margins);  
  10.      cr.PrintOptions.printerName=strPrinterName;  
  11.      cr.PrintToPrinter(1,false,0,0)//参数设置为0,表示打印所用页  
  12.    } 

【编辑推荐】

  1. 深入浅出.NET接口:阿猫阿狗和程序员
  2. 程序员如何选择入门编程语言?
  3. 程序员最常犯的五大非技术性错误
  4. Java程序员的知识架构浅析
  5. 专家级程序员的“饲养”心得

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...
2012年第四季度互联网状况报... [[71653]]  北京时间4月25日消息,据国外媒体报道,全球知名的云平台公司Akamai Te...