Source Code



Save a StringGrid as a CSV file

  • anonymous
  • Dec 19' 13
  • Delphi
  • 433
  • 42
  • United States

ds

 

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;
  • Tags : csv file, data, grid, delphi
comments powered by Disqus

Related Code

Empty Recylce Bin

Get MAC Address of Network Card

Kill Active Program

Resizable forms and size grip

Simple Like Function

Suspend the PC

Get current url

Using WinZip as a backup tool

Upper the first letter of each word

Rotate Font



Category