.NET水晶報(bào)表優(yōu)劣談
.NET水晶報(bào)表首先要從概念入手,水晶報(bào)表(Crystal Report)是業(yè)內(nèi)最專業(yè)、功能最強(qiáng)的報(bào)表系統(tǒng),它除了強(qiáng)大的報(bào)表功能外,最大的優(yōu)勢是實(shí)現(xiàn)了與絕大多數(shù)流行開發(fā)工具的集成和接口。
1、.NET水晶報(bào)表的好處
1)利用水晶報(bào)表可以進(jìn)行數(shù)值求平均值,畫圖等
2)利用水晶報(bào)表可以把文件導(dǎo)出不同的格式(word等)
2、.NET水晶報(bào)表的兩種格式
1)pull模式,不利用DataSet,直接從數(shù)據(jù)庫中取出數(shù)據(jù)
2) push模式,使用DataSet,利用它進(jìn)行數(shù)據(jù)的加載和處理等
3. .NET水晶報(bào)表使用的庫
1)水晶報(bào)表的引擎(CREnging.dll),作用:合并數(shù)據(jù),裝換格式
2)水晶報(bào)表設(shè)計(jì)器(CRDesigner.dll),作用:設(shè)計(jì)標(biāo)題,插入數(shù)據(jù)等
3)水晶報(bào)表查看控件(CRWebFormViewer.DLL)
4)需要引入的命名空間
- using CrystalDecisions.CrystalReports.Engine;
- using CrystalDecisions.Shared;
4、Pull模式下使用水晶報(bào)表
1)創(chuàng)建rpt文件
2)拖放CrystalReportViewer
3)綁定
5、讀取.NET水晶報(bào)表文件
- private void ReadCRV(cryatalReportViewer crv)
- {
- openFileDialog dlg=new OpenFileDialog();
- dlg.Title="打開水晶報(bào)表文件";
- dlg.Filter="水晶報(bào)表文件(*.rpt)|*.rpt|所有文件|*.*";
- if(dlg.showDialog()==DialogResult.OK)
- {
- crv.ReportSource=dlg.FileName;
- }
- }
6. B/S下讀取報(bào)表的文件
- private void ReadCRV(cryatalReportViewer crv,File file)
- {
- string strName=file.PostedFile.FileName;
- if(strName.Trim()!="")
- {
- crv.ReportSource=strName
- Session["fileName"]=strName;
- }
- }
在B/S中要防止數(shù)據(jù)源的丟失
- priavte void Page_Load(object sender,System.EventArgs e)
- {
- if(Session["fileName"]!=null)
- {
- crv.ReportSource=Session["fileName"].ToString();
- }
- }
7. 假如直接從數(shù)據(jù)庫中讀取數(shù)據(jù)
采用PULL模式可能出現(xiàn)錯(cuò)誤(登錄的用戶名和密碼不對(duì))
- 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模式,直接在數(shù)據(jù)源讀取
- 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. 導(dǎo)出水晶報(bào)表的文件
- 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下水晶報(bào)表的打印
- 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)//參數(shù)設(shè)置為0,表示打印所用頁
- }
【編輯推薦】