Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Export to CSV

Posted on 2000-02-24
5
Medium Priority
?
2,491 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 200 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

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…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

722 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