• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3896
  • Last Modified:

How to connect a csv file to Delphi

Can a comma delimited file, (*.csv) be viewed through delphi?  If so, what components, settings, and code are used?
0
dpnd009
Asked:
dpnd009
  • 4
1 Solution
 
mikelittlewoodCommented:
How do you want to view it? Just as plain text? Formatted like a spreadsheet?
0
 
mikelittlewoodCommented:
If you just want to view the text, drop a memo onto a form and call

memo1.Lines.LoadFromFile( 'c:\mycsv.csv');
0
 
mikelittlewoodCommented:
Another option maybe you want to load it into a stringgrid

var
   i, j: integer;
   slFile, sl: TStringList;
begin
   try
      slFile := TStringList.Create;
      slFile.LoadFromFile( 'c:\mycsv.csv');

      with stringgrid1 do
      try
         rowcount := slFile.Count;
         FixedCols := 1;
         FixedCols := 0;

         for i := 0 to slFile.count - 1 do
         begin
            sl := TStringList.Create;
            sl.Delimiter := ',';
            sl.DelimitedText := slFile.strings[ i];

            for j := 0 to sl.count - 1 do
               Cells[ j, i+1] := sl.Strings[ j];
         end;
      finally
         freeAndNil( sl)
      end;
   finally
      freeAndNil( slFile)
   end;
end;
0
 
mikelittlewoodCommented:
rowcount := slFile.Count;
should probably be rowcount := slFile.Count+1 to allow for header row
0
 
dpnd009Author Commented:
To use as a spreadsheet, or say to use TDbGrid to view the file.  Using the code you have and say having a list box to select a file.  Or what about saving a csv file to access?
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now