anchor.diff:    end;
   end;
   
  +function TmDBGridHelper.ConfirmFileOverwrite: boolean;
  +begin
  +  Result := MessageDlg('Confirm', 'The selected file already exists. Overwrite it?', mtConfirmation, mbYesNo, 0) = mrYes;
  +end;
  +
  +procedure TmDBGridHelper.ExportGridToFile(aFileType: String);
  +var
  +  fs : TFileStream;
  +  oldCursor : TCursor;
  +begin
  +  if aFileType = 'CSV' then
  +  begin
  +    FSaveDialog.DefaultExt:= 'csv';
  +    FSaveDialog.Filter:='Comma Separated Values files|*.csv';
  +  end
  +  else
  +  if aFileType = 'XLS' then
  +  begin
  +    FSaveDialog.DefaultExt:= 'xls';
  +    FSaveDialog.Filter:='Excel 97-2003 files|*.xls';
  +  end;
  +  if FSaveDialog.Execute then
  +  begin
  +    if FileExists(FSaveDialog.FileName) then
  +    begin
  +      if not ConfirmFileOverwrite then
  +        exit;
  +    end;
  +    try
  +      oldCursor := Screen.Cursor;
  +      try
  +        Screen.Cursor:= crHourGlass;
  +        fs := TFileStream.Create(FSaveDialog.FileName, fmCreate);
  +        try
  +          if aFileType = 'CSV' then
  +            Self.ExportGridAsCsv(fs)
  +          else if aFileType = 'XLS' then
  +            Self.ExportGridAsXls(fs);
  +        finally
  +          fs.Free;
  +        end;
  +      finally
  +        Screen.Cursor:= oldCursor;
  +      end;
  +    except
  +      on E:Exception do
  +      begin
  +        MessageDlg('Unable to write file. Check if the file is open by another application. If so, close it and run this command again.', mtInformation, [mbOk], 0);
  +      end;
  +    end;
  +  end;
  +
  +end;
  +
   constructor TmDBGridHelper.Create(aGrid : TDBGrid);
   begin
     FSettings := TmGridColumnsSettings.Create;
     FSortManager := nil;
     FDBGrid := aGrid;
  +  FSaveDialog := TSaveDialog.Create(nil);
   end;
   
   destructor TmDBGridHelper.Destroy;
  
positive.code:     procedure SaveSettings (aStream : TStream);
      procedure EnableSort (aSortManager : ISortableDatasetManager);
      procedure EnableHeaderPopupMenu (aOriginalGridPopupMenu : TPopupMenu);
      procedure ExportGridAsCsv (aStream : TStream);
  
      property Grid : TDBGrid read FDBGrid;
    end;
  
negative.code:     end;
      FDBGrid.DataSource.DataSet.RecNo:= rn;
  
  
    finally
      FDBGrid.DataSource.DataSet.EnableControls;
    end;
  end;
  
  procedure TmDBGridHelper.ExportGridAsCsv(Sender : TObject);
  begin
    ExportGridToFile('CSV');
  end;
  
  {$IFDEF FPC}
  
  procedure TmDBGridHelper.ExportGridAsXls(aStream: TStream);
  var
    MyWorkbook: TsWorkbook;
    MyWorksheet : TsWorksheet;
    tmpFields : TFields;
    i, rn, row : integer;
  begin
    MyWorkbook := TsWorkbook.Create;
    try
      MyWorksheet := MyWorkbook.AddWorksheet('Sheet1');
  
      FDBGrid.DataSource.DataSet.DisableControls;
      try
        tmpFields := FDBGrid.DataSource.DataSet.Fields;
        for i := 0 to tmpFields.Count - 1 do
        begin
          MyWorksheet.WriteText(0, i, tmpFields[i].DisplayLabel);
          MyWorksheet.WriteBackgroundColor(0, i, $00D0D0D0);
        end;
  
        rn := FDBGrid.DataSource.DataSet.RecNo;
        row := 1;
  
        FDBGrid.DataSource.DataSet.First;
        while not FDBGrid.DataSource.DataSet.EOF do
        begin
          for i := 0 to tmpFields.Count - 1 do
          begin
            if not tmpFields[i].IsNull then
            begin
              if (tmpFields[i].DataType = ftSmallint) or (tmpFields[i].DataType = ftInteger) or (tmpFields[i].DataType = ftLargeint) then
                MyWorksheet.WriteNumber(row, i, tmpFields[i].AsFloat, nfGeneral, 0)
              else
              if (tmpFields[i].DataType = ftFloat) then
                MyWorksheet.WriteNumber(row, i, tmpFields[i].AsFloat)
              else
              if (tmpFields[i].DataType = ftDate) or (tmpFields[i].DataType = ftDateTime) or (tmpFields[i].DataType = ftTime) or (tmpFields[i].DataType = ftTimeStamp) then
                MyWorksheet.WriteDateTime(row, i, tmpFields[i].AsDateTime)
              else
              if (tmpFields[i].DataType = ftBoolean) then
                MyWorksheet.WriteBoolValue(row, i, tmpFields[i].AsBoolean)
              else
                MyWorksheet.WriteText(row, i, tmpFields[i].AsString);
            end;
          end;
  
          inc(row);
          FDBGrid.DataSource.DataSet.Next;
        end;
        FDBGrid.DataSource.DataSet.RecNo:= rn;
  
      finally
        FDBGrid.DataSource.DataSet.EnableControls;
      end;
      MyWorkbook.WriteToStream(aStream, sfExcel8);
    finally
      MyWorkbook.Free;
    end;
  end;
  
  procedure TmDBGridHelper.ExportGridAsXls(Sender : TObject);
  begin
    ExportGridToFile('XLS');
  end;
  
  {$ELSE}
  
  procedure TmDBGridHelper.ExportGridAsXls(aStream: TStream);
  begin
    // TODO
  end;
  {$ENDIF}
  
  end.
  