Jump to content

export to XLS or CSV


JRIchmann
 Share

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...