Export (0) Print
Expand All

Customizing Export Options

Visual Studio .NET 2003

The Crystal Report Engine allows you to deliver reports in many different ways. Using the export option, you can send the report to a disk file, to an Exchange folder, or to MS Mail (MAPI). The report can be exported to Excel, Crystal Reports, Microsoft Word, HTML, Portable Document Format (PDF), or Rich Text (RTF) format.

There are three ways you can set up users' export options:

  • Leave the default Export Expert button on the CrystalReportViewer toolbar.
  • Create your own button that calls the Export Expert dialog box. This method allows you to hide the standard toolbar when you require none of its other functions or when you want to customize the interface. This is available only through the Windows Forms Viewer.
  • Write code to specify a particular export type and destination.

Exporting Reports with the Export Button

The Export button is located at the top left corner of the CrystalReportViewer toolbar. When users click the Export button, a dialog box prompts them for the export format.

The Export button appears automatically unless you disable the viewer toolbar (in which case you can either prompt users for export options, or use code to export with specific options).

Prompting Users for Export Options

When only the Export button is needed from the CrystalReportViewer toolbar, you can disable the toolbar and create an Export button of your own. This functionality is available only with the Windows Forms Viewer.

Creating a Customized Export Expert Button

  1. Add a Button control to the form.
  2. Double-click the Button control to specify code for the Click event. Depending on the language you are using, insert the appropriate code (from the examples below) in the corresponding source file.
    [Visual Basic]
    crystalReportViewer1.ExportReport ()
    
    [C#]
    crystalReportViewer1.ExportReport ();
    
    [C++]
    crystalReportViewer1->ExportReport ();
    
    [VJ#]
    crystalReportViewer1.ExportReport ();
    

    When users click the button, the Export dialog box will prompt them to choose one of the various export format options offered by Crystal Reports for Visual Studio .NET.

Setting Export Options through Code

You can set the report export options by setting the export destination, format, and file name within the code.

Note   There are no default values for DestinationType, FormatType, or DiskFileName; therefore, you must specify values for all three fields. This can be done through the report engine.

The following example exports the report to disk in Microsoft Excel 8.0 format.

To set export options

  1. Add a Button control to the form.
  2. For the Text property of the Button, type Export to MS Excel File.
  3. Double-click the Button control to specify code for the Click event. Depending on the language you are using, insert the appropriate code (from the examples below) in the corresponding source file.
    [Visual Basic]
    ' Declare variables and get the export options.
    Dim exportOpts As New ExportOptions()
    Dim diskOpts As New DiskFileDestinationOptions()
    Dim excelFormatOpts As New ExcelFormatOptions()
    exportOpts = Report.ExportOptions
    
    ' Set the excel format options.
    excelFormatOpts.ExcelTabHasColumnHeadings = true
    
    exportOpts.ExportFormatType = ExportFormatType.Excel
    exportOpts.FormatOptions = excelFormatOpts
    
    ' Set the export format.
    exportOpts.ExportFormatType = ExportFormatType.Excel
    
    exportOpts.ExportDestinationType = ExportDestinationType.DiskFile
    
    ' Set the disk file options.
    diskOpts.DiskFileName = fileName
    exportOpts.DestinationOptions = diskOpts
    
    Report.Export()
    
    [C#]
    // Declare variables and get the export options.
    ExportOptions exportOpts = new ExportOptions();
    ExcelFormatOptions excelFormatOpts = new ExcelFormatOptions ();
    DiskFileDestinationOptions diskOpts = new DiskFileDestinationOptions();
    exportOpts = Report.ExportOptions;
    
    // Set the excel format options.
    excelFormatOpts.ExcelUseConstantColumnWidth = true;
    exportOpts.ExportFormatType = ExportFormatType.Excel;
    exportOpts.FormatOptions = excelFormatOpts;
    
    // Set the disk file options and export.
    exportOpts.ExportDestinationType = ExportDestinationType.DiskFile;
    diskOpts.DiskFileName = fileName;
    exportOpts.DestinationOptions = diskOpts;
    
    Report.Export ();
    
    [C++]
    // Declare variables and get the export options.
    ExportOptions* exportOpts = new ExportOptions();
    ExcelFormatOptions* excelFormatOpts = new ExcelFormatOptions ();
    DiskFileDestinationOptions* diskOpts = new DiskFileDestinationOptions();
    exportOpts = Report->ExportOptions;
    
    // Set the excel format options.
    excelFormatOpts->ExcelUseConstantColumnWidth = true;
    exportOpts->ExportFormatType = ExportFormatType::Excel;
    exportOpts->FormatOptions = excelFormatOpts;
    
    // Set the disk file options and export.
    exportOpts->ExportDestinationType = ExportDestinationType::DiskFile;
    diskOpts->DiskFileName = "fileName";
    exportOpts->DestinationOptions = diskOpts;
    
    Report->Export ();
    
    [VJ#]
    // Declare variables and get the export options.
    ExportOptions exportOpts = new ExportOptions();
    ExcelFormatOptions excelFormatOpts = new ExcelFormatOptions ();
    DiskFileDestinationOptions diskOpts = new DiskFileDestinationOptions();
    exportOpts = Report.get_ExportOptions();
    
    // Set the excel format options.
    excelFormatOpts.set_ExcelUseConstantColumnWidth(true);
    exportOpts.set_ExportFormatType(ExportFormatType.Excel);
    exportOpts.set_FormatOptions(excelFormatOpts);
    
    // Set the disk file options and export.
    exportOpts.set_ExportDestinationType(ExportDestinationType.DiskFile);
    diskOpts.get_DiskFileName(fileName);
    exportOpts.set_DestinationOptions(diskOpts);
    
    Report.Export ();
    
Show:
© 2014 Microsoft