Solved

Export to CSV

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

770 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