ASP.NET2.0/3.5/4.0水晶报表创建以及调用方法
1. 创建CrystalReport(网站中添加新项CrystalReport,或者单独开发水晶报表)放到指定目录
2、使用向导创建报表(如何创建请参考其它资料)Suppliers.rpt
3、VS.NET2005/2008/2010布局报表,添加删除字段.
4、回到页面,选择控件CrystalReportViewer将其放到页面上。
5、设置CrystalReportViewer1数据源(介绍两种方法)。
引入
using CrystalDecisions.Shared; using CrystalDecisions.CrystalReports.Engine; using CrystalDecisions.ReportSource;
第一种: 拉模式
<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="True" GroupTreeImagesFolderUrl="" Height="1202px" ReportSourceID="CrystalReportSource1" ToolbarImagesFolderUrl="" ToolPanelWidth="200px" Width="1104px" ToolPanelView="None" /> <CR:CrystalReportSource ID="CrystalReportSource1" runat="server"> <Report FileName="..\..\ERP\Reports\ZMQuotation.rpt"> </Report> </CR:CrystalReportSource>
CS:
CrystalReportSource1.ReportDocument.SetDatabaseLogon(PublicVar.ShatdbID, PublicVar.shatdbPW, PublicVar.ShatdbDS, PublicVar.Shatdb);//取得公共变量 CrystalReportSource1.ReportDocument.SetParameterValue("@ItemP", "no"); CrystalReportSource1.ReportDocument.SetParameterValue("@descriptionP", "no"); CrystalReportViewer1.DataBind();
注释:PublicVar.ShatdbID 用户名, PublicVar.shatdbPW 密码, PublicVar.ShatdbDS 数据源(机器名或者IP), PublicVar.Shatdb 数据库名
CrystalReportSource1.ReportDocument.SetParameterValue("@ItemP", "no"); 报表参数
这种方法通常第一次登陆的时候就需要给定一个默认参数(如果带参数)
第二种:推模式
string DBConfig_sql = PublicVar.strCon; DataSet ds = new DataSet(); SqlConnection sqlCon = new SqlConnection(DBConfig_sql); SqlCommand sqlCmd = new SqlCommand(sql, sqlCon); SqlDataAdapter sqlAd = new SqlDataAdapter(); sqlAd.SelectCommand = sqlCmd; sqlAd.Fill(ds, "sql"); //如果绑定报表 //注意此处必需指明Dataset中的表的名称,否则会提示“您请求的报表需要更多信息.” CrystalReportSource1.ReportDocument.SetDataSource(ds.Tables["sql"]); CrystalReportViewer1.ReportSource = CrystalReportSource1; CrystalReportViewer1.DataBind(); //未绑定 String path = Server.MapPath("HandQty.rpt"); CrystalReportViewer1.Load(path); CrystalReportSource1.ReportDocument.SetDataSource(ds.Tables["sql"]); CrystalReportViewer1.ReportSource = CrystalReportSource1; CrystalReportViewer1.DataBind();
存储过程:
ReportDocument myReport = new ReportDocument(); SqlParameter[] parms = new SqlParameter[1]; parms[0] = new SqlParameter("@id", SqlDbType.Int); parms[0].Value = id; DataSet ds = new DataSet(); ds = DataAccess.DataAccessSHDB.GetTableByStore("project", parms).DataSet; CrystalDecisions.CrystalReports.Engine.ReportDocument doc = new CrystalDecisions.CrystalReports.Engine.ReportDocument(); string reportPath = Server.MapPath("..//Reports//project.rpt"); //doc.Load(@"C:/test/1.rpt"); doc.Load(reportPath); doc.SetDataSource(ds.Tables[0]); CrystalReportViewer1.ReportSource = doc; CrystalReportViewer1.DataBind();
如果有时候无法刷新数据:添加如下代码即可
CrystalReportSource1.ReportDocument.SetParameterValue("@ItemP", "no"); CrystalReportSource1.ReportDocument.SetParameterValue("@descriptionP", "no"); CrystalReportViewer1.DataBind();
第三种方式:
直接show在页面,不含水晶报表工具栏;
private void Print() { SampleRequest sr = new SampleRequest(); sr.TaskCode = taskcode; DataSet ds = new DataSet(); ds = sr.SampleRequestDetail(); if (ds != null) { CrystalDecisions.CrystalReports.Engine.ReportDocument doc = new CrystalDecisions.CrystalReports.Engine.ReportDocument(); string reportPath = Server.MapPath("..\\..\\ERP\\Reports\\SampleRequest.rpt"); doc.Load(reportPath); doc.SetDataSource(ds.Tables[0]); CrystalReportViewer1.ReportSource = doc; CrystalReportViewer1.DataBind(); string filetype = ""; filetype = "PDF"; string contenttype = ""; //string myfilename = Request.MapPath(".\\") + Session.SessionID + "." + filetype;//目录地址 注意反义字符 string myfilename = "E:\\Report\\SAL\\" + "SampleRequest" + Baseclass.getLongData().ToString() + "." + filetype; CrystalDecisions.Shared.DiskFileDestinationOptions mydiskoptions = new CrystalDecisions.Shared.DiskFileDestinationOptions(); mydiskoptions.DiskFileName = myfilename; CrystalDecisions.Shared.ExportOptions myExportOptions = new ExportOptions();//oCR.ExportOptions; myExportOptions.DestinationOptions = mydiskoptions; myExportOptions.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile; contenttype = "application/pdf"; myExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat; doc.Export(myExportOptions); //输出报表 到服务器端的制定路径 Response.ClearContent(); //清空页面 Response.ClearHeaders(); Response.ContentType = contenttype; Response.WriteFile(myfilename);//直接写入页面,页面平铺 可实现在线阅读功能 Response.Flush(); Response.Close(); } else { JScript.Alert("没有找到记录", this.Page); } }