ActiveReports supports exporting with tables in the RichText control. RichText controls can be loaded with an RTF file that contains tables, or even an HTML file, and the resulting report can be exported to any of a number of formats.
This walkthrough illustrates how to create and export a report which contains the RichText control.
This walkthrough is split up into the following activities:
- Adding an ActiveReport to the Visual Studio project
- Adding the appropriate export filters to your project
- Adding controls to the report to contain data
- Loading an RTF file into a RichText control
- Creating an HTML file to be loaded into a RichText control
- Loading the HTML file into a RichText control
- Viewing the report
- Using the export methods to export data to all available file formats
To complete the walkthrough, you will need an RTF file which contains a table.
Adding an ActiveReport to the Visual Studio project
To add an ActiveReport to your project
- Open a new project in Visual Studio.
- From the Project menu, select Add New Item.
- Select ActiveReports 3.0 File and rename the file rptRich.
- Click Open.
Adding the export filters to your project
To add the export filters to your project
- Click on the following export controls one at a time in the appropriate toolbox to select them.
- HtmlExport
- PdfExport
- RtfExport
- TextExport
- TiffExport
- XlsExport
- Drag each control onto the Windows Form.
- The controls will appear in the component tray below the Windows Form.
Adding controls to the report to contain data
To add controls to the report
- Add the following controls to the Detail section:
Control Name Size Location RichTextBox RichTextBoxRTF 3, 1.5 0, 0 RichTextBox RichTextBoxHTML 3, 1.5 3.25, 0
Creating an RTF file to be loaded into a RichText control
To write a quick RTF file
- Open Wordpad, and paste the following text into it.
Customer List by Country
Argentina
- Rancho grande
- Océano Atlántico Ltda.
- Cactus Comidas para llevar
Austria
- Piccolo und mehr
- Ernst Handel
Belgium
- Suprêmes délices
- Maison Dewey
Brazil
- Familia Arquibaldo
- Wellington Improtadora
- Que Delícia
- Tradição Hipermercados
- Ricardo Adocicados
- Hanari Carnes
- Queen Cozinha
- Comércio Mineiro
- Gourmet Lanchonetes
Month Sales October $4,872 November $8,517 December $9,623 - Save the file as "sample.rtf" in the bin directory (VB) or the debug directory in the bin (C#) of your project.
Loading an RTF file into a RichText control
To load an RTF file into the RichText control
To write the code in Visual Basic or C#
- Double-click in the detail section of of rptRich. This creates an event-handling method for rptRich's Detail_Format event. Add code to the handler to:
- Load an RTF file into the RichText control
The following example shows what the code for the method looks like.
'Visual Basic Private Sub Detail1_Format(ByVal sender As Object, ByVal e As System.EventArgs) Handles _ Detail1.Format Dim streamRTF As New System.IO.FileStream(System.Windows.Forms.Application.StartupPath _ + "\sample.rtf", System.IO.FileMode.Open) Me.RichTextBoxRTF.Load(streamRTF, RichTextType.Rtf) End Sub
//C# private void detail_Format(object sender, System.EventArgs eArgs) { System.IO.FileStream streamRTF = new System.IO.FileStream(System.Windows.Forms.Application .StartupPath + "\\sample.rtf", System.IO.FileMode.Open); this.RichTextBoxRTF.Load(streamRTF, RichTextType.Rtf); }
- Load an RTF file into the RichText control
Creating an HTML file to be loaded into a RichText control
To write a quick HTML file
- Open Notepad and paste the following code into it.
<html> <body> <center><h1>Customer List by Country</h1></center> <h2>Argentina</h2> <ul> <li>Rancho grande <li>Océano Atlántico Ltda. <li>Cactus Comidas para llevar </ul> <h2>Austria</h2> <ul> <li>Piccolo und mehr <li>Ernst Handel </ul> <h2>Belgium</h2> <ul> <li>Suprêmes délices <li>Maison Dewey </ul> <h2>Brazil</h2> <ul> <li>Familia Arquibaldo <li>Wellington Improtadora <li>Que Delícia <li>Tradição Hipermercados <li>Ricardo Adocicados <li>Hanari Carnes <li>Queen Cozinha <li>Comércio Mineiro <li>Gourmet Lanchonetes </ul> <table> <tr><th>Month</th><th>Sales</th></tr> <tr><td>October</td><td>$4,872</td></tr> <tr><td>November</td><td>$8,517</td></tr> <tr><td>December</td><td>$9,623</td></tr> </table> </body> </html>
- Save the file as "sample.html" in the bin directory (VB) or the debug directory in the bin (C#) of your project.
Loading the HTML file into a RichText control
To write the code in Visual Basic or C#
- Right-click in any section of the design window of rptRich, and click on View Code to display the code view for the report. In the event-handling method for rptRich's Detail_Format event created previously, add code to the handler to:
- Load the HTML file into the RichText control
The following example shows what the code for the method looks like.
'Visual Basic Private Sub Detail1_Format(ByVal sender As Object, ByVal e As System.EventArgs) Handles _ Detail1.Format Dim streamRTF As New System.IO.FileStream(System.Windows.Forms.Application.StartupPath _ + "\sample.rtf", System.IO.FileMode.Open) Me.RichTextBoxRTF.Load(streamRTF, RichTextType.Rtf) Dim streamHTML As New System.IO.FileStream(System.Windows.Forms.Application.StartupPath + _ "\sample.HTML", System.IO.FileMode.Open) Me.RichTextBoxHTML.Load(streamHTML, RichTextType.Html) End Sub //C# private void detail_Format(object sender, System.EventArgs eArgs) { System.IO.FileStream streamRTF = new System.IO.FileStream(System.Windows.Forms .Application.StartupPath + "\\sample.rtf", System.IO.FileMode.Open); this.RichTextBoxRTF.Load(streamRTF, RichTextType.Rtf); System.IO.FileStream streamHTML = new System.IO.FileStream(System.Windows.Forms .Application.StartupPath + "\\sample.html", System.IO.FileMode.Open); this.RextTextBoxHTML.Load(streamHTML, RichTextType.Html); }
- Load the HTML file into the RichText control
Viewing the report
To view the report
- Add the ActiveReports viewer control to the Windows Form..
- Set the viewer control's Dock property to Fill.
- Add the code needed to set the viewer document equal to the report document.
To write the code for the viewer in Visual Basic or C#
- Double-click the center of the viewer control. This creates an event-handling method for the Viewer Load event. Add code to the handler to:
- Format the viewer to show the report when it is run
The following example shows what the code for the method looks like.
'Visual Basic Private Sub Viewer1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles _ Viewer1.Load Dim rpt As New rptRich() Viewer1.Document = rpt.Document rpt.Run() End Sub
//C# private void viewer1_Load(object sender, System.EventArgs e) { rptRich rpt = new rptRich(); this.viewer1.Document = rpt.Document; rpt.Run(); }
- Format the viewer to show the report when it is run
Using the export methods to export data to all available file formats
To write the code in Visual Basic or C#
- In the event-handling method for the Viewer1_Load event created previously, add code to the handler to:
- Export the report to all available file formats
The following example shows what the code for the method looks like.
'Visual Basic Private Sub Viewer1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles arv.Load Dim rpt As New rptRich rpt.Run() Viewer1.Document = rpt.Document Me.HtmlExport1.Export(rpt.Document, Application.StartupPath + "\\HTMLExpt.html") Me.PdfExport1.Export(rpt.Document, Application.StartupPath + "\\PDFExpt.pdf") Me.RtfExport1.Export(rpt.Document, Application.StartupPath + "\\RTFExpt.rtf") Me.TextExport1.Export(rpt.Document, Application.StartupPath + "\\TextExpt.txt") Me.TiffExport1.Export(rpt.Document, Application.StartupPath + "\\TIFFExpt.tiff") Me.XlsExport1.Export(rpt.Document, Application.StartupPath + "\\XLSExpt.xls") End Sub
//C# private void viewer1_Load(object sender, System.EventArgs e) { rptRich rpt = new rptRich(); rpt.Run(); viewer1.Document = rpt.Document; this.htmlExport1.Export(rpt.Document, Application.StartupPath + "\\HTMLExpt.html"); this.pdfExport1.Export(rpt.Document, Application.StartupPath + "\\PDFExpt.pdf"); this.rtfExport1.Export(rpt.Document, Application.StartupPath + "\\RTFExpt.rtf"); this.textExport1.Export(rpt.Document, Application.StartupPath + "\\TextExpt.txt"); this.tiffExport1.Export(rpt.Document, Application.StartupPath + "\\TIFFExpt.tiff"); this.xlsExport1.Export(rpt.Document, Application.StartupPath + "\\XLSExpt.xls"); }
- Export the report to all available file formats