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

创新互联建站专注为客户提供全方位的互联网综合服务,包含不限于成都网站制作、成都网站设计、肃宁网络推广、成都小程序开发、肃宁网络营销、肃宁企业策划、肃宁品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联建站为所有大学生创业者提供肃宁建站搭建服务,24小时服务热线:18982081108,官方网址:www.cdcxhl.com
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)需要引入的命名空间
- using CrystalDecisions.CrystalReports.Engine;
 - using CrystalDecisions.Shared;
 
4、Pull模式下使用水晶报表
1)创建rpt文件
2)拖放CrystalReportViewer
3)绑定
5、读取.NET水晶报表文件
- private void ReadCRV(cryatalReportViewer crv)
 - {
 - openFileDialog dlg=new OpenFileDialog();
 - dlg.Title="打开水晶报表文件";
 - dlg.Filter="水晶报表文件(*.rpt)|*.rpt|所有文件|*.*";
 - if(dlg.showDialog()==DialogResult.OK)
 - {
 - crv.ReportSource=dlg.FileName;
 - }
 - }
 
6. B/S下读取报表的文件
- private void ReadCRV(cryatalReportViewer crv,File file)
 - {
 - string strName=file.PostedFile.FileName;
 - if(strName.Trim()!="")
 - {
 - crv.ReportSource=strName
 - Session["fileName"]=strName;
 - }
 - }
 
在B/S中要防止数据源的丢失
- priavte void Page_Load(object sender,System.EventArgs e)
 - {
 - if(Session["fileName"]!=null)
 - {
 - crv.ReportSource=Session["fileName"].ToString();
 - }
 - }
 
7. 假如直接从数据库中读取数据
采用PULL模式可能出现错误(登录的用户名和密码不对)
- private void ReadCRV(CrystalReportViewer crv,CrystalReport cr)
 - {
 - ReportDocument reportDoc=new ReportDocument();
 - reportDoc.Load(Server.MapPath(cr));//要加载的rpt文件的名字
 - //解决登录的问题
 - TableLogOnInfo logonInfo = new TableLogOnInfo();
 - foreach(Table tb in ReportDoc.Database.Tables)
 - {
 - logonInfo=tb.LogOnInfo;
 - logonInfo.ConnectionInfo.ServerName="(loacl)";
 - logonInfo.ConnectionInfo.DatabaseName="Pubs";
 - logonInfo.ConnectionInfo.UserId="sa";
 - logonInfo.ConnectionInfo.Password="";
 - tb.ApplyLogOnInfo(logonInfo);
 - }
 - crv.ReportSource=reportDoc;
 - }
 
8. 采用Push模式,直接在数据源读取
- private void BindReport(CrystalReportViewer crv)
 - {
 - string strProvider="Server=(local);DataBase=pubs;uid=sa;pwd=";
 - CrystalReport cr=new CrystalReport();
 - DataSet ds=new DataSet();
 - SqlConnection conn=new SqlConnection(strProvider);
 - conn.open();
 - string strSql="select * from jobs";
 - SqlDataAdapter dap=new SqlDataAdapter(strSql,conn);
 - adp.Fill(ds,"jobs");
 - cr.SetDataSource(ds);
 - crcrv.ReportSource=cr;
 - }
 
9. 导出水晶报表的文件
- private void ExportCrv(CrystalReport cr)
 - {
 - DiskFileDestionOptions dOpt=new DiskFileDestionOptions();
 - cr.ExportOptions.ExportDestinationType=ExportDestinationType.DiskFile();
 - cr.ExportOptions.ExportFormatType= ExportFormatType.PortableDocFormat;
 - dOpt.DiskFileName="C:\output.pdf";
 - cr.ExportOptions.DestinationOptions=dOpt;
 - cr.Export();
 - }
 - private void ExportCrv(CrystalReport cr,string strType,string strPath)
 - {
 - DiskFileDestionOptions dOpt=new DiskFileDestionOptions();
 - cr.ExportOptions.ExportDestinationType=ExportDestinationType.DiskFile();
 - switch(strType)
 - {
 - case "RTF":
 - cr.ExportOptions.ExportFormatType=ExportFormatType.RichText;
 - dOpt.DiskFileName=strPath;
 - break;
 - case "PDF":
 - cr.ExportOptions.ExportFormatType=ExportFormatType.PortableDocFormat;
 - dOpt.DiskFileName=strPath;
 - break;
 - case "DOC":
 - cr.ExportOptions.ExportFormatType=ExportFormatType.WordForWindows;
 - dOpt.DiskFileName=strPath;
 - break;
 - case "XLS":
 - cr.ExportOptions.ExportFormatType=ExportFormatType.Excel;
 - dOpt.DiskFileName=strPath;
 - break;
 - default;
 - break;
 - }
 - cr.ExportOptions.DestinationOptions=dOpt;
 - cr.Export();
 - }
 
10 B/S下水晶报表的打印
- priavte void PrintCRV(CrystalReport cr)
 - {
 - string strPrinterName=@"printName";
 - PageMargins margins=cr.PrintOptions.PageMargins;
 - margins.bottomMargin = 250;
 - margins.leftMargin = 350;
 - margins.rightMargin = 350;
 - margins.topMargin = 450;
 - cr.PrintOptions.ApplyPageMargins(margins);
 - cr.PrintOptions.printerName=strPrinterName;
 - cr.PrintToPrinter(1,false,0,0)//参数设置为0,表示打印所用页
 - }
 
                本文题目:.NET水晶报表优劣谈
                
                链接分享:http://www.csdahua.cn/qtweb/news10/483310.html
            
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网