Save a StringGrid as a CSV file in delphi

// Save a StringGrid as a CSV file

function TForm1.SaveToCSV:Boolean;
var
SD : TSaveDialog;
I : Integer;
CSV : TStrings;
FileName : String;
begin
Try
// Filedialog erzeugen
SD := TSaveDialog.Create(Self);
SD.Filter := ‘CSV-Trennzeichen getrennt (*.csv)|*.CSV’;
//Filedialog ausführen
If SD.Execute = True Then
Begin
//Filename zuweisen
FileName := SD.FileName;
If Copy(FileName,Pos(‘.’,FileName),Length(FileName)-Pos(‘.’,FileName)+1) <> ‘.csv’ Then FileName := FileName + ‘.csv’;
Screen.Cursor := crHourGlass;
//Stringliste erzeugen
CSV := TStringList.Create;
Try
//Stringliste füllen
For I := 0 To Grid.RowCount – 1 Do CSV.Add(Grid.Rows[I].CommaText);
//CSV speichern
CSV.SaveToFile(FileName);
Result := True;
Finally
CSV.Free;
End;
End;

Finally
SD.Free;
Screen.Cursor := crDefault;
End;
end;

//SaveToCSV ausführen (sample call)
procedure TForm1.BtnSaveClick(Sender: TObject);
begin
SaveToCSV;
end;