whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1541)

Database (90)
Files (137)
Forms (107)
Graphic (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Math (76)
Misc (126)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (266)
VCL (242)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

66 Visitors Online


 
...save a StringGrid as a CSV file?
Autor: Schatzl Reinhard
[ Print tip ]  

Tip Rating (69):  
     


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;



 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners