Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Delphi 从Tstringgrid检索数据并将检索到的数据填充到包含在word中的图表中的快速方法_Delphi_Delphi 2007 - Fatal编程技术网

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

Delphi 从Tstringgrid检索数据并将检索到的数据填充到包含在word中的图表中的快速方法,delphi,delphi-2007,Delphi,Delphi 2007,我已将包含TStringgrid表的记录存储在数据库中具有的表的长blob字段中。我使用下面的代码检索存储在数据库中的表中的长blob。但是,这是非常缓慢的。有人能推荐一种使用delphi将存储在DB中的tstringgrid数据打印到word中的图表的快速方法吗 Field := mySQLQuery1.FieldByName('Table'); //Accessing the table field in DB blob := mySQLQuery1.CreateBlobStream(Fi

我已将包含TStringgrid表的记录存储在数据库中具有的表的长blob字段中。我使用下面的代码检索存储在数据库中的表中的长blob。但是,这是非常缓慢的。有人能推荐一种使用delphi将存储在DB中的tstringgrid数据打印到word中的图表的快速方法吗

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时常见的错误。每次调用自动化Excel都会产生巨大的成本,因为您要对不同的流程执行COM调用。这样做会产生严重的开销

不要将数据设置为N个不同的调用(每个单元格一个),而是将数据设置为一个分配给所有N个单元格的调用。通过选择一个代表工作表整个目标区域的范围,并在一次调用中指定一个包含所有N项的变量数组来完成此操作


这里可以找到一些类似的示例代码:

进行一些分析。找出哪个部分慢。我存储了10000多条记录。当我使用更少的记录时速度会更快。据猜测,瓶颈可能是对工作表.Cells.Item[i,j-6]的调用。值:=strotfloatsubstring;,但你真的需要分析一下,找出哪部分速度慢。您可以在上面的代码中重写或重新设计半打内容,以在不同的地方提高性能,但除非您修复缓慢的部分,否则您只是在黑暗中盲目刺痛,很可能不会成功。@J。。。你完全正确。我刚刚注释掉了您提到的行,代码运行得非常快。那么,你能给我建议一种更好的方法来快速填写工作表中的项目以生成图表吗?@DavidHeffernan在他的回答中给出了一个很好的开始位置——通常使用Excel,你希望将数组数据传输到某个范围以进行此类操作。有很多关于如何做到这一点的例子。找到了谢谢