首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从文本网格中检索数据并将检索到的数据填充到word中包含的图表的快速方法

从文本网格中检索数据并将检索到的数据填充到word中包含的图表的快速方法
EN

Stack Overflow用户
提问于 2016-09-21 05:14:47
回答 1查看 834关注 0票数 2

我已经存储了一个包含TStringgrid表的记录在一个长的blob字段中,在一个数据库中的一个表中。我正在使用下面的代码检索存储在DB中的表中的长blob。但是,它是非常慢的。有没有人建议使用delphi快速打印存储在DB中的字符串网格数据到word中的图表。

代码语言:javascript
复制
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}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-21 05:58:17

根据评论,瓶颈是分配给

代码语言:javascript
复制
AWorkSheet.Cells.Item[i, (j-6)].value

在循环中。这是自动化Excel时经常犯的错误。每次调用自动化Excel都会产生很大的开销,因为您要对不同的进程执行COM调用。这样做有很大的开销。

不是使用N个不同的调用来设置数据,每个单元一个调用来设置数据,而是使用一个分配给所有N个单元的调用来设置数据。为此,选择一个表示工作表的整个目标区域的范围,并在一次调用中分配一个包含所有N个项的变量数组。

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

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39603832

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档