Solved

Export to CSV

Posted on 2000-02-24
5
2,176 Views
Last Modified: 2012-05-04
Hi
Does anybody have some code for how to export a stringgrid to CSV to be read in Excel
0
Comment
Question by:koger
  • 4
5 Comments
 
LVL 8

Expert Comment

by:ZifNab
ID: 2555289
Hi Koger,

Have a look a the free StringGrid :

http://www.tmssoftware.com/advgrid.htm

Regards, Zif.
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 2555310
you can of course also use OLE to transfer your data
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 2555563
this is also a possible way, if you don't want to use a component or OLE :

procedure TForm1.BitBtn1Click(Sender: TObject);
var F:TextFile;
    i,j : integer;
begin
  AssignFile(F, 'c:\griddata.csv');
  Rewrite(F);
  for i := 0 to stringgrid1.RowCount-1 do
  begin
    for j := 0 to stringgrid1.ColCount-1 do
    begin
      if j = stringgrid1.ColCount-1 then
       Writeln(F,stringgrid1.cells[i,j])
      else
       Write(F,stringgrid1.cells[i,j],',');
    end;
  end;
  CloseFile(F);
end;

Zif.
0
 

Author Comment

by:koger
ID: 2557181
Thanks Zifnab, but I would have some source code not a compiled dcu and I can't use OLE.
I don't know the CSV text (i don't have Excel), but what if a cell contains a comma, I guess you should then put a couple of "" around the text, but I don't know.
0
 
LVL 8

Accepted Solution

by:
ZifNab earned 50 total points
ID: 2557237
Hi Koger,

Then it's even simpler :

procedure TForm1.Button1Click(Sender: TObject);
var F:TextFile;
    i : integer;
begin
 AssignFile(F, 'c:\griddata.csv');
 Rewrite(F);
 for i := 0 to stringgrid1.RowCount-1 do
 begin
  writeln(F,stringgrid1.Rows[i].CommaText);
 end;
 CloseFile(F);
end;

CommaText does it for you :-) (if a cell contains a comma, the value will be written as...","5,4","...

I tried it, and it imports into Excell without a problem.

Regards, Zif.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now