Example 8: Exporting to Excel with progress

Uses ExportToXLSX to write the grid’s data to an Excel workbook, and OnExportProgress to show the progress.

Scenario

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.

Units

Besides EgFireGrid: System.IOUtils (TPath), System.SysUtils (Exception) and FMX.DialogService (TDialogService).

The handlers

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;

Why it works this way

See Also

(C) 2026 Easygate, Lda