Skip to content

DevExpress-Examples/vcl-reports-non-interactive-export

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DevExpress VCL Reports — Generate Reports in a Backend / Service Application

This example uses DevExpress Reports for Delphi/C++Builder to generate reports in a command line application. The application accepts an order ID parameter, queries the database for order data, and exports an "order" report as a PDF file.

This example bypasses the DevExpress Report Viewer dialog and generates a report using the DevExpress Reports backend. Use the approach demonstrated in this sample project to implement REST/Web API backends, Windows Services, workflows, and scheduled jobs. This approach can be particularly beneficial for the following usage scenarios (reports generated without direct user interaction):

  • Mass-export reports to PDF, DOCX, image, and other formats.
  • Share, email, and print report documents silently.
  • Implement custom report management UIs.

A PDF report example generated by a command line application (demonstrates order number 11077)


Prerequisites

DevExpress Reports Prerequisites

Test the Example

  1. Open and build the Delphi project in the RAD Studio.

  2. Start a console session and navigate to the Delphi project directory.

  3. Run the app and specify the order ID (a number between 10248 and 11077):

    > PDFReportGenerator.exe 11077
    
    Report saved to: Order_11077.pdf
    
  4. Generate reports for other order IDs and compare generated PDF files.

Implementation Details

Follow the steps below to create an application that imports a report layout from a file, binds the layout to data, and exports the generated report to a PDF file.

Step 1: Initialize a Report and Import a Report Layout

An application requires a template report layout previously created in the Report Designer. You can import a report layout from a REPX file or load a layout from a database.

This example imports a report layout from the Order.repx file.

Delphi:

AReport := TdxReport.Create(nil); // AReport: TdxReport;
try
    // Set an internal report name (does not affect the exported content)
    AReport.ReportName := 'Order Report';
    // Load the report layout from the specified file
    AReport.Layout.LoadFromFile('Order.repx');
    // ...
finally
    AReport.Free;
end;

Step 2: Create a Database Connection

Create a database connection component to supply data to the report. This example uses a SQLite sample database (nwind.db).

Delphi:

// AConnection: TdxBackendDatabaseSQLConnection;
AConnection := TdxBackendDatabaseSQLConnection.Create(nil);
try
    // Assign a database name matching the name specified in the report layout
    AConnection.DisplayName := 'NWindConnectionString';
    // Assign a connection string required to use the local SQLite database
    AConnection.ConnectionString := 'XpoProvider=SQLite; Data Source=nwind.db; Mode=ReadOnly';
    // ...
finally
    AConnection.Free;
end;

For detailed information on data source management and supported database engines, refer to the following help topic: VCL Backend: Supported Database Systems.

Step 3: Define Report Parameter Values

A report layout may include one or more parameters. Parameters allow you to modify database queries and generate different reports based on the same report template and underlying data. For example, Order.repx includes a single OrderIDParameter that filters data by order ID.

To modify parameters, load them using the TdxReport.Parameters.LoadFromLayout method and assign values to TdxReport.Parameters list members as follows:

Delphi:

AReport.Parameters.LoadFromLayout;
// Set the "OrderIdParameter" value in the report layout
AReport.Parameters['OrderIdParameter'].Value := AOrderID;

Step 4: Export Report Content to a File

This example exports a report to a PDF file using the TdxReport.ExportToPDF method:

Delphi:

AStream := TMemoryStream.Create; // AStream: TMemoryStream;
try
    // Export a report to a memory stream in the PDF format
    AReport.ExportToPDF(AStream);
    // Save memory stream content to a file
    AStream.SaveToFile('Order_' + AOrderID + '.pdf');
finally
    AStream.Free;
end;

For detailed information on available export formats, refer to the following help topic: TdxReport.ExportTo.

Export Multiple Reports

The approach outlined in this example allows you to generate and export multiple reports based on the same layout and data using a list of parameters. You need to initialize the report layout and data connection once (steps 1 and 2) and repeat steps 3 and 4 for each parameter.

Delphi:

// ...
AReport.LoadParametersFromReport;

for AOrderID in AOrderIDList:
    AReport.Parameters['OrderIdParameter'].Value := AOrderID;

    AStream := TMemoryStream.Create; // AStream: TMemoryStream;
    try
        // Export a report to a memory stream in the PDF format
        AReport.ExportToPDF(AStream);
        // Save memory stream content to a file
        AStream.SaveToFile('Order_' + AOrderID + '.pdf');
    finally
        AStream.Free;
    end;
end;

Files to Review

Documentation

More Examples

Does This Example Address Your Development Requirements/Objectives?

(you will be redirected to DevExpress.com to submit your response)

About

Generate DevExpress VCL Reports in backend and service applications.

Topics

Resources

License

Stars

Watchers

Forks

Contributors

Languages