procedure TForm2.Button1Click(Sender: TObject);
var v,sheet,range
levariant ;
j:integer ;
begin
with mydata.qyblack do
begin
close ;
sql.Clear ;
sql.Add('select * from blacktb where w_ddate>='+#39+edit1.text+#39+' and w_ddate<='+#39+edit2.text+#39);
open ;
end;
if mydata.qyblack.RecordCount=0 then
begin
showmessage('is not data!');
exit ;
end;
v :=createoleobject('excel.application');
v.workbooks.add ;
v.workbooks[1].worksheets[1].name :='aaa';
sheet:=v.workbooks[1].worksheets[1];
v.visible :=true;
range:=sheet.range['A1:H1'];
range.merge ;
range.formular1c1:=' ('+datetostr(datetimepicker1.Date)+'至'+datetostr(datetimepicker2.Date)+')開除工人名單';
sheet.cells[3,1]:='身份証編號';
sheet.cells[3,2]:='名 稱';
sheet.cells[3,3]:='省 份';
sheet.cells[3,4]:='地 址';
sheet.cells[3,5]:='入廠日期';
sheet.cells[3,6]:='離廠日期';
sheet.cells[3,7]:='原因';
sheet.cells[3,8]:='原所在廠';
mydata.qyblack.First ;
j:=3 ;
while not mydata.qyblack.Eof do
begin
j:=j+1 ;
sheet.cells[j,1].numberformatlocal :='@';
sheet.cells[j,1]:=mydata.qyblack.FieldByName('w_id').AsString ;
sheet.cells[j,2]:=mydata.qyblack.FieldByName('w_cname').AsString ;
sheet.cells[j,3]:=mydata.qyblack.FieldByName('w_nation').AsString ;
sheet.cells[j,4]:=mydata.qyblack.FieldByName('w_addr').AsString ;
sheet.cells[j,5]:=mydata.qyblack.FieldByName('w_cdate').AsString ;
sheet.cells[j,6]:=mydata.qyblack.FieldByName('w_ddate').AsString ;
sheet.cells[j,7]:=mydata.qyblack.FieldByName('w_reason').AsString ;
sheet.cells[j,8]:=mydata.qyblack.fieldbyname('w_factory').AsString ;
mydata.qyblack.Next ;
if mydata.qyblack.Eof then break ;
end;
sheet.cells.entirecolumn.autofit ;
// v.selection.entirecolumn.atutofit ;
// v.displayalerts :=false ;
sheet.printpreview ;
end;
我才寫的,給你個參考吧,希望可以幫你