0

I am developing a program and I have a StringGrid on it; when a particular button is pressed, my program saves the stringgtid into c:\myfolder\tab9.txt. I would like to put a progress bar that indicate how many time remains at the end of the saving process because sometime the grid has a lot of rows and it could take some time. I am using this code:

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

I call the procedure in this way: SaveSG(StringGrid1,'c:\myfolder\myfile.txt');. My problem is that I don't understand how to do a progress bar that indicate the progress of the saving. At the moment I've only declared ProgressBar1.Position:=0 and ProgressBar1.Max:=FileSize. Do you have any suggestions?

4

1 回答 1

3

我们在谈论多少个细胞?您的主要瓶颈是您正在为每个单元格写入文件,而不是进行缓冲写入。

我建议你用来自 TStringGrid 的数据填充 TStringList,并使用 TStringList.SaveToFile() 方法。

我已经在具有 10,000,000 个单元格(10,000 行 x 1,000 列)的 StringGrid 上测试了以下过程,它可以在不到一秒的时间内将数据保存到磁盘:

procedure SaveStringGrid(const AStringGrid: TStringGrid; const AFilename: TFileName);
var
  sl    : TStringList;
  C1, C2: Integer;
begin
  sl := TStringList.Create;
  try
    sl.Add(IntToStr(AStringGrid.ColCount));
    sl.Add(IntToStr(AStringGrid.RowCount));
    for C1 := 0 to AStringGrid.ColCount - 1 do
      for C2 := 0 to AStringGrid.RowCount - 1 do
        sl.Add(AStringGrid.Cells[C1, C2]);
    sl.SaveToFile(AFilename);
  finally
    sl.Free;
  end;
end;
于 2013-05-24T19:08:52.697 回答