Jump to content

export to XLS or CSV


JRIchmann

Recommended Posts

Hi...

 

I have a Query.... Query is shown in Grid...

 

Now I want export this Grid to local machine (as CSV oder as XLS)

 

have  you a Tip for me Or a Demo ?

 

I use: Delphi XE7 and latest Version of UniGui with UniDac from DevArt.

 

bye and thanks

 

www.JRichmann.de

Link to comment
Share on other sites

  • 3 weeks later...
hello try this code...

 



procedure ExportarCSV(Dataset: TDataset; Archivo: String);
var iArchivo: TStringlist;
Linea: String;
i: Integer;
begin
iArchivo := TStringlist.Create;
Linea := '';
for i := 0 to Dataset.FieldCount - 1 do begin
if Dataset.Fields[i].Visible then begin
if Linea <> '' then Linea := Linea + ';';
Linea := Linea + AnsiQuotedStr(Dataset.Fields[i].DisplayLabel, '"');
end;
end;
iArchivo.Add(Linea);
Dataset.DisableControls;
Dataset.First;
while not Dataset.Eof do begin
Linea := '';
for i := 0 to Dataset.FieldCount - 1 do begin
if Dataset.Fields[i].Visible then begin
if Linea <> '' then Linea := Linea + ';';
Linea := Linea + AnsiQuotedStr(Dataset.Fields[i].AsString, '"');
end;
end;
iArchivo.Add(Linea);
Dataset.Next;
end;
Dataset.First;
Dataset.EnableControls;
iArchivo.SaveToFile(Archivo);
end;


Call

fname := 'F' + FormatDateTime('hhmmss', Now()) + '.csv'; // Create a unique name for report.
ExportarCSV(Dm.myQuery,UniServerModule.LocalCachePath + fname);
unisession.SendFile(UniServerModule.LocalCachePath + fname);


Link to comment
Share on other sites

  • 2 years later...

Hi.

 

I'm testing this solution and have a problem.

 

When unisession.sendfile raises, it ask for save file or open with ...

 

If select save, and I open it later, it works.

 

But if I select Open with excel, it opens but unformatted.

 

Any solution for this?

 

Thanks.

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...