Código
{OBS: É necessário que na USES esteja a: CommonTools7}
implementation
{$R *.dfm}
Procedure Exportar_para_CSV;
Var
i : Integer;
F : TextFile;
Arquivo : string;
Begin
Arquivo := ChangeFileExt( Form1.Svd1.FileName, '.CSV' );
AssignFile( F, Arquivo );
ReWrite( F );
// Cabeçalho
For i := 0 To Pred( Form1.Query1.FieldCount ) Do Begin
Write( F, Form1.Query1.Fields[ i ].FieldName ) ;
If ( i < Pred( Form1.Query1.FieldCount ) ) Then
Write( F, ';' );
End;
WriteLn( F );
While ( Not Form1.Query1.Eof ) Do Begin
For i := 0 To Pred( Form1.Query1.FieldCount ) Do Begin
Write( F, Form1.Query1.Fields[ i ].AsString ) ;
If ( i < Pred( Form1.Query1.FieldCount ) ) Then
Write( F, ';' );
End;
WriteLn( F );
Form1.Query1.Next;
End;
Flush( F );
Close( F );
End;
{Depois basta chamar a Procedure no ponto em que você quiser. No meu caso em um button, testava se um checkbox (chkExportar) estava checado.}
if chkexportar.Checked then
begin
Form1.Svd1.Execute;//Para abrir o SaveDialog
Exportar_para_CSV;
end;
Planeta Delphi - www.planetadelphi.com.br - Todos os direitos reservados | Copyright 2001-2009