|
8 | 8 | System.SysUtils, |
9 | 9 | System.Classes, |
10 | 10 | System.IniFiles, |
11 | | - dxBackend, |
| 11 | + dxBackend, // DevExpress Reports and Dashboards backend |
12 | 12 | dxBackend.Bundled, |
13 | 13 | dxBackend.ConnectionString.SQL, |
14 | | - dxReport; |
| 14 | + dxReport, // DevExpress Reports frontend |
| 15 | + dxReport.Parameters; |
15 | 16 |
|
16 | 17 | procedure ExportReportToPdf; |
17 | 18 | var |
18 | | - AManager: TdxBackendDataConnectionManager; |
| 19 | + AConfig: TIniFile; |
19 | 20 | AConnection: TdxBackendDatabaseSQLConnection; |
20 | 21 | AReport: TdxReport; |
21 | | - |
| 22 | + AReportParameter: TdxReportParameter; |
22 | 23 | AStream: TMemoryStream; |
23 | | - |
24 | | - AIni: TIniFile; |
25 | | - |
26 | | - AParamName: string; |
27 | | - AKeys: TStringList; |
28 | | - I: Integer; |
29 | 24 | begin |
30 | | - TdxBackend.Instance.Start; // Start the backend |
| 25 | + // Start the DevExpress backend. |
| 26 | + TdxBackend.Instance.Start; |
31 | 27 |
|
32 | | - AManager := TdxBackendDataConnectionManager.Create(nil); |
33 | | - AIni := TIniFile.Create('..\PDFReportGenerator.ini'); |
| 28 | + // Read the configuration file. |
| 29 | + AConfig := TIniFile.Create('..\PDFReportGenerator.ini'); |
34 | 30 | try |
35 | | - AConnection := TdxBackendDatabaseSQLConnection(AManager.DataConnections. |
36 | | - Add(TdxBackendDatabaseSQLConnection)); |
37 | | - |
38 | | - AConnection.DisplayName := AIni.ReadString('Connection', 'Name', ''); |
39 | | - AConnection.ConnectionString := |
40 | | - AIni.ReadString('Connection', 'ConnectionString', ''); |
41 | | - AConnection.Active := True; |
42 | | - |
43 | | - AReport := TdxReport.Create(nil); |
| 31 | + // Create a database connection. |
| 32 | + AConnection := TdxBackendDatabaseSQLConnection.Create(nil); |
44 | 33 | try |
45 | | - AReport.Layout.LoadFromFile(AIni.ReadString('Report', 'LayoutFileName', '')); |
46 | | - AReport.ReportName := 'Report'; |
47 | | - |
48 | | - AReport.LoadParametersFromReport; |
| 34 | + // Assign the name "MS SQL Connection" to the database connection. |
| 35 | + // As part of this operation, the connection manager registers the connection and makes it available to reports. |
| 36 | + AConnection.DisplayName := AConfig.ReadString('Connection', 'Name', ''); |
| 37 | + // Assign the specified connection string to the database connection. |
| 38 | + AConnection.ConnectionString := AConfig.ReadString('Connection', 'ConnectionString', ''); |
49 | 39 |
|
50 | | - AKeys := TStringList.Create; |
| 40 | + // Create a new DevExpress Report. |
| 41 | + AReport := TdxReport.Create(nil); |
51 | 42 | try |
52 | | - for I := 0 to AKeys.Count - 1 do |
53 | | - begin |
54 | | - AParamName := AKeys[I]; |
| 43 | + // Assign a name to the newly created report. |
| 44 | + AReport.ReportName := 'Report'; |
| 45 | + // Load report layout from a `.repx` layout file. |
| 46 | + // Report layout contains visual elements and data bindings: |
| 47 | + // - List of parameters used to filter data obtained from a database and displayed in the report. |
| 48 | + // - Database connection name and SQL query to obtain data from a database. |
| 49 | + AReport.Layout.LoadFromFile(AConfig.ReadString('Report', 'LayoutFileName', '')); |
| 50 | + |
| 51 | + // Read a list of parameter names from the layout file. |
| 52 | + // (Presented layout file has a single parameter named `CategoryNameReportParameter`.) |
| 53 | + AReport.LoadParametersFromReport; |
55 | 54 |
|
56 | | - if AReport.Parameters.ParamByName[AParamName] <> nil then |
57 | | - AReport.Parameters |
58 | | - .ParamByName[AKeys[I]] |
59 | | - .Value := AIni.ReadString('Parameters', AParamName, ''); |
60 | | - end; |
61 | | - finally |
62 | | - AKeys.Free; |
63 | | - end; |
| 55 | + // For each parameter, read and assign its value from the configuration file. |
| 56 | + // Parameter values are used to filter data obtained from a database. |
| 57 | + // The presented technique allows you to produce various reports by filtering data with parameters. |
| 58 | + for AReportParameter in AReport.Parameters do |
| 59 | + AReportParameter.Value := AConfig.ReadString('Parameters', AReportParameter.Name, ''); |
64 | 60 |
|
65 | | - AStream := TMemoryStream.Create; |
66 | | - try |
67 | | - AReport.ExportToPDF(AStream); |
68 | | - AStream.SaveToFile(AIni.ReadString('Report', 'OutputFileName', '')) |
| 61 | + // Create a memory stream to hold the exported PDF data. |
| 62 | + AStream := TMemoryStream.Create; |
| 63 | + try |
| 64 | + // Export the report to PDF format and save it to the memory stream. |
| 65 | + AReport.ExportToPDF(AStream); |
| 66 | + // Write the content of the memory stream to an output file. |
| 67 | + // The file name is specified in the configuration file. |
| 68 | + AStream.SaveToFile(AConfig.ReadString('Report', 'OutputFileName', '')); |
| 69 | + finally |
| 70 | + AStream.Free; |
| 71 | + end; |
69 | 72 | finally |
70 | | - AStream.Free; |
| 73 | + AReport.Free; |
71 | 74 | end; |
72 | 75 | finally |
73 | | - AReport.Free; |
| 76 | + AConnection.Free; |
74 | 77 | end; |
75 | 78 | finally |
76 | | - AManager.Free; |
77 | | - AIni.Free; |
| 79 | + AConfig.Free; |
78 | 80 | end; |
79 | 81 | end; |
80 | 82 |
|
81 | 83 | begin |
82 | 84 | try |
83 | | - { TODO -oUser -cConsole Main : Insert code here } |
84 | 85 | ExportReportToPdf; |
85 | 86 | except |
86 | 87 | on E: Exception do |
|
0 commit comments