I'm trying to export data from a stringgrid in delphi 7 to microsoft excel. I have been using this code to do it:
objExcel := TExcelApplication.Create(nil);
objExcel.Visible[LOCALE_USER_DEFAULT] := true;
objWB := objExcel.workbooks.add(null,LOCALE_USER_DEFAULT);
lineNumber := 1;
for i:=1 to stringgrid1.rowcount-1 do begin
for j:=0 to stringgrid1.ColCount-1 do begin
objWB.Worksheets.Application.Cells.Item[i+lineNumber,j+1] := ''''+stringgrid1.Cells[j,i];
end;
end;
but when the data is big, it takes a very long time to finish. is there other faster way to export data from delphi 7 stringgrid to excel?
The problem is that you are calling the Excel object for every cell; this is a slow operation at the best of times, so doing this for a large number of cells is going to take a long time. I had a case of this not so long ago: 4000 rows with 9 columns took about 44 seconds to transfer to Excel.
My current solution involves creating a csv file then importing that csv into Excel.
Another way comes from Mike Shkolnik which I am quoting as is:
I suggest that you try both methods and see which is faster for your purposes.
The quickest way is to use an array of Variant,and just pass the entire array to Excel: