Board index » delphi » Saving the contents of a DbGrid to a tab delimited text file

Saving the contents of a DbGrid to a tab delimited text file

Hi
Now can I save the contents of a DbGrid to a tab delimited text file?
There does not seem to be a function like SaveToFile for the TDBGrid or
tTable component.
Thanks
Tim
 

Re:Saving the contents of a DbGrid to a tab delimited text file


You can use the BDE ASCII driver to get a comma delimited text file but not
a tab delimited file. You will have to write the records yourself using the
Pascal text file I/O routines AssignFile, Reset, Rewrite, Readln and
Writeln.

--
Bill Todd (TeamB)
(Questions received via email cannot be answered.)

Re:Saving the contents of a DbGrid to a tab delimited text file


The following code may help you to save database tables as a text file

function TSmartTable.ConvertCSV2TXT(FileName, SChar : String) : String;
var CSV : TStrings; S, EmptyStr : String; I, J, K, LK , MaxF : Integer;
FldSizes : array [0..100] of Integer;
begin
  Result := FileName;
  CSV    := TStringList.Create;
  try
    CSV.LoadFromFile(FileName);
    // Get Max Sizes
    FillChar(FldSizes, SizeOf(FldSizes),   0);
    MaxF := 1;
    for I := 0 to CSV.Count -1 do begin
      S := CSV[I];
      K := Pos(SChar, S);
      J := 0;
      while K > 0 do begin
        if FldSizes[J] < K then FldSizes[J] := K;
        System.Delete(S, 1, K);
        K := Pos(SChar, S);
        Inc(J);
      end;
      if MaxF < J then MaxF := J;
      FldSizes[J] := Length(S);
    end;
    for I := 0 to CSV.Count -1 do begin
      S := CSV[I];
      J := 0;
      K := Pos(SChar, S);
      LK := 0;
      while K > 0 do begin
        System.Delete(S, K, 1);
        EmptyStr := StrOfChars(' ', FldSizes[J]-K+LK);
        System.Insert(EmptyStr, S, K);
        LK := K + Length(EmptyStr)-1;
        K := Pos(SChar, S);
        Inc(J);
      end;
      CSV[I] := StrToAnsi(S);
    end;
    CSV.SaveToFile(ChangeFileExt(FileName, '.TXT'));
    // Create Header
    CSV.Clear;
    CSV.Add('[' + ExtractFileName(ChangeFileExt(FileName, '')) + ']');
    CSV.Add('Filetype=Fixed');
    J := 0;
    for I := 0 to MaxF -1 do begin
      S := Format('Field_%d=Field_%d,Char,%.2d,00,%.2d', [I+1, I+1,
FldSizes[I], J]);
      CSV.Add(S);
      Inc(J, FldSizes[I]);
    end;
    CSV.Add('CharSet=ascii');
    CSV.SaveToFile(ChangeFileExt(FileName, '.SCH'));
    Result := ChangeFileExt(FileName, '.TXT');
  finally
    CSV.Free;
  end;
end;

* Sent from RemarQ http://www.remarq.com The Internet's Discussion Network *
The fastest and easiest way to search and participate in Usenet - Free!

Other Threads