如何加载和保存StringGrid内容?

时间:2011-07-29 01:37:03

标签: delphi delphi-7 delphi-2010 delphi-xe

代码的第一部分工作正常,而第二部分(注释)则没有。 它会覆盖我的A1文件,尽管它应该写入A2。

procedure TForm1.AdvGlowButton12Click(Sender: TObject);
var
  i,j: Integer;
  Seznam: TStrings;
  ApplicationPath: string;
begin
  if (cxRadiogroup3.ItemIndex and cxRadiogroup2.ItemIndex) = 0 then begin
    ApplicationPath:= ExtractFileDir(Application.ExeName);
    Seznam:= TStringList.Create;
    try
      for i:=0 to advStringGrid2.ColCount-1 do
        Seznam.AddStrings(advStringGrid2.Cols [i]);
      for i:=0 to advStringGrid2.rowCount-1 do
        Seznam.AddStrings(advStringGrid2.rows [j]);
      Seznam.SaveToFile(ApplicationPath+'\A1.txt');
    finally
      seznam.free;
    end;
  end ;
  //if cxRadiogroup3.ItemIndex = 1 and cxRadiogroup2.ItemIndex = 0 then begin
  //  ApplicationPath:= ExtractFileDir(Application.ExeName);
  //  Seznam:= TStringList.Create;
  //  try
  //    for i:=0 to advStringGrid2.ColCount-1 do
  //      Seznam.AddStrings(advStringGrid2.Cols [i]);
  //    for i:=0 to advStringGrid2.rowCount-1 do
  //      Seznam.AddStrings(advStringGrid2.rows [j]);
  //    Seznam.SaveToFile(ApplicationPath+'\A2.txt');
  //  finally
  //    seznam.free;
  //  end ;
  //end
end;

我做错了什么? 当我尝试从空文本文件加载内容时,为什么stringgrid给出listindex超出界限?如果我将空的stringgrid保存到该文件,稍后,虽然文件中没有任何内容,但它不会抱怨?奇怪...

这就是我如何将A1和A2加载到stringgrid中。

procedure TForm1.cxRadioGroup2Click(Sender: TObject);
Var
  I,j,k: Integer;
  Seznam: TStrings;
  ApplicationPath: string;
begin
  case cxradioGroup2.ItemIndex of
    0: begin
         if cxradioGroup3.ItemIndex = 0 then begin
           Seznam:= TStringList.Create;
           AdvStringgrid2.ClearAll;
           try
             Seznam.LoadFromFile('A1.txt');
             k:= 0;
             for i:=0 to advStringGrid2.ColCount-1 do
               for j:=0 to advStringGrid2.RowCount-1 do begin
                 advstringGrid2.Cells [i,j]:= Seznam.Strings [k];
                 Inc(k);
               end;
           finally
             seznam.free;
           end;
         end;
         if cxradioGroup3.ItemIndex = 1 then begin
           Seznam:= TStringList.Create;
           AdvStringgrid2.ClearAll;
           try
             Seznam.LoadFromFile('A2.txt');
             k:=0;
             for i:=0 to advStringGrid2.ColCount-1 do
               for j:=0 to advStringGrid2.RowCount-1 do begin
                 advstringGrid2.Cells [i,j]:= Seznam.Strings [k];
                 Inc(k);
               end;
           finally
             seznam.free;
           end;
         end;
       end;
  end;
end;

3 个答案:

答案 0 :(得分:0)

你怎么知道它覆盖了A1.txt?在这两种情况下,您都保存完全相同的内容。

答案 1 :(得分:0)

这是来自SwissDelphiCenter的旧tipp,可以帮助你

// Save StringGrid1 to 'c:\temp.txt':
procedure TForm1.Button1Click(Sender: TObject);
begin
  SaveStringGrid(StringGrid1, 'c:\temp.txt');
end;

// Load StringGrid1 from 'c:\temp.txt':
procedure TForm1.Button2Click(Sender: TObject);
begin
  LoadStringGrid(StringGrid1, 'c:\temp.txt');
end;

// Save a TStringGrid to a file

procedure SaveStringGrid(StringGrid: TStringGrid; const FileName: TFileName);
var
  f:    TextFile;
  i, k: Integer;
begin
  AssignFile(f, FileName);
  Rewrite(f);
  with StringGrid do
  begin
    // Write number of Columns/Rows
    Writeln(f, ColCount);
    Writeln(f, RowCount);
    // loop through cells
    for i := 0 to ColCount - 1 do
      for k := 0 to RowCount - 1 do
        Writeln(F, Cells[i, k]);
  end;
  CloseFile(F);
