代碼如下:幫忙看看
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, DBTables, comobj, StdCtrls, Buttons, Grids, DBGrids;
type
TForm1 = class(TForm)
DBGrid1: TDBGrid;
Label1: TLabel;
BitBtn1: TBitBtn;
BitBtn3: TBitBtn;
DB1: TDatabase;
Query1: TQuery;
DataSource1: TDataSource;
BitBtn2: TBitBtn;
SaveDialog1: TSaveDialog;
procedure BitBtn3Click(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.BitBtn3Click(Sender: TObject);
begin
close;
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
var sql:string;
begin
query1.SQL.Clear;
query1.SQL.Add('select DISTINCT a.op_no,a.op_date,b.cusord_no,c.exop_no,d.cus_no,i.cussite_no,a.due_date,a.need_date,');
query1.SQL.Add('e.st_no,f.ship_no,f.ship_name,convert(nvarchar(40),k.excus_name) as excus_name,');
query1.SQL.Add('convert(nvarchar(40),k.excussite_name) as excussite_name,g.inv_no,g.inv_name,g.inv_spec,');
query1.SQL.Add('a.qty,isnull(h.pac_qty,0) as pac_qty,a.qty-isnull(h.pac_qty,0) as f_qty');
query1.SQL.Add('from soordd a');
query1.SQL.Add('left join soordm b on a.orderm_id=b.orderm_id');
query1.SQL.Add('left join scmpo_in c on b.cusord_no=c.exop_no');
query1.SQL.Add('left join bstcus d on a.cus_id=d.cus_id');
query1.SQL.Add('left join mtlstorage e on a.st_id=e.st_id');
query1.SQL.Add('left join bstship f on a.ship_id=f.ship_id');
query1.SQL.Add('left join mtlinv g on a.inv_id=g.inv_id');
query1.SQL.Add('left join mtltran h on a.orderd_id=h.orderd_id');
query1.SQL.Add('left join bstcussite i on b.cussite_id=i.cussite_id');
query1.SQL.Add('left join scmpoline_in j on c.selfsoa_id=j.selfsob_id');
query1.SQL.Add('left join scmso_in k on c.soscm_uid=k.scm_uid');
query1.SQL.Add('where a.state=1');
query1.SQL.Add('order by a.op_no');
query1.Prepare;
query1.Open;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
DB1.DriverName:='MSSQL';
DB1.Params.Clear;
DB1.Params.Add('SERVER NAME=MIS-08');
DB1.Params.Add('DATABASE NAME=AXMTWDB');
DB1.Params.Add('USER NAME=sa');
DB1.DatabaseName:='MSQL';
DB1.LoginPrompt:=false;
DB1.Connected:=TRUE;
Query1.DatabaseName:='MSQL';
Query1.RequestLive:=False;
DBGrid1.ReadOnly:=true;
end;
procedure TForm1.BitBtn2Click(Sender: TObject);
var
i:integer;
xlsfile:string;
elcapp,workbook:variant;
begin
savedialog1.Execute;
xlsfile:=savedialog1.filename;
elcapp:=Createoleobject('excel.application');
workbook:=createoleobject('excel.sheet');
workbook:=elcapp.workbooks.add;
elcapp.cells(1,1):='銷售訂單';
elcapp.cells(1,2):='訂單日期';
elcapp.cells(1,3):='採購訂單';
elcapp.cells(1,4):='客戶PO';
elcapp.cells(1,5):='客戶編號';
elcapp.cells(1,6):='地址編號';
elcapp.cells(1,7):='預定交期';
elcapp.cells(1,8):='生管CFM交期';
elcapp.cells(1,9):='倉庫編號';
elcapp.cells(1,10):='運送方式編號';
elcapp.cells(1,11):='運送方式';
elcapp.cells(1,12):='客戶名稱';
elcapp.cells(1,13):='交貨地址';
elcapp.cells(1,14):='機種編號';
elcapp.cells(1,15):='機種名稱';
elcapp.cells(1,16):='規格';
elcapp.cells(1,17):='數量';
elcapp.cells(1,18):='已交數量';
elcapp.cells(1,19):='未交數量';
query1.First;
for i:=0 to query1.recordcount-1 do
begin
elcapp.cells(i 2,1):=query1.fieldbyname('op_no').asstring;
elcapp.cells(i 2,2):=query1.fieldbyname('op_date').asstring;
elcapp.cells(i 2,3):=query1.fieldbyname('cusord_no').asstring;
elcapp.cells(i 2,4):=query1.fieldbyname('exop_no').asstring;
elcapp.cells(i 2,5):=query1.fieldbyname('cus_no').asstring;
elcapp.cells(i 2,6):=query1.fieldbyname('cussite_no').asstring;
elcapp.cells(i 2,7):=query1.fieldbyname('due_date').asstring;
elcapp.cells(i 2,8):=query1.fieldbyname('need_date').asstring;
elcapp.cells(i 2,9):=query1.fieldbyname('st_no').asstring;
elcapp.cells(i 2,10):=query1.fieldbyname('ship_no').asstring;
elcapp.cells(i 2,11):=query1.fieldbyname('ship_name').asstring;
elcapp.cells(i 2,12):=query1.fieldbyname('excus_name').asstring;
elcapp.cells(i 2,13):=query1.fieldbyname('excussite_name').asstring;
elcapp.cells(i 2,14):=query1.fieldbyname('inv_no').asstring;
elcapp.cells(i 2,15):=query1.fieldbyname('inv_name').asstring;
elcapp.cells(i 2,16):=query1.fieldbyname('inv_spec').asstring;
elcapp.cells(i 2,17):=query1.fieldbyname('qty').asstring;
elcapp.cells(i 2,18):=query1.fieldbyname('pac_qty').asstring;
elcapp.cells(i 2,19):=query1.fieldbyname('f_qty').asstring;
query1.Next;
end;
workbook.saveas(xlsfile);
workbook.close;
showmessage('恭喜你,轉出成功\');
end;
end.