Solved

Export to CSV

Posted on 2000-02-24
5
2,278 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
tidtcpserver connection lost handle 2 103
Delphi Yen format 3 45
firemonkey keyboard covers the controls 1 37
DBCtrlGrid, Delphi, Scroll 8 38
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

828 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