Přidat otázku mezi oblíbenéZasílat nové odpovědi e-mailem Uložení sloupcu v delphi

Dobry den.
Mam ve gridu 2 sloupce s daty.
Chci ten obsah uložit do nějakého souboru (třeba csv, ktery umí tabulator).
Chtěl bych,aby se zachovali sloupce.
Strkat mezí ně tabulator nepomáhá :-\
Poradíte?

A := 'A1' + Chr(9) + 'A2'

Předmět Autor Datum
CSV = Comma-separated values, hodnoty oddělené čárkami Proč tam tedy cpeš tabulátor? ::) Případně m…
host 23.02.2014 18:09
host
A musí tam byť silou mocou tabulátor? Nestačia stĺpce oddelené čiarkami? procedure SaveGrid(const F… poslední
pme 23.02.2014 18:31
pme

A musí tam byť silou mocou tabulátor? Nestačia stĺpce oddelené čiarkami?

procedure SaveGrid(const Filename: string; Grid: TStringGrid);
var
  Strs : TStringList;
  i : integer;
begin
  Strs := TStringList.Create;
  try
    if Grid.RowCount > 0 then
    begin
      for i := 0 to Grid.RowCount -1 do Strs.Add( Grid.Rows[i].CommaText );
    end;
    Strs.SaveToFile(Filename);
  finally
    Strs.Free;
  end;
end;

procedure LoadGrid(const Filename: string; Grid: TStringGrid);
var
  Strs : TStringList;
  i : integer;
begin
  Strs := TStringList.Create;
  try
    Strs.LoadFromFile(Filename);
    if Strs.Count > 0 then
    begin
      for i := 0 to Strs.Count -1 do Grid.Rows[i].CommaText := Strs[i];
    end;
  finally
    Strs.Free;
  end;
end;

Ak by si trval na tabulátoroch, potom mierne upravená verzia:

procedure SaveGrid(const Filename: string; Grid: TStringGrid);
var
  Strs : TStringList;
  i : integer;
begin
  Strs := TStringList.Create;
  try
    if Grid.RowCount > 0 then
    begin
      for i := 0 to Grid.RowCount -1 do
      begin
        Grid.Rows[i].StrictDelimiter := true;
        Grid.Rows[i].Delimiter := #9; //TAB

        Strs.Add( Grid.Rows[i].DelimitedText );
      end;
    end;
    Strs.SaveToFile(Filename);
  finally
    Strs.Free;
  end;
end;

procedure LoadGrid(const Filename: string; Grid: TStringGrid);
var
  Strs : TStringList;
  i : integer;
begin
  Strs := TStringList.Create;
  try
    Strs.LoadFromFile(Filename);
    if Strs.Count > 0 then
    begin
      for i := 0 to Strs.Count -1 do
      begin
        Grid.Rows[i].DelimitedText := Strs[i];
      end;
    end;
  finally
    Strs.Free;
  end;
end;

Zpět do poradny Odpovědět na původní otázku Nahoru