Web Design that taps into the haromny and vision of your dreams.

Load CSV Data into a StringGrid in Dephi

By on in Coding

125 words, estimated reading time 1 minutes.

This procedure will load CSV data stored in a file into a StringGrid control for display or processing.

procedure LoadCSV(Filename: string; sg: TStringGrid);
var
   i, j, Position, count, edt1: integer;
   temp, tempField : string;
   FieldDel: char;
   Data: TStringList;
begin
  Data := TStringList.Create;
  FieldDel := ',';
  Data.LoadFromFile(Filename);
  temp :=  Data[1];
  count := 0;
  for i:= 1 to length(temp) do
    if copy(temp,i,1) =  FieldDel then
      inc(count);
  edt1 := count+1;
  sg.ColCount := 30;
  sg.RowCount := Data.Count +1;
  sg.FixedCols := 0;
  for i := 0 to Data.Count - 1 do
    begin;
      temp :=  Data[i];
      if copy(temp,length(temp),1) <> FieldDel then
        temp := temp + FieldDel;
      while Pos('"', temp) > 0 do
        begin
          Delete(temp,Pos('"', temp),1);
        end;
      for j := 1 to edt1 do
      begin
        Position := Pos(FieldDel,temp);
        tempField := copy(temp,0,Position-1);
        sg.Cells[j-1,i+1] := tempField;
        Delete(temp,1,length(tempField)+1);
      end;
    end;
    Data.Free;
end;

Last updated on: Saturday 24th June 2017

 

Comments

Have a question or suggestion? Please leave a comment to start the discussion.

 

Leave a Reply

Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

Your email address will not be published.