从 Tstringgrid 检索数据并将检索到的数据填充到包含 word 的图表中的快速方法

Fast way to retrieve data from Tstringgrid and to populate the retrieved data in chart containing in word

我已将包含 TStringgrid table 的记录存储在数据库中 table 的长 blob 字段中。我正在使用下面的代码检索存储在数据库中 table 中的长 blob。但是它非常慢。有人可以建议使用 delphi.

将存储为数据库中的 longblob 的 tstringgrid 数据快速打印到图表中的方法吗
Field := mySQLQuery1.FieldByName('Table');  //Accessing the table field in DB
blob := mySQLQuery1.CreateBlobStream(Field, bmRead); 
F := TStringList.Create;
try
  F.LoadFromStream(blob); //To load blob into string list
  try
    rowCount:= StrToInt(F[0])-1; //To get the total count of rows in string grid
    colCount:= StrToInt(F[1]); //To get the total count of columns in string grid

    aShape := WordApplication1.ActiveDocument.InlineShapes.Item(1); //To access the excel embedded chart in word
    ashape.OLEFormat.Activate;
    control2 := aShape.OLEFormat.Object_ as ExcelWorkBook;
    AWorkSheet := control2.sheets['Table1'] as ExcelWorkSheet; //To access the sheet in word
  except
    on E: Exception do
      MessageDlg(E.Message, mtInformation, [mbOk], 0);
  end; { try }

  i:= 2;           
  while i <= rowCount do
  begin
    str:=F[i + 2];  
    //The values of each row stored in Tstringgrid for example are of the followingorder 
    //',,,,,,"0,00011","13,6714","0,00023","13,5994"'

    for j := 1 to colCount do
    begin
      a:=pos('"',str);
      b:=pos(',',str);
      if (b<a) OR (a=0) then //To get and remove all null values by using searching for , delimiter
      begin
        if b=0 then substring:=str
        else
        begin
          substring:=copy(str,0,pos(',',str)-1);
          str:=copy(str,pos(',',str)+1,length(str));
        end; {if}
      end {if}
      else
      begin   //To get all values by using searching for " delimiter
        str:=copy(str,pos('"',str)+1, length(str));
        substring:=copy(str,0,pos('"',str)-1);
        str:=copy(str,pos('"',str)+2,length(str));
      end; {else}
      if substring<> '' then
      begin
        AWorkSheet.Cells.Item[i, (j-6)].value := StrToFloat(substring);
      end;
    end; {for j}
    i := i + 1;
  end;{ while i}
finally
  F.Free;
  freeandnil(blob);
end; {try}

根据评论,瓶颈是分配给

AWorkSheet.Cells.Item[i, (j-6)].value

在一个循环中。这是自动化 Excel 时常犯的错误。每次调用 automate Excel 都会产生大量成本,因为您正在对不同的进程执行 COM 调用。这样做会产生严重的开销。

与其使用 N 个不同的调用(每个单元一个)设置数据,不如使用一个分配给所有 N 个单元的调用来设置数据。为此,请选择一个代表工作表整个目标区域的范围,并在一次调用中分配一个包含所有 N 项的变体数组。

可以在此处找到类似的一些示例代码:c++ Excel OLE automation. Setting the values of an entire cell-range 'at once'