Friday, October 9, 2009

TListView and CSV

If you ever need to save/load the content of a CSV file from/to a TListView, then this post will give you a start, but first what is a CSV file anyways? A CSV file is a format in which you can save table values as text. CSV stands for Comma Separated Values, Microsoft Excel can save/load sheets as/from CSV.
More info about CSV format can be found here and here.
here are the procedures which let's you save/load the content of a TListView to/from a CSV file.
(* this procedure saves the content of a TListView *)
(*   to a CSV file *)
procedure ListViewToCSV(
          theListView: TListView;
          const FileName: String);
var item: TListItem;
    index,
    subIndex: Integer;
    theFile: TStringList;
    Line: String;
begin
     (* initialize a TStringList *)
     theFile := TStringList.Create;
     (* loop to get all items of theListView *)
     for index := 0 to theListView.Items.Count -1 do begin
         (* store the current item in a local variable *)
         item := theListView.Items[index];
         (* format the current line of the CSV *)
         Line := Format('%s,%s',
              [item.Caption, item.SubItems.CommaText]);
         (* add the line to CSV *)
         theFile.Add(Line);
     end;
     (* save the CSV file *)
     theFile.SaveToFile(FileName);
     (* free theFile variable *)
     FreeAndNil(theFile);
end;

(* this procedure loads the content of a CSV file *)
(* to a TListView *)
procedure ListViewFromCSV(
          theListView: TListView;
          const FileName: String);
var item: TListItem;
    index,
    comPos,
    subIndex: Integer;
    theFile: TStringList;
    Line: String;
begin
     (* initialize a TStringList *)
     theFile := TStringList.Create;
     (* then load it from FileName *)
     theFile.LoadFromFile(FileName);
     (* loop to get all lines *)
     for index := 0 to theFile.Count -1 do begin
         (* store each line in a string variable *)
         Line := theFile[index];
         (* for every line add a item to the list *)
         item := theListView.Items.Add;
         (* get the first comma from the line *)
         comPos := Pos(',', Line);
         (* copy the value from start of string to *)
         (* the position of comma -1 *)
         item.Caption := Copy(Line, 1, comPos -1);
         (* delete from Line starting from 1 to *)
         (* position of comma *)
         Delete(Line, 1, comPos);
         (* get the next comma *)
         comPos := Pos(',', Line);
         (* loop to get all values from the string *)
         (* 1 comma means 1 column *)
         while comPos > 0 do begin
               item.SubItems.Add(Copy(Line, 1, comPos -1));
               Delete(Line, 1, comPos);
               comPos := Pos(',', Line);
         end;
         (* add last value as SubItem *)
         item.SubItems.Add(Line);
     end;
     (* free the TStringList *)
     FreeAndNil(theFile);
end;
Screen shot of the demo application

Download source code or binary form of the demo application.

No comments:

Post a Comment

Blogroll(General programming and Delphi feeds)