在 Visual Studio .NET中使用Crystal Report(下) (cashcho翻译)
发表于:2007-06-30来源:作者:点击数:
标签:
在 Visual Studio .NET 中使用 Crystal Report( 下 ) from translated by cash(天下第七) cashcao@msn.com() Crystal Report 演示-使用 Push Model 下面看看如何使用Push Model实现Crystal Reports 1.创建一个设计时的dataset 2.创建一个.rpt文件并指向我们
在 Visual Studio .NET中使用Crystal Report(下)
from
translated by cash(天下第七)
cashcao@msn.com()
Crystal Report 演示-使用Push Model
下面看看如何使用Push Model实现Crystal Reports
1.创建一个设计时的dataset
2.创建一个.rpt文件并指向我们前面创建的dataset
3.在.aspx页面上放置Crystal Report Viewer控件,设定它的属性指向上一步创建的.rpt文件。
4.在code behind page中,书写连接
数据库的函数
5. 加上databind方法。
创建一个设计时的dataset去定义Reports的Fielsds.
1)在"Solution Explorer"右击,选择"Add" --> select "Add New Item--> Select "DataSet"
2) 从"Server Explorer"面板中的"
SQL Server"中拖进"Stores"表
3) 这将在dataset中创建一个"Stores" table
用这种方法创建的.xsd文件仅仅包含了field的定义,里面没有任何数据。需要你创建一个与数据库的链接并且将数据填充进去。
创建.rpt文件
4)创建一个.rpt文件。与前面唯一不同的是不通过Crystal Report得到表,我们将用dataset来创建它。
5)建立.rpt文件后,右击"Details" section,选择"Add/Remove Database"
6) 在"Database Expert"窗口,展开"Project Data",展开"ADO.NET DataSet","DataSet1", 选择 "Stores" table.
7)点击">"将"Stores" table包括进"Selected Tables"
8) 接下来设定report的布局。
创建一个Crystal Report Viewer Control
9) 接下来的步骤是用PULL Model创建一个Crystal Report viewer Control并设定它的属性。
改code behind page 代码:
10)为你的page load里设计如下了程序:
Sub BindReport()
Dim myConnection As New SqlClient.SqlConnection()
myConnection.ConnectionString= "server= (local)\NetSDK;database=pubs;Trusted_Connection=yes"
Dim MyCommand As New SqlClient.SqlCommand()
MyCommand.Connection = myConnection
MyCommand.CommandText = "Select * from Stores"
MyCommand.CommandType = CommandType.Text
Dim MyDA As New SqlClient.SqlDataAdapter()
MyDA.SelectCommand = MyCommand
Dim myDS As New Dataset1()
@#This is our DataSet created at Design Time
MyDA.Fill(myDS, "Stores")
@#You have to use the same name as that of your Dataset that you created during design time
Dim oRpt As New CrystalReport1()
@# This is the Crystal Report file created at Design Time
oRpt.SetDataSource(myDS)
@# Set the SetDataSource property of the Report to the Dataset
CrystalReportViewer1.ReportSource = oRpt
@# Set the Crystal Report Viewer@#s property to the oRpt Report object that we created
End Sub
注意:在上面的代码中,你可能会注意到oRpt对象是"Strongly Typed" Report file的一个实例。 如果我们用"UnTyped" Report,我们将不得不使用ReportDocument 对象并且手工load这个report文件进去。
运行你的程序
11) F5 运行。
输出report文件到另一种格式
你可以选择将你的report文件输出成以下格式:
1. PDF (Portable Document Format)
2. DOC (MS Word Document)
3. XLS (MS Excel Spreadsheet)
4. HTML (Hyper Text Markup Language – 3.2 or 4.0 compli
ant)
5. RTF (Rich Text Format)
事实上,你可以放置一个button来引发一个输出函数。
输出一个以Pull Model创建的report文件
当输出一个以Pull Model创建的report文件的时候,Crystal Report对与数据库的连接及所需的记录很敏感,所以你只可使用下面提供的代码:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myReport As CrystalReport1 = New CrystalReport1()
@#Note : we are creating an instance of the strongly-typed Crystal Report file here.
Dim DiskOpts As CrystalDecisions.Shared.DiskFileDestinationOptions = New CrystalDecisions.Shared.DiskFileDestinationOptions()
myReport.ExportOptions.ExportDestinationType = CrystalDecisions.[Shared].ExportDestinationType.DiskFile
@# You also have the option to export the report to other sources
@# like Microsoft Exchange, MAPI, etc.
myReport.ExportOptions.ExportFormatType = CrystalDecisions. [Shared].ExportFormatType.PortableDocFormat
@#Here we are exporting the report to a .pdf format. You can
@# also choose any of the other formats specified above.
DiskOpts.DiskFileName = "c:\Output.pdf"
@#If you do not specify the exact path here (i.e. including
@# the drive and Directory),
@#then you would find your output file landing up in the
@#c:\WinNT\System32 directory - atleast in case of a
@# Windows 2000 System
myReport.ExportOptions.DestinationOptions = DiskOpts
@#The Reports Export Options does not have a filename property
@#that can be directly set. Instead, you will have to use
@#the DiskFileDestinationOptions object and set its DiskFileName
@#property to the file name (including the path) of your choice.
@#Then you would set the Report Export Options
@#DestinationOptions property to point to the
@#DiskFileDestinationOption object.
myReport.Export()
@#This statement exports the report based on the previously set properties.
End Sub
输出一个由Push Model创建的report文件
当输出一个由Push Model创建的report文件时,第一步是必需手工创建一个连接并绑定数据库。设定这个reports的‘SetDataSource’为此dataset(如前所述)。然后就可以调用上面所示的代码了。
原文转自:http://www.ltesting.net