一.我们采用下面的几步使用Push模式执行水晶报表:
1. 设计一个DataSet
2. 创建一个.rpt文件同时将其指定给上一步建立的DataSet。
3. 在aspx页面中拖放一个CrystalReportViewer控件同时将其与前面的rpt文件建立联系。
4. 在代码中访问数据库并把数据存入DataSet
5. 调用DataBind方法。
oRpt = new ReportDocument();
string RptDir =Server.MapPath("MyReport.rpt");
//Response.Write(RptDir);
//string RptDir = "E://vs2005//ASPcrystal//MyReport.rpt"; //crystalreport1.rpt文件所在的绝对路径
oRpt.Load(RptDir);
SqlConnection con = new SqlConnection("server=.;database=northwind;uid=sa;pwd=;");
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from customers", con);
DataSet ds = new DataSet();
da.Fill(ds, "customers");
oRpt.SetDataSource(ds);
this.CrystalReportViewer1.ReportSource = oRpt;
二.C#.Net的WinForm中使用水晶报表
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
MyReport mydoc = new MyReport();
mydoc.SetDataSource(ds.Tables["persontb"]);
crystalReportViewer1.ReportSource = mydoc;
TextObject tb = (TextObject)mydoc.ReportDefinition.ReportObjects["Text13"];
tb.Text = vievprint.content;
Code Behind 程序设计
18) 在Page_Load方法中调用DataBind方法。
protected void Page_Load(object sender, EventArgs e)
{
TableLogOnInfo logOnInfo = new TableLogOnInfo();
oRpt = new ReportDocument();
string RptDir = "E://vs2005//ASPcrystal//MyReport.rpt"; //crystalreport1.rpt文件所在的绝对路径
oRpt.Load(RptDir);
logOnInfo.ConnectionInfo.ServerName = "Hpc";
logOnInfo.ConnectionInfo.DatabaseName = "Northwind";
logOnInfo.ConnectionInfo.UserID = "sa";
logOnInfo.ConnectionInfo.Password ="";
oRpt.Database.Tables[0].ApplyLogOnInfo(logOnInfo);
this.CrystalReportViewer1.ReportSource = oRpt;
}
执行你的程序
20.若要生成pdf文档 在以上代码中加入
ExportOptions crExportOptions=new ExportOptions();
DiskFileDestinationOptions crDiskFileDestinationOptions=new DiskFileDestinationOptions();
crDiskFileDestinationOptions.DiskFileName="f://bbs//test//crystal//crystalreport1.pdf";
crExportOptions=oRpt.ExportOptions ;
crExportOptions.DestinationOptions=crDiskFileDestinationOptions;
crExportOptions.ExportDestinationType =ExportDestinationType.DiskFile;
crExportOptions.ExportFormatType =ExportFormatType.PortableDocFormat ;
oRpt.Export();
oRpt.Close();
读取报表导出的内容并传到客户端?继续加入以下代码
Response.ClearContent();
Response.ClearHeaders ();
Response.ContentType ="application/pdf";
Response.WriteFile("f://bbs//test//crystal//crystalreport1.pdf");
Response.Flush();
Response.Close();