Skip to main content

TdxCustomSpreadSheet.SaveToStream(TStream,TdxSpreadSheetCustomFormatClass) Method

Exports the current spreadsheet document to a stream.

Declaration

procedure SaveToStream(AStream: TStream; AFormat: TdxSpreadSheetCustomFormatClass = nil); virtual;

Parameters

Name Type Description
AStream TStream

The target stream.

AFormat TdxSpreadSheetCustomFormatClass

Optional. The target spreadsheet document format. This parameter accepts a reference to the corresponding TdxSpreadSheetCustomFormat class descendant to select the required format.

If you omit this parameter, the procedure saves the current document using the internal Spreadsheet control binary format.

Refer to the following section for a complete list of available options: Available Export Formats.

Remarks

Call the SaveToStream procedure to export the current spreadsheet document to a stream in any supported format.

Available Export Formats

Pass the following class-references as the optional AFormat parameter to save content in the corresponding format:

TdxSpreadSheetBinaryFormat
Default. Internal Spreadsheet control binary format. The SaveToStream procedure uses this format if you omit the optional AFormat parameter.
TdxSpreadSheetCSVFormat
Comma-Separated Values (CSV) format.
TdxSpreadSheetHTMLFormat | TdxSpreadSheetHTMFormat
Hypertext Markup Language (HTML/HTM) format.
TdxSpreadSheetTXTFormat
Plain text format (TXT).
TdxSpreadSheetXLSFormat | TdxSpreadSheetXLTFormat
Microsoft Excel® Binary Format (XLS/XLT).
TdxSpreadSheetXLSXFormat | TdxSpreadSheetXLTXFormat
Office OpenXML Spreadsheet Format (XLSX/XLTX).
TdxSpreadSheetXMLFormat
Extensible Markup Language (XML) format.

Code Examples

Export Spreadsheet Content to XLSX File

The following code example exports the current spreadsheet document to a file in the Office OpenXML Spreadsheet (XLSX) format:

uses
  dxSpreadSheet,              // Declares the TdxSpreadSheet control
  dxShellDialogs,             // Declares the TdxSaveFileDialog component
  dxSplashForms;              // Declares the TdxSplashFormManager class and related types
// ...

procedure TMyForm.cxButtonExportToXLSXClick(Sender: TObject);
var
  AStream: TMemoryStream;
begin
  if not dxSaveFileDialog1.Execute(Handle) then Exit;   // Displays the "Save File" dialog
  AStream := TMemoryStream.Create;      // Creates a stream as an intermediary container
  // Displays a Wait Form before export operation
  TdxSplashFormManager.WaitForm.Show(dxSpreadSheet1.Parent);
  try
    dxSpreadSheet1.SaveToStream(AStream, TdxSpreadSheetXLSXFormat); // Exports content to a stream
    AStream.SaveToFile(dxSaveFileDialog1.FileName);   // Saves XLSX stream content to a file
  finally
    AStream.Free;                       // Releases the intermediary memory stream
    TdxSplashFormManager.WaitForm.Hide; // Hides the Wait Form once the operation is complete
  end;
end;

Copy XLSX Documents Between Spreadsheet Controls

The following code example copies a document between two TdxSpreadSheet control instances (from dxSpreadSheet1 to dxSpreadSheet2) using an intermediary memory stream in the Office OpenXML Spreadsheet (XLSX) format:

uses
  dxSpreadSheet,              // Declares the TdxSpreadSheet control
  dxSplashForms;              // Declares the TdxSplashFormManager class and related types
// ...

procedure TMyForm.cxButtonExportToXLSXClick(Sender: TObject);
var
  AStream: TMemoryStream;
begin
  AStream := TMemoryStream.Create;      // Creates a stream as an intermediary container
  // Displays a Wait Form before export and import operations
  TdxSplashFormManager.WaitForm.Show(dxSpreadSheet1.Parent);
  try
    dxSpreadSheet1.SaveToStream(AStream, TdxSpreadSheetXLSXFormat); // Exports content
    AStream.Position := 0;              // Resets stream position
    dxSpreadSheet2.LoadFromStream(AStream, TdxSpreadSheetXLSXFormat); // Imports content
  finally
    AStream.Free;                       // Releases the intermediary memory stream
    TdxSplashFormManager.WaitForm.Hide; // Hides the Wait Form once operations are complete
  end;
end;
See Also