vcl-dxreport-dot-tdxreport-dot-exporttocsv-x28-system-dot-classes-dot-tstream-x29.md
Exports report content to a stream in the comma-separated values (CSV) format.
procedure ExportToCSV(AStream: TStream);
| Name | Type | Description |
|---|---|---|
| AStream | TStream |
The target stream.
|
Call the ExportToCSV procedure to export report content to a stream in the CSV format.
The Report Designer dialog allows you to configure CSV export settings (encoding, separator, export mode, etc.). Open the Properties tab and expand the following nodes to modify CSV export settings: Behavior | Export Options | CSV Export Options.
The following code example configures a memory-based data source (TdxBackendInMemoryJSONConnection), loads an XML-based (REPX) report layout, and export generated report (TdxReport) content to a file in the CSV format using an intermediary TMemoryStream object:
uses
dxBackend.ConnectionString.JSON, // Declares the TdxBackendInMemoryJSONConnection component
dxReport, // Declares the TdxReport component
dxShellDialogs; // Declares the TdxSaveFileDialog component
// ...
procedure TMyForm.cxButtonExportToCSVClick(Sender: TObject);
var
AReport: TdxReport;
AJSONDataConnection: TdxBackendInMemoryJSONConnection;
AStream: TMemoryStream;
AJSONData: string;
begin
// Define a table that consists of three columns ("id", "Region", and "Sales") and five data rows
AJSONData :=
'[{"id": 1, "Region": "Asia", "Sales": 4.7685},' + // Row #1
'{"id": 2, "Region": "Australia", "Sales": 1.9576},' + // Row #2
'{"id": 3, "Region": "Europe", "Sales": 3.3579},' + // Row #3
'{"id": 4, "Region": "North America", "Sales": 3.7477},' + // Row #4
'{"id": 5, "Region": "South America", "Sales": 1.8237}]'; // Row #5
if not dxSaveFileDialog1.Execute(Handle) then Exit; // Displays the "Save File" dialog
AJSONDataConnection: := TdxBackendInMemoryJSONConnection.Create(Self); // Creates a data connection
try
AJSONDataConnection.Name := 'JSONData'; // Assigns a name to the created data connection
AJSONDataConnection.SetJSONValue(AJSONData); // Assigns the defined JSON data string
AReport := TdxReport.Create(Self); // Creates a TdxReport component
try
AReport.Layout.LoadFromFile('MyReportLayout.repx'); // Loads an XML-based report layout
AReport.ReportName := 'MyReport'; // Defines a report name
AStream := TMemoryStream.Create; // Creates a stream as an intermediary container
try
AReport.ExportToCSV(AStream); // Exports content to a stream in the CSV format
AStream.SaveToFile(dxSaveFileDialog1.FileName); // Saves CSV stream content to a file
finally
AStream.Free; // Releases the intermediary memory stream
end;
finally
AReport.Free; // Releases the TdxReport component
end;
finally
AJSONDataConnection.Free; // Releases the data connection component
end;
end;
#include "dxBackend.ConnectionString.JSON.hpp" // Declares the TdxBackendInMemoryJSONConnection component
#include "dxReport.hpp" // Declares the TdxReport component
#include "dxShellDialogs.hpp" // Declares the TdxSaveFileDialog component
// Add the following linker directives to the corresponding CPP source file:
#pragma link "dxReport" // Required to use dxDashboard.Control.hpp declarations
#pragma link "dxShellDialogs" // Required to use dxShellDialogs.hpp declarations
#if defined(_WIN64) // Required to use dxBackend.ConnectionString.JSON.hpp declarations
#pragma link "dxBackend.ConnectionString.JSON.o"
#else
#pragma link "dxBackend.ConnectionString.JSON.obj"
#endif
// ...
void __fastcall TMyForm::cxButtonExportToCSVClick(TObject *Sender)
{
TdxReport *AReport;
TdxBackendInMemoryJSONConnection *AJSONDataConnection;
TMemoryStream *AStream;
// Define a table that consists of three columns ("id", "Region", and "Sales") and five data rows
UnicodeString AJSONData =
"[{\"id\": 1, \"Region\": \"Asia\", \"Sales\": 1.9576}," // Row #1
"{\"id\": 2, \"Region\": \"Australia\", \"Sales\": 1.9576}," // Row #2
"{\"id\": 3, \"Region\": \"Europe\", \"Sales\": 3.3579}," // Row #3
"{\"id\": 4, \"Region\": \"North America\", \"Sales\": 3.7477}," // Row #4
"{\"id\": 5, \"Region\": \"South America\", \"Sales\": 1.8237}]" // Row #5
if(!dxSaveFileDialog1->Execute(Handle)) { return; } // Displays the "Save File" dialog
AJSONDataConnection = new TdxBackendInMemoryJSONConnection(this); // Creates a data connection
try
{
AJSONDataConnection->Name = "JSONData"; // Assigns a name to the created data connection
AJSONDataConnection->SetJSONValue(AJSONData); // Assigns the defined
AReport = new TdxReport(this);
try
{
AReport->Layout->LoadFromFile("MyReportLayout.repx"); // Loads an XML-based report layout
AReport->ReportName = "MyReport"; // Defines a report name
AStream = new TMemoryStream(); // Creates a stream as an intermediary container
try
{
dxReport1->ExportToCSV(AStream); // Exports content to a stream in the CSV format
AStream->SaveToFile(dxSaveFileDialog1->FileName); // Saves CSV stream content to a file
}
__finally
{
delete AStream; // Releases the intermediary memory stream
}
}
__finally
{
delete AReport; // Releases the TdxReport component
}
}
__finally
{
delete AJSONDataConnection; // Releases the data connection component
}
}
To see the report export functionality in action, run the Report Designer/Viewer demo in the VCL Demo Center installed with compiled DevExpress VCL demos. Select any demo in the sidebar on the left, click the Export button, and use any export option listed in the menu.
Tip
You can find full source code for the installed compiled Report demo in the following folder:
_%PUBLIC%\Documents\DevExpress VCL Demos\MegaDemos\Product Demos\ExpressReports_
You can call the following procedures to export report content to a stream in other formats:
ExportToExports report content to a stream in any supported format.ExportToDOCXExports report content to a stream as an Office OpenXML (DOCX) document.ExportToHTMLExports report content to a stream as an HTML document.ExportToImage
Exports report content to a stream in the current image export format (selected using the Report Designer dialog).
The default image export format is PNG (Portable Network Graphics).
ExportToMHTExports report content to a stream as a MIME HTML (MHT) document.ExportToPDFExports report content to a stream in PDF format.ExportToRTFExports report content to a stream as an RTF-formatted string.ExportToTextExports report content to a stream as plain text.ExportToXLSExports report content to a stream in Microsoft Excel® binary format (XLS).ExportToXLSXExports report content to a stream in Office Open XML spreadsheet format (XLSX). See Also
TdxCustomDashboardControl.ExportToCSV Procedure