2016-09-20 4 views
2

DB内のテーブルの長いBLOBフィールドにTStringgridテーブルを含むレコードを格納しました。次のコードを使用して、DB内のテーブルに格納されている長いブロブを取得しています。しかし、それは非常に遅いです。デルファイを使用してDB内のlongblobとして保存されたtstringgridデータを単語のグラフに素早く印刷する方法を提案する人もいます。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} 
+1

は、いくつかのプロファイリングを行います。どの部分が遅いか分かります。 –

+0

私は10000以上のレコードを保存しています。レコード数を減らすと速くなります – mdel

+1

ボトルネックはおそらく 'AWorkSheet.Cells.Item [i、(j-6)]。value:= StrToFloat(substring);'の呼び出しです。どの部分が遅いかを知るためにプロファイルを作成する必要があります。上記のコードでは、さまざまな場所でパフォーマンスを向上させるために書き直しや再設計が可能な半ダースのことがありますが、遅い部分を修正しない限り、暗闇の中で盲目的に突き刺し、成功しない可能性があります。 –

答えて

4

コメントによれば、ボトルネックは、ループ内

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

に割り当てています。これは、Excelを自動化する際によくある間違いです。 COMコールを別のプロセスに実行しているため、Excelを自動化するたびに大きなコストがかかります。これを行うには重大なオーバーヘッドがあります。

N個の別個の呼び出し(各セルに1つずつ)でデータを設定する代わりに、すべてのN個のセルに割り当てる1回の呼び出しでデータを設定します。ワークシートの対象領域全体を表す範囲を選択し、N個のすべての項目を含むバリアント配列を1回の呼び出しで割り当てます。

その静脈内のいくつかのサンプルコードは、ここで見つけることができます:c++ Excel OLE automation. Setting the values of an entire cell-range 'at once'

+0

ありがとうございました http://stackoverflow.com/questions/16641897/export-delphi-stringgrid-to-excel – mdel

関連する問題