//连接excel
procedure TForm1.btnExcelConClick(Sender: TObject);
begin
btnAddData.Enabled:=true;
btnDiscon.Enabled:=true;
lcid:=getuserdefaultlcid();
excelapplication1.Connect;
excelapplication1.visible[lcid]:=true;
workbook1:=excelapplication1.workbooks.add(emptyparam,lcid);
end;
//断开连接
procedure TForm1.btnDisconClick(Sender: TObject);
begin
btnAddData.enabled:=false;
btnExcelCon.enabled:=false;
excelapplication1.activeworkbook.close(false,emptyparam,emptyparam,0);
excelapplication1.quit;
excelapplication1.disconnect;
end;
//把数据加入到excel中
procedure TForm1.btnAddDataClick(Sender: TObject);
var
ws
levariant;
i,j :integer;
s1 :variant;
begin
btnAddData.enabled:=false;
ws:= excelapplication1.activesheet as _worksheet;
ws.cells.item[2,2]:='EmpId';
ws.cells.item[2,3]:='EDptId';
ws.cells.item[2,4]:='EmpNo';
ws.cells.item[2,5]:='EmpCardNo';
ws.cells.item[2,6]:='EmpName';
ws.cells.item[2,7]:='EmpSex';
ws.cells.item[2,8]:='DDptId';
ws.cells.item[2,9]:='DptNo';
ws.cells.item[2,10]:='DptName';
ws.cells.item[2,11]:='CardId';
ws.range['a2:h2'].entirecolumn.horizontalalignment:= xlcenter;
IF query1.FindFirst then
begin
i:=0;
with dbgrid1.datasource.dataset do
while not query1.Eof do
begin
// gotobookmark(pointer(dbgrid1.items
));
for j:=0 to fieldcount-1 do
begin
if (j>=0) then
s1:=fields[j].value;
ws.cells.item[i+3,j+2]:=s1;
end;
query1.Next;
i:=i+1;
end;
end;
end;