Uses ExportToXLSX
to write the grid’s data to an Excel workbook, and OnExportProgress
to show the progress.
An Export button writes the products to
Products.xlsx: only the selected rows when some are
selected, all of them otherwise. A progress bar follows the export, and
the file name is shown at the end.
Besides EgFireGrid: System.IOUtils
(TPath), System.SysUtils
(Exception) and FMX.DialogService
(TDialogService).
procedure TForm1.BtnExportClick(Sender: TObject);
var
FileName: string;
begin
FileName := TPath.Combine(TPath.GetDocumentsPath, 'Products.xlsx');
ProgressBar1.Value := 0;
ProgressBar1.Visible := True;
try
try
// Memo text cut at 1000 characters; only the selected rows when there are any
Grid1.ExportToXLSX(FileName, 1000, Grid1.SelectedRowCount > 0);
TDialogService.ShowMessage('Exported to ' + FileName);
except
on E: Exception do
TDialogService.ShowMessage('Export failed: ' + E.Message);
end;
finally
ProgressBar1.Visible := False;
end;
end;
procedure TForm1.Grid1ExportProgress(Sender: TObject; var PercentDone: Integer);
begin
ProgressBar1.Value := PercentDone;
{$IFDEF MSWINDOWS}
Application.ProcessMessages; // lets the bar repaint while the export runs
{$ENDIF}
end;
True and a selection, only the selected ones.ACTION_SEND intent) to let the user open it
in another app.Application.ProcessMessages does; keep the user from
starting another action meanwhile (the button can be disabled during the
export).