我已将包含 TStringgrid 表的记录存储在数据库表中的长 blob 字段中。我正在使用下面的代码检索存储在数据库表中的长 blob。但它的速度却非常慢。有人可以建议使用delphi将数据库中作为longblob存储的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 项的变体数组。
可以在这里找到一些类似的示例代码:c++ Excel OLE automation. Setting the values of an entire cell-range 'at once'
关于delphi - 从 Tstringgrid 检索数据并将检索到的数据填充到包含 word 的图表中的快速方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39603832/