天天看點

技巧一:Delphi XE3 Excel導入資料到StringGrid

procedure tdataeditdrfrm.btn8click(sender:

tobject);

var

excelx,excely:string;

excelapp:variant;

workbook:olevariant;

excelrowcount,excelcolumncount:longint;

i,j,row,row1:integer;

begin

row1:=1;

pb1.min:=0;

dlgopen1.filter:=‘excel檔案|*.xls|*.xlsx‘;

if dlgopen1.execute then

try

excelapp:=createoleobject(‘excel.application‘);

workbook:=excelapp.workbooks.open(dlgopen1.filename);

excelapp.visible:=false;

excelrowcount:=excelapp.activesheet.usedrange.rows.count;

row:=1;

pb1.max:=excelrowcount;

for i := 1 to excelrowcount do

excelx:= excelapp.cells[i,1].value;

excely:=excelapp.cells[i,2].value;

if (excelx<>‘‘)

and (excely<>‘‘) then

if

strngrd1.rowcount<i then

strngrd1.rowcount:=strngrd1.rowcount+1;

strngrd1.cells[0,row]:=inttostr(row);

strngrd1.cells[1,row]:=excelx;

strngrd1.cells[2,row]:=excely;

row:=row+1;

end;

pb1.stepby(1);

finally

workbook.close;

excelapp.quit;

excelapp

:= unassigned;

workbook := unassigned;