在 Visual Studio .NET中使用Crystal Report(下) (cashcho翻譯)

來源:互聯網
上載者:User
visual Visual Studio .NET 中使用 Crystal Report( )







from www.aspfree.com
translated by cash(天下第七)
cashcao@msn.com(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 compliant)



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(如前所述)。然後就可以調用上面所示的代碼了。




相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。