this slowpoke moves

Export StringGrid to CSV

uses ExtCtrls, Grids

private
    { Private declarations }
    function SaveToCSV:Boolean;
    
//

function TForm1.SaveToCSV:Boolean;
var
  SD : TSaveDialog;
  I : Integer;
  CSV : TStrings;
  FileName : String;
begin
  Try
  SD := TSaveDialog.Create(Self);
  SD.Filter := 'CSV (*.csv)|*.CSV';
  If SD.Execute = True Then
  Begin
    FileName := SD.FileName;
    If Copy(FileName,Pos('.',FileName),Length(FileName)-Pos('.',FileName)+1) <> '.csv' Then FileName := FileName + '.csv';
    Screen.Cursor := crHourGlass;
    CSV := TStringList.Create;
    Try
      For I := 0 To StringGrid1.RowCount - 1 Do CSV.Add(StringGrid1.Rows[I].CommaText);
      CSV.SaveToFile(FileName);
      Result := True;
    Finally
      CSV.Free;
    End;
  End;

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


procedure TForm1.Button1Click(Sender: TObject);
begin
  SaveToCSV;
end;

Keine Kommentare:

Kommentar veröffentlichen

Beliebte Posts

Translate