![]() |
paradox-daten als csv speichern
hallo,
ich möchte gerne meine daten aus der paradox-db als eine csv-datei abspeichern, um sie in excel wieder zu verwenden. ich habe gelesen, dass man die daten in eine TStringList mit ; getrennt schreiben soll und dann als .csv speichern soll, aber wie macht man das konkret? mfg bastos |
Geh in zwei verschachtelten Schleifen die die felder und die Datensaetze deiner Anwendung durch.
|
hi,
habe das jetzt so:
Delphi-Quellcode:
leider hat er nur einen datensatz gespeichert und danach eine fehlermeldung ausgegeben."access violation at adress... in module..."
procedure TFormMain.Button2Click(Sender: TObject);
var sl: TStringList; begin Query3.close; Query3.SQL.text :='select * from member order by Nr'; Query3.Open; sl:=TStringList.Create; while not Query3.Eof do begin try sl.Add(Query3.fieldByName('Name').AsString); sl.SaveToFile('c:\test.txt'); finally end; sl.free; end; end; was mache ich noch falsch? mfg bastos |
Zunächst fehlt:
Delphi-Quellcode:
Wichtiger ist jedoch, dass das SaveToFile und Free der Stringlist außerhalb der While-Schleife erfolgen sollten! Und für eine "echte" csv-Datei fehlt noch das trennzeichen (Komma oder Semikolon).
Query3.next
|
Hallo bastos,
getestet und funktionsfähig:
Code:
Hab' ich schon vor einiger Zeit geschrieben, steht vielleicht ein bischen mehr drin als nötig, aber als Beispiel sollte es funktionieren.
procedure TFBrowser.ANSII1Click(Sender: TObject);
var abbr : boolean; headtxt, tx, s : string; i, j : integer; v : variant; ftyp : TFieldType; begin if not Tab1.Active then exit; headtxt := FBrowser.Caption; s := ExtractFileName(Tab1.TableName); s := Copy(s,1,Pos('.',s)) + 'TXT'; AssignFile(f, BrowDataPath + s); FBrowser.Caption := FBrowser.Caption + ' ' + Upper(BrowDataPath) + Lower(s); {$I-} Rewrite(f); if (IOResult) = 0 then begin InitGau(BotGau, Tab1); Tab1.DisableControls; tx := ''; for i:= 0 to Tab1.FieldCount - 1 do begin if i > 0 then s := Tab1.FieldDefs.Items[i].Name; tx := tx + Upper(s); if i < Tab1.FieldCount - 1 then tx := tx + ';'; end; WriteLn(f,tx); abbr := IOResult <> 0; Tab1.First; while (not abbr) and (not Tab1.Eof) do begin tx := ''; for i:= 0 to Tab1.FieldCount - 1 do begin s := ''; v := Tab1.Fields[i].AsVariant; // if v = Null then begin // ftyp := Tab1.FieldDefs.Items[i].DataType; // end; ftyp := Tab1.FieldDefs.Items[i].DataType; try case ftyp of ftString : if v = Null then s := '' else s:=Tab1.Fields[i].AsString; { Zeichen- oder Stringfeld';} ftAutoInc : if v = Null then s := '' else str(v:20:0,s); { Autoinkrement-32-Bit -Integer-Feld als Zählerfeld} ftBytes : if v = Null then s := '' else str(v:5:0,s); { Feste Anzahl von Bytes (binäre Speicherung)} ftSmallint, { 16-Bit-Integer-Feld} ftInteger, { 32-Bit-Integer-Feld} ftWord : if v = Null then s := '' else str(v:20:0,s); { Vorzeichenloses 16-Bit-Integer-Feld} ftBoolean : if v = Null then s := 'Falsch' { Logisches Feld} else if v = true then s := 'Wahr' else s := 'Falsch'; ftFloat, { Numerisches Gleitkommafeld} ftCurrency : { Währungsfeld} begin if v = Null then v := 0; str(v:20:4,s); if Pos('.',s) > 0 then s := Copy(s,1,Pos('.',s)-1) + ',' + Copy(s,Pos('.',s)+1,length(s)); end; ftDate : if v = Null then s := '' else s := DateToStr(v); { Datumsfeld} ftTime : if v = Null then s := '' else s := TimeToStr(v); { Zeitfeld} ftDateTime : if v = Null then s := '' else s := DateTimeToStr(v); { Feld für Datum und Zeit} ftBCD : s:='#BCD#'; { Binärcodiertes Dezimalfeld} ftVarBytes : s:='#VarBytes#'; { Variable Anzahl von Bytes (binäre Speicherung)} ftBlob : s:='#Blob#'; { BLOB-Field} ftMemo : if v = Null then s:='#Memo#' else s := v; { Memofeld für Text} ftGraphic : s:='#Graphic#'; { Bitmap-Feld} ftFmtMemo : s:='#FMemo#'; { Formatiertes Memofeld für Text} ftParadoxOle: s:='#PdoxOle#'; { Paradox-OLE-Feld} ftDBaseOle: s:='#dBaseOle#'; { dBASE-OLE-Feld} ftTypedBinary:s:='#Binary#'; { Typisiertes Binärfeld} else s:='#???#'; { Unbekannt oder unbestimmt} end; {end of case} except on E: Exception do s := ''; end; { if Copy(s,1,1) <> '#' then begin} s := trim(s); j := Pos(';',s); while j > 0 do begin if j < length(s) then s := copy(s,1,j-1) + ',' + copy(s,j+1,length(s)) else s := copy(s,1,j-1); j := Pos(';',s); end; tx := tx + s; if i < Tab1.FieldCount - 1 then tx := tx + ';'; { end;} end; {of for i := 0 to FieldCount - 1} WriteLn(f,tx); abbr := IOResult <> 0; RefreshGau(BotGau); {, _rc_gau, _zel_gau, _rcg_gau);} Tab1.Next; end; {end of while} CloseGau(BotGau); Tab1.First; Tab1.EnableControls; end else begin {Fehler beim Erstellen der Export-Datei} end; CloseFile(f); {$I+} FBrowser.Caption := headtxt; end; mfg eddy |
Tabellen ins CSV Format kopieren
Liste der Anhänge anzeigen (Anzahl: 1)
Hallo Bastos,
Hab Dir mal eine kleine Komponente entwickelt. Die Komponente ist abgeleitet von TQuery. Du must also die üblichen Schritte durchführen, um eine Verbindung mit einer Datenbank zu erstellen. Dann musst Du lediglich die Eigenschaften
Delphi-Quellcode:
und
ExportFile
Delphi-Quellcode:
einstellen und die Funktion
Seperator
Delphi-Quellcode:
aufrufen.
ExportAsCSV
Delphi-Quellcode:
Gruß, Tom
unit QueryExport;
interface uses Windows, Messages, SysUtils, Classes, DB, DBTables; type TQueryExport = class(TQuery) private { Private-Deklarationen } fExportFile: String; fSeperator: String; protected { Protected-Deklarationen } public { Public-Deklarationen } procedure ExportAsCsv; published { Published-Deklarationen } property ExportFile: String read fExportFile write fExportFile; property Seperator: String read fSeperator write fSeperator; end; procedure Register; implementation procedure Register; begin RegisterComponents('Specialwork Database', [TQueryExport]); end; procedure TQueryExport.ExportAsCsv; var ExportedStrings: TStringList; Y: Integer; CurrentDataset: String; begin ExportedStrings:=TStringList.Create; try try if Active then begin while not Eof do begin CurrentDataset:=''; for Y:=0 to Fields.Count-1 do begin CurrentDataset := CurrentDataset + Fields[y].AsString; CurrentDataset := CurrentDataset + Seperator; end; ExportedStrings.Add(CurrentDataset); Next; end; end; ExportedStrings.SaveToFile(ExportFile); except on E: Exception do begin OutputDebugString(PChar(E.Message)); end; end; finally ExportedStrings.Free; end; end; end. |
Alle Zeitangaben in WEZ +1. Es ist jetzt 21:56 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz