delphi4arab منتديات دلفي للعرب
حفظ نتيجة Query في ملف - نسخة قابلة للطباعة

+- delphi4arab منتديات دلفي للعرب (https://delphi4arab.net/forum)
+-- قسم : منتديات دلفي للعرب التعليمية (https://delphi4arab.net/forum/forumdisplay.php?fid=3)
+--- قسم : سؤال و جواب حول دلفي (https://delphi4arab.net/forum/forumdisplay.php?fid=15)
+--- الموضوع : حفظ نتيجة Query في ملف (/showthread.php?tid=3990)



حفظ نتيجة Query في ملف - لؤي - 12-03-2012

السلام عليكم

كيف أحفظ نتيجة Query في ملف نصي أو ملف CSV ؟


حفظ نتيجة Query في ملف - لؤي - 12-03-2012

وجدت طريقتين :

الأولى :

PHP كود :
var   Stream: TFileStream;
 
i: Integer;
  
OutLine: string;
  
sTemp: string;
  
begin   Stream := TFileStream.Create('C:\fichierFinal1.csv', fmCreate);
 try     while 
not zQuery1.Eof do
 
begin
 
// You'll need to add your special handling here where OutLine is built
 
OutLine := '';
for 
i := 0 to zQuery1.FieldCount - 1 do
 
begin         sTemp := ZQuery1.Fields[i].AsString;
// Special handling to sTemp here
 
OutLine := OutLine + sTemp + ',';
 
end;
// Remove final unnecessary ','
SetLength(OutLine, Length(OutLine) - 1);
 
// Write line to file
 
Stream.Write(OutLine[1], Length(OutLine) * SizeOf(Char));
 
// Write line ending
 
Stream.Write(sLineBreak, Length(sLineBreak));
 
zQuery1.Next;
 
end;
 finally
 
Stream.Free;  // Saves the file
  
end;
end; 
الثانية:

PHP كود :
var
List: 
TStringList;
  
S: String;
   
I: Integer;
 
begin
List := TStringList.Create;
try     
ZQuery1.First;
 while 
not ZQuery1.Eof do
 
begin
  S 
:= '';
for 
I := 0 to ZQuery1.FieldCount - 1 do
 
begin
 
if S > '' then
  S 
:= S + ',';
  
S := S + ZQuery1.Fields[I].AsString ;
 
end;
 List.
Add(S);
ZQuery1.Next;
 
end;
 finally
 List.
SaveToFile('C:\fichierFinal.csv');
  List.
Free;
  
end;
 
end; 



حفظ نتيجة Query في ملف - kachwahed - 12-03-2012

وعليكم السلام عليكم ورحمة الله
إذا استخدمت ADO يمكنك اختيار pfADTG (صيغة خاصة) أو pfXML (صغية XML) جاهزة:
PHP كود :
procedure TCustomADODataSet.SaveToFile(const FileName: WideString;
  
Format: TPersistFormat); 
المثال السابق يدوي من هنا:
كود :
http://stackoverflow.com/questions/5680017/delphi-tquery-save-to-csv-file
الكائن TClientDataSet يحتوي على أنماط جاهزة أيضا:
كود :
TDataPacketFormat = (dfBinary, dfXML, dfXMLUTF8);
مثال آخر لـ CSV
PHP كود :
procedure btnSaveClick(Sender: TObject);
begin
  DBGrid1
.Visible := False;
  try
    if (
SaveDialog1.Execute = true) and
     (
length(SaveDialog1.FileName)  0) then
      begin
        SaveDialog1
.DefaultExt := 'CSV';
        
SaveAsCSV(SaveDialog1.FileName,ClientDataSet1);
      
end;
  finally
    
DBGrid1.Visible := True;
  
end;  
end;
*************************************************************
procedure SaveAsCSV(myFileName: string; myDataSet: TDataSet);
var
  
myTextFile: TextFile;
  
i: integer;
  
s: string;
begin
  
//create a new file
  
AssignFile(myTextFile, myFileName);
  
Rewrite(myTextFile);

  
s := ''; //initialize empty string

  
try
    
//write field names (as column headers)
    
for i := 0 to myDataSet.FieldCount - 1 do
      
begin
        s 
:= s + Format('"%s";', [myDataSet.Fields[i].FieldName]);
      
end;
    
Writeln(myTextFile, s);

    
//write field values
    
while not myDataSet.Eof do
      
begin
        s 
:= '';
        for 
i := 0 to myDataSet.FieldCount - 1 do
          
begin
            s 
:= s + Format('"%s";', [myDataSet.Fields[i].AsString]);
          
end;
        
Writeln(myTextfile, s);
        
myDataSet.Next;
      
end;

  finally
    
CloseFile(myTextFile);
  
end;
end; 
لم أجربه.المصدر:
كود :
http://delphi.xcjc.net/viewthread.php?tid=43357
بالتوفيق.