用delphi导出数据到excel(100分)

  • 主题发起人 主题发起人 祥宇
  • 开始时间 开始时间

祥宇

Unregistered / Unconfirmed
GUEST, unregistred user!
最好给出实际例子及源代码
急需
要快!!!!

 
我有,你的email
 
procedure TLatencyQuery.BitBtn5Click(Sender: TObject);
var tclientid,i:integer;
excelapp,myworkbook:variant;
strfilename,tname,tel,address,starttime,vehiclecode,price,memo:string;
begin
if not query1.Active then
begin
showmessage('尚未查询!');
exit;
end;
if query1.RecordCount=0 then
begin
showmessage('没有查询结果!');
exit;
end;
try
excelapp:=createoleobject('excel.application');
myworkbook:=createoleobject('excel.sheet');
except
showmessage('无法导入EXCEL文件,请确认已经安装EXCEL!');
exit;
end;
SaveDialog1.Execute;
strfilename:=SaveDialog1.filename;
myworkbook:=excelapp.workbooks.add;

myworkbook.worksheets[1].Rows[1].Font.Name:='宋体';
myworkbook.worksheets[1].Rows[1].Font.Color:= clblack;
myworkbook.worksheets[1].Rows[1].Font.Bold:= True;
myworkbook.worksheets[1].Rows[1].Font.size:=20;
myworkbook.worksheets[1].Rows[2].Font.Bold:= True;
myworkbook.worksheets[1].cells[1,4]:='客户回访记录';
myworkbook.worksheets[1].cells[2,1]:='姓名';
myworkbook.worksheets[1].cells[2,2]:='联系电话';
myworkbook.worksheets[1].cells[2,3]:='住址';
myworkbook.worksheets[1].cells[2,4]:='所购车型';
myworkbook.worksheets[1].cells[2,5]:='价格';
myworkbook.worksheets[1].cells[2,6]:='日期';
myworkbook.worksheets[1].cells[2,7]:='反馈信息';
myworkbook.worksheets[1].cells[2,8]:='备注';
query1.First;
i:=1;
while not query1.Eof do
begin
tclientid:=query1.fieldbyname('clientid').AsInteger;
tname:=query1.fieldbyname('name').AsString;
tel:=query1.fieldbyname('tel').AsString+' '
+query1.fieldbyname('mobile').AsString ;
address:=query1.fieldbyname('address').AsString;


query4.Close;
query4.SQL.Clear;
query4.SQL.Add('select client.name,client.tel,client.mobile,');
query4.SQL.Add(' client.address,outproduct.vehiclecode,outproduct.unitcash,');
query4.SQL.Add(' out.starttime,out.memo');
query4.SQL.Add(' from out,client,outproduct');
query4.SQL.Add(' where out.clientid=client.clientid');
query4.SQL.Add(' and client.clientid='+inttostr(tclientid));
query4.SQL.Add(' and out.depno='+inttostr(mainform.ndepno));
query4.SQL.Add(' and outproduct.outno=out.outno ');
query4.SQL.Add(' and out.type=0 ');
query4.SQL.Add(' and out.state<>5 ');
query4.SQL.Add(' and out.state<>4 ');
query4.SQL.Add(' and out.state<>3 ');
query4.Prepare;
query4.Open;
if query4.RecordCount=0 then
begin
myworkbook.worksheets[1].cells[2+i,1]:=tname;
myworkbook.worksheets[1].cells[2+i,2]:=tel;
myworkbook.worksheets[1].cells[2+i,3]:=address;
myworkbook.worksheets[1].cells[2+i,4]:='无';
myworkbook.worksheets[1].cells[2+i,5]:='0';
myworkbook.worksheets[1].cells[2+i,6]:=starttime;
myworkbook.worksheets[1].cells[2+i,8]:='尚未购买车辆';
i:=i+1;
end
else
begin
query4.First;
while not query4.Eof do
begin
vehiclecode:=query4.fieldbyname('vehiclecode').asstring;
price:=query4.fieldbyname('unitcash').asstring;//单价
memo:=query4.fieldbyname('memo').asstring;
starttime:=query4.fieldbyname('starttime').AsString;
myworkbook.worksheets[1].cells[2+i,1]:=tname;
myworkbook.worksheets[1].cells[2+i,2]:=tel;
myworkbook.worksheets[1].cells[2+i,3]:=address;
myworkbook.worksheets[1].cells[2+i,4]:=vehiclecode;
myworkbook.worksheets[1].cells[2+i,5]:=price;
myworkbook.worksheets[1].cells[2+i,6]:=starttime;
myworkbook.worksheets[1].cells[2+i,8]:=memo;
i:=i+1;
query4.Next;
end;
end;

query1.Next;
end;
try
myworkbook.saveas(strfilename);
myworkbook.close;
excelapp.quit;
excelapp:=unassigned;
except
myworkbook.close;
excelapp.quit;
end;

end;
 
谢谢
我的邮箱:tom007_81meng@163.com
最好是实例!
 
lotusleaf,老兄,你是不是少了些东西。scExcelexport.dcu不知道。系统老是报错。错在哪里?
说明一下,我不会这方面的东西。
 
后退
顶部