export delphi stringgrid to excel -


i'm trying export data stringgrid in delphi 7 microsoft excel. have been using code it:

  objexcel := texcelapplication.create(nil);   objexcel.visible[locale_user_default] := true;   objwb := objexcel.workbooks.add(null,locale_user_default);   linenumber := 1;    i:=1 stringgrid1.rowcount-1 begin     j:=0 stringgrid1.colcount-1 begin       objwb.worksheets.application.cells.item[i+linenumber,j+1] := ''''+stringgrid1.cells[j,i];     end;   end; 

but when data big, takes long time finish. there other faster way export data delphi 7 stringgrid excel?

the quickest way use array of variant,and pass entire array excel:

var   xls, wb, range: olevariant;   arrdata: variant;   rowcount, colcount, i, j: integer; begin   {create variant array we'll copy our data}   rowcount := stringgrid1.rowcount;   colcount := stringgrid1.colcount   arrdata := vararraycreate([1, rowcount, 1, colcount], varvariant);    {fill array}   := 1 rowcount     j := 1 colcount       arrdata[i, j] := stringgrid1.cells[j-1, i-1];    {initialize instance of excel}   xls := createoleobject('excel.application');    {create workbook}   wb := xls.workbooks.add;    {retrieve range data must placed}   range := wb.worksheets[1].range[wb.worksheets[1].cells[1, 1],                                   wb.worksheets[1].cells[rowcount, colcount]];    {copy data allocated variant array}   range.value := arrdata;    {show excel our data}   xls.visible := true; end; 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -