Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

19.08 Exporting to HTML

Exporting to HTML

HTML output is inherently different from exporting to the other file formats. HTML files are meant to be viewed in a web browser and this can impose certain requirements on how you present the data to the user. You have the option of displaying the entire report in a single browser window or breaking it up into separate web pages. If you display the report on a single page, the user can view all the data at one time. But this requires a lot of scrolling to see everything. If you decide to break up the report into separate pages, you have to decide whether you will provide your own interface for navigating between the pages or whether you want page navigation links to be automatically added to the bottom of each page. Of course, doing it automatically is a much easier solution to implement, but you have to consider whether this fits in with the design of your entire web site. Luckily, each of these options is easy to set and you can quickly play around with each one and decide what works best for each project.

The report viewer control doesn’t have HTML listed in its dropdown list of available export file types. Exporting to HTML always requires writing your own code.

Table 19-6 lists the HTML export specific properties that control the output of the HTML pages.

Table 19-6. HTMLFormatOptions Properties.

Property Description
FirstPageNumber The first page number to export.
HTMLEnableSeparatedPages Boolean that sets whether the HTML output will put each report page on its own web page.
HTMLBaseFolderName The folder for storing the HTML files.
HTMLFileName The filename used for saving the HTML output.
HTMLHasPageNavigator Boolean that sets whether the bottom of each page should have navigation links.
LastPageNumber The last page number to export.
UsePageRange Boolean that enables/disables the use of page ranges.

Listing 19-3. Setting the format options for HTML.
[VB.NET]
Public Sub ExportToHTML(ByVal HTMLBaseFolderName As String, ByVal Filename As String, ByRef myReport As CrystalDecisions.CrystalReports.Engine.ReportDocument, ByVal EnableSeparatedPages As Boolean, ByVal HasPageNavigator As Boolean)
Dim myExportOptions As New CrystalDecisions.Shared.ExportOptions
'Set HTML specific formatting properties
myExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.HTML40
Dim myFormatOptions As CrystalDecisions.Shared.HTMLFormatOptions
myFormatOptions = CrystalDecisions.Shared.ExportOptions.CreateHTMLFormatOptions()
myFormatOptions.HTMLBaseFolderName = HTMLBaseFolderName
myFormatOptions.HTMLFileName = Filename
myFormatOptions.HTMLHasPageNavigator = HasPageNavigator
myFormatOptions.HTMLEnableSeparatedPages = EnableSeparatedPages
'Assign the formatting properties to the report and export it
myExportOptions.FormatOptions = myFormatOptions
myReport.Export(myExportOptions)
End Sub
[C#]
public void ExportToHTML(string HTMLBaseFolderName, string Filename, ref CrystalDecisions.CrystalReports.Engine.ReportDocument myReport, bool EnableSeparatedPages, bool HasPageNavigator)
{
CrystalDecisions.Shared.ExportOptions myExportOptions = new CrystalDecisions.Shared.ExportOptions();
//Set HTML specific formatting properties
myExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.HTML40;
CrystalDecisions.Shared.HTMLFormatOptions myFormatOptions;
myFormatOptions = CrystalDecisions.Shared.ExportOptions.CreateHTMLFormatOptions();
myFormatOptions.HTMLBaseFolderName = HTMLBaseFolderName;
myFormatOptions.HTMLFileName = Filename;
myFormatOptions.HTMLHasPageNavigator = HasPageNavigator;
myFormatOptions.HTMLEnableSeparatedPages = EnableSeparatedPages;
//Assign the formatting properties to the report and export it
myExportOptions.FormatOptions = myFormatOptions;
myReport.Export(myExportOptions);
}

Unlike the previous code listings, this listing doesn’t specify a disk file destination type. This is because HTML output is stored in a separate folder and these properties are already part of the format options object. A separate object isn’t necessary to specify them.

There are two properties that deal with the filename that should be mentioned. The HTMLBaseFolderName property sets the folder that the HTML output is saved in. However, this is a little deceiving because the export process creates another folder within the base and puts the HTML files in this sub-folder. Unfortunately, you don’t have any control over the name of this sub-folder. It is always given the report name. For example, if the report is called “EmployeeList.rpt”, the subfolder is named “EmployeeList”. If you leave the HTMLBaseFolderName property empty, only the subfolder is created.

You also have to be aware of how the HTML files are named. If the property HTMLUseSeperatedPages is True, the pages will be named according to the following rules:

  • The first page is the filename you specified (e.g. Report.htm).
  • The next pages have a number concatenated at the end (e.g. Report1.htm, Report2.htm…)
  • The last page has the word “Last” concatenated at the end (e.g. ReportLast.htm).

If you print a page range, the numbers will not match the actual page number. The first page has no number and the second page printed is numbered as “1”, regardless of its actual page number on the report. As an example, if you print pages 5 through 10, page 5 is named Report.htm; page 6 is named Report1.htm; and page 10 is named ReportLast.htm.

As an example of how confusing the folder names are, Figure 19-5 shows a snapshot of an HTML report that used a base folder name of “Crystal HtmlFiles”; the report name is “EmployeeList”; and the HTML file name is “Employees”.





Figure 19-5. The folder/file naming convention for exporting HTML.