2009-7-27 13:12

Asp.Net中设计与使用水晶报表

我们采用下面的几步使用Push模式执行水晶报表:

1. 设计一个DataSet

2. 创建一个.rpt文件同时将其指定给上一步建立的DataSet。

3. 在aspx页面中拖放一个CrystalReportViewer控件同时将其与前面的rpt文件建立联系。

4. 在代码中访问数据库并把数据存入DataSet

5. 调用DataBind方法。

设计一个DataSet

1) 右击“解决方案浏览器”,选择“添加”--“添加新项”-->“数据集”

2) 从“服务器资源管理器”中的“SQL Server”中拖放“Stores”表(位于PUBS数据库中)。

3) 此时在数据集中就会有一个Stores表的结构图。

- .xsd文件中仅仅包含一个结构图,但是不会有任何数据在里面。

创建 .rpt 文件 :

4) 使用上面的介绍过的方法创建此文件,唯一的不同就是使用数据集来代替前面的直接连接数据。

5)建立.rpt文件之后,右击“详细资料”-->"添加/删除数据库“

6) 在”数据库专家“窗口中,展开”项目数据“(代替以前的OleDb),展开“ADO.Net数据集”--"DataSet1“,选择”Stores“表。

7) 将”Stores"表添加到“选定的表”中,点击“OK”

8) 使用PULL模式下的方法,建立一个WebForm

建立一个Crystal Report Viewer 控件

9) 建立一个Crystal Report Viewer 控件,并设定其属性,此处与PULL模式下是一致的。

Code Behind 代码:

10) 在Page_Load方法中使用下面的子函数:

VB.Net代码:

以下为引用的内容:
  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()
    '这就是我们在设计模式上使用的DataSet
    MyDA.Fill(myDS, "Stores")
    '你不得不使用与你前面DataSet相同名字。
    Dim oRpt As New CrystalReport1()
    ' 水晶报表绑定
    oRpt.SetDataSource(myDS)
    ' 设定水晶报表的ReportSource
    CrystalReportViewer1.ReportSource = oRpt
  End Sub

 

C#代码:

以下为引用的内容:
private void BindReport()
{
  string strProvider = "Server=(local);DataBase=pubs;UID=sa;PWD=";
  CrystalReport1 oCR = new CrystalReport1();
  Dataset1 ds = new Dataset1();
  SqlConnection MyConn = new SqlConnection(strProvider);
  MyConn.Open();
  string strSel = "Select * from Stores";
  SqlDataAdapter MyAdapter = new SqlDataAdapter(strSel,MyConn);
  MyAdapter.Fill(ds,"stores");
  oCR.SetDataSource(ds);
  this.CrystalReportViewer1.ReportSource = oCR;
}


注意:在上面的代码中,你得注意一下oRpt是"Strongly Typed"的报表文件。如果你需要使用"UnTyped"报表,你得使用ReportDocument对象,然后再调用报表文件。 运行你的程序。

11) 运行你的程序,将报表文件导出成为其它格式,你能够将报表文件导出成为下列格式:

以下为引用的内容:
      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 compliant)
      5. RTF (Rich Text Format)


使用Pull模式导出报表,当导出使用Pull模式创建的文件时,水晶报表准确地打开所需要的数据,下面是执行导出功能的代码:

以下为引用的内容:
C#代码:

VB.Net代码:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  Dim myReport As CrystalReport1 = New CrystalReport1()
  '注意:这里我们建立一个strong-typed的水晶报表实例。
    Dim DiskOpts As CrystalDecisions.Shared.DiskFileDestinationOptions = New CrystalDecisions.Shared.DiskFileDestinationOptions()
    myReport.ExportOptions.ExportDestinationType = CrystalDecisions.[Shared].ExportDestinationType.DiskFile
  ' 导出成为其它文件时也需要这个选项
  ' 如Microsoft Exchange, MAPI等.
    myReport.ExportOptions.ExportFormatType = CrystalDecisions. [Shared].ExportFormatType.PortableDocFormat
  '这里我们导出成为.pdf格式文件,你也能选择上面的其它类型文件
    DiskOpts.DiskFileName = "c:\Output.pdf"
  '如果你不指定确切的目录,那么文件就会保存到[Windows]\System32目录中去了
    myReport.ExportOptions.DestinationOptions = DiskOpts
  '水晶报表文件不包含直接的FileName属性,因此你不能直接指定保存的文件名
  '所以你不得不使用DiskFileDestinationOptions对象,设置它的DiskFileName属性 

'为你想要的路径,最后将水晶报表的DestinationsOptions属性指定为上面的DiskFileDestinationOption
    myReport.Export()
  '上面的代码将完成导出工作。
End Sub

使用PUSH模式导出水晶报表,当导出的报表是由PUSH模式建立的时,第一步就是通过编程建立连接并组装DataSet,设置报表的的SetDataSource属性。再下面的步骤就有Pull模式一样的了。

 

来源:中国自学编程网

相关

服务器Microsoft
本评论 更新于:2026-4-13 20:37:14
在C114 APP中与业内人士畅聊通信行业热点话题!