end;

// Load a TStringGrid from a file

procedure LoadStringGrid(StringGrid: TStringGrid; const FileName: TFileName);
var
  f:          TextFile;
  iTmp, i, k: Integer;
  strTemp:    String;
begin
  AssignFile(f, FileName);
  Reset(f);
  with StringGrid do
  begin
    // Get number of columns
    Readln(f, iTmp);
    ColCount := iTmp;
    // Get number of rows
    Readln(f, iTmp);
    RowCount := iTmp;
    // loop through cells & fill in values
    for i := 0 to ColCount - 1 do
      for k := 0 to RowCount - 1 do
      begin
        Readln(f, strTemp);
        Cells[i, k] := strTemp;
      end;
  end;
  CloseFile(f);
end;

我正在尝试理解你的代码并尽可能好地重写他。 (它没有经过测试)

procedure TForm1.AdvGlowButton12Click(Sender: TObject);
var
  i, j: Integer;
  Seznam: TStrings;
  ApplicationPath: string;
  fileName: string;
  line: string;
begin
  if (cxRadiogroup2.ItemIndex = 0) then begin
    if (cxRadiogroup3.ItemIndex = 0) then
      fileName:= 'A1.txt'
    else
      fileName:= 'A2.txt'

    ApplicationPath:= ExtractFileDir(Application.ExeName);
    Seznam:= TStringList.Create;
    try
      for k:=0 to advStringGrid2.RowCount-1 do begin
        line:= '';
        for i:=0 to advStringGrid2.ColCount-1 do
          line = line + '|' + advStringGrid2.Cells[i, k];
        Seznam.AddStrings(line);
      end;
      Seznam.SaveToFile(ApplicationPath + '\' + fileName);
    finally
      seznam.Free;
    end;
  end;
end;

procedure TForm1.cxRadioGroup2Click(Sender: TObject);
var
  splitList: TStringList;
  i, j: Integer;
  Seznam: TStrings;
  ApplicationPath: string;
  fileName: string;
  line: string;
  sepIndex: integer;
begin
  if (cxRadiogroup2.ItemIndex = 0) then begin
    if (cxRadiogroup3.ItemIndex = 0) then
      fileName:= 'A1.txt'
    else
      fileName:= 'A2.txt'

    AdvStringgrid2.ClearAll; // don't know what this does

    ApplicationPath:= ExtractFileDir(Application.ExeName);
    Seznam:= TStringList.Create;
    try
      Seznam.LoadFromFile(fileName);
      advstringGrid2.RowCount:= Seznam.Count;
      splitList:= TStringList.Create;
      for i:=0 to Seznam.Count-1 do begin
        line:= Seznam.Strings [i];
        Split('|', line, splitList);
        advStringGrid2.ColCount:= Max(advStringGrid2.ColCount, splitList.Count);
        for k:=0 to splitList.Count-1 do
          advStringGrid2.Cells[i, k]:= splitList[k];
      end; 
    finally
      splitList.Free;
      seznam.Free;
    end;
  end;
end;

procedure Split (const Delimiter: Char; Input: string; const Strings: TStrings);
begin
   Assert(Assigned(Strings));
   Strings.Clear;
   Strings.Delimiter:= Delimiter;
   Strings.DelimitedText:= Input;
end;

希望有所帮助

答案 2 :(得分:0)

创立并适应了我的需求。然后共享:-)

procedure LoadStringGrid(const AFileName: TFileName; AGrid: TStringGrid);
var
    slRows: TStringList;
    i: integer;
begin
    slRows:= TStringList.Create;
  try
    slRows.LoadFromFile(AFileName);
    for i:= 0 to slRows.Count -1 do
      AGrid.Rows[i +1].CommaText:= slRows[i];
    finally
        slRows.Free;
    end;
end;//  LoadStringGrid
procedure SaveStringGrid(const AFileName: TFileName; AGrid: TStringGrid);
var
    slRows: TStringList;
    i: integer;
begin
    slRows:= TStringList.Create;
  try
    for i:= 1 to AGrid.RowCount -1 do
        slRows.Add(AGrid.Rows[i].CommaText);
    slRows.SaveToFile(AFileName);
    finally
        slRows.Free;
    end;
end;//  SaveStringGrid