一個小例子,自己看
procedure Tfrm_MCUsePlanList.bbtn_OutToExcelClick(Sender: TObject);
var
ExcelApp,worksheets,workbooks: Variant;
i: integer;
sTableName1,sTableName2,sTempFileName: string;
begin
if MainForm.TakeOperateFunc('N5',4) then
begin
ExcelApp := CreateOleObject( 'Excel.Application' );
//ExcelApp.WorkBooks.Open(od_Total.FileName);
SD_Excel.Filter:='Microsoft Excel Files (*.xls)|*.XLS';
if SD_Excel.Execute then
begin
if Pos('.XLS',UpperCase(SD_Excel.FileName))=0 then
sTempFileName:=SD_Excel.FileName+'.XLS'
else
sTempFileName:=SD_Excel.FileName;
if FileExists(sTempFileName)then
begin
if Application.MessageBox('ゅン?竒??,琌?滦籠玂??','矗ボ:',MB_ICONINFORMATION + MB_OKCANCEL) = IDOK then
DeleteFile(sTempFileName);
end;
//恶?计沮
try
ExcelApp := CreateOLEObject('Excel.Application');
except
Application.Messagebox('⊿Τ?杆Excel┪?Excel?杆ぃタ谔,ぃ?秈︽计沮锣?','矗ボ:',64);
Exit;
end;
WorkBooks := ExcelApp.WorkBooks.Add;
WorkSheets := WorkBooks.WorkSheets.Add;
//ExcelApp.ActiveWorkBook.WorkSheets.Add;
ExcelApp.Cells[1,1].Value := '?腹';
ExcelApp.Cells[1,2].Value := '???絏';
ExcelApp.Cells[1,3].Value := '???嘿';
ExcelApp.Cells[1,4].Value := '羆ノ秖';
ExcelApp.Cells[1,5].Value := '?潦计秖';
ExcelApp.Cells[1,6].Value := '?ノ计秖';
ExcelApp.Cells[1,7].Value := '??畐?';
ExcelApp.Cells[1,8].Value := '璹砯翴';
ExcelApp.Cells[1,9].Value := '畐?虫?';
ExcelApp.Cells[1,10].Value := '?潦矗玡戳';
ExcelApp.Cells[1,11].Value := '?杆砏?';
ExcelApp.Cells[1,12].Value := 'ㄑ莱坝';
if cb_Before7.Checked then
begin
sTableName1:='dbo.tbl_MOWFetchNoteInfo';
sTableName2:='dbo.tbl_MOWFetchNoteDetails';
end
else
begin
sTableName1:='dbo.tbl_MaterielPlan';
sTableName2:='dbo.tbl_MaterielPlanDetails';
end;
with DM_FlexSide.AQry_Report do
begin
Close;
SQL.Clear;
SQL.Add('Delete From TSBA');
ExecSQL;
Close;
SQL.Clear;
SQL.Add('Insert into TSBA Select * from [192.168.0.5].SMF.dbo.TSBA');
ExecSQL;
Close;
SQL.Clear;
SQL.Add('SELECT '+sTableName2+'.MateCode, '+sTableName2+'.MateDescription, dbo.TPDT.TPDT_LEAD,');
SQL.Add('SUM('+sTableName2+'.ExchangeNum) AS ExchangeNum, dbo.TSBA.TSBA_SC_QTY, dbo.TSBA.TSBA_AVA_QTY, dbo.TSBA.TSBA_PO_QTY,');
SQL.Add('dbo.TPDT.TPDT_SAF_STK, dbo.TPDT.TPDT_ORD_PNT, dbo.TPDT.TPDT_TPCK, dbo.tbl_BASPrdtInfo.TPDT_VEND,');
SQL.Add('dbo.TPDT.TPDT_DES1, dbo.TPDT.TPDT_DES2, dbo.TPDT.TPDT_DES3, dbo.TPDT.TPDT_DES4, dbo.TPDT.TPDT_STK_UOM ');
SQL.Add('FROM (SELECT * FROM '+sTableName1+' WHERE SUBSTRING('+sTableName1+'.NoteNo, 3, 2)=''MC'') ');
SQL.Add('vt_MOWFetchNoteInfo INNER JOIN '+sTableName2+' ON vt_MOWFetchNoteInfo.NoteNo = ');
SQL.Add(''+sTableName2+'.NoteNo AND vt_MOWFetchNoteInfo.NoteType = '+sTableName2+'.NoteType ');
SQL.Add('LEFT OUTER JOIN dbo.TPDT ON '+sTableName2+'.MateCode = dbo.TPDT.TPDT_CDE LEFT OUTER JOIN ');
SQL.Add('dbo.TSBA ON '+sTableName2+'.MateCode = dbo.TSBA.TSBA_PART LEFT OUTER JOIN ');
SQL.Add('dbo.tbl_BASPrdtInfo ON '+sTableName2+'.MateCode = dbo.tbl_BASPrdtInfo.PrdtNo');
SQL.Add('where ((ExchangeNum is not Null) AND (ExchangeNum<>0))');
if (Uppercase(trim(edt_PlanNo1.Text))<>'ALL')and(trim(edt_PlanNo1.Text)<>'') then
begin
if trim(edt_PlanNo2.Text)<>'' then
begin
SQL.Add(' AND (vt_MOWFetchNoteInfo.NoteNo between :NoteNo1 and :NoteNo2) ');
Parameters.ParamByName('NoteNo1').Value:=trim(edt_PlanNo1.Text);
Parameters.ParamByName('NoteNo2').Value:=trim(edt_PlanNo2.Text);
end
else
begin
SQL.Add(' AND (vt_MOWFetchNoteInfo.NoteNo > :NoteNo) ');
Parameters.ParamByName('NoteNo').Value:=trim(edt_PlanNo1.Text);
end;
end;
if (Uppercase(trim(edt_TPDT_CDE.Text))<>'ALL')and(trim(edt_TPDT_CDE.Text)<>'') then
begin
if trim(edt_TPDT_CDE2.Text)<>'' then
begin
SQL.Add(' And (TPDT.TPDT_CDE between :TPDT_CDE1 and :TPDT_CDE2)');
Parameters.ParamByName('TPDT_CDE1').Value:=trim(edt_TPDT_CDE.Text);
Parameters.ParamByName('TPDT_CDE2').Value:=trim(edt_TPDT_CDE2.Text);
end
else
begin
SQL.Add(' And (TPDT.TPDT_CDE > :TPDT_CDE)');
Parameters.ParamByName('TPDT_CDE').Value:=trim(edt_TPDT_CDE.Text);
end;
end;
if Uppercase(trim(edt_ContractNo.Text))<>'ALL' then
begin
SQL.Add(' and (vt_MOWFetchNoteInfo.ContractNo=:ContractNo)');
Parameters.ParamByName('ContractNo').Value:=trim(edt_ContractNo.Text);
end;
if (Uppercase(trim(edt_TPDT_NAM.Text))<>'ALL')and(trim(edt_TPDT_NAM.Text)<>'') then
begin
SQL.Add(' and ');
SQL.Add('((TPDT.TPDT_DES1 like :TPDT_DES1) or ');
SQL.Add('(TPDT.TPDT_DES2 like :TPDT_DES2) or ');
SQL.Add('(TPDT.TPDT_DES3 like :TPDT_DES3) or ');
SQL.Add('(TPDT.TPDT_DES4 like :TPDT_DES4))');
Parameters.ParamByName('TPDT_DES1').Value:='%'+trim(edt_TPDT_NAM.Text)+'%';
Parameters.ParamByName('TPDT_DES2').Value:='%'+trim(edt_TPDT_NAM.Text)+'%';
Parameters.ParamByName('TPDT_DES3').Value:='%'+trim(edt_TPDT_NAM.Text)+'%';
Parameters.ParamByName('TPDT_DES4').Value:='%'+trim(edt_TPDT_NAM.Text)+'%';
end;
if (Uppercase(trim(edt_S_TPDT_LEAD1.Text))<>'ALL')and(trim(edt_S_TPDT_LEAD1.Text)<>'') then
begin
SQL.Add(' and ');
if trim(edt_S_TPDT_LEAD2.Text)<>'' then
begin
SQL.Add(' (TPDT.TPDT_LEAD between :TPDT_LEAD1 and :TPDT_LEAD2)');
Parameters.ParamByName('TPDT_LEAD1').Value:=trim(edt_S_TPDT_LEAD1.Text);
Parameters.ParamByName('TPDT_LEAD2').Value:=trim(edt_S_TPDT_LEAD2.Text);
end
else
begin
SQL.Add(' (TPDT.TPDT_LEAD > :TPDT_LEAD)');
Parameters.ParamByName('TPDT_LEAD').Value:=trim(edt_S_TPDT_LEAD1.Text);
end;
end;
if (Uppercase(trim(edt_FilialeNo.Text))<>'ALL')and(trim(edt_FilialeNo.Text)<>'') then
begin
SQL.Add(' and ');
SQL.Add('(Left(vt_MOWFetchNoteInfo.NoteNo,2)=:NoteNo)');
Parameters.ParamByName('NoteNo').Value:=trim(edt_FilialeNo.Text);
end;
if (Uppercase(trim(edt_NoteDate1.Text))<>'ALL')and(trim(edt_NoteDate1.Text)<>'') then
begin
SQL.Add(' and ');
if trim(edt_NoteDate2.Text)<>'' then
begin
SQL.Add(' (vt_MOWFetchNoteInfo.NoteDate between :NoteDate1 and :NoteDate2)');
Parameters.ParamByName('NoteDate1').Value:=trim(edt_NoteDate1.Text);
Parameters.ParamByName('NoteDate2').Value:=trim(edt_NoteDate2.Text);
end
else
begin
SQL.Add(' (vt_MOWFetchNoteInfo.NoteDate > :NoteDate1)');
Parameters.ParamByName('NoteDate1').Value:=trim(edt_NoteDate1.Text);
end;
end;
SQL.Add(' GROUP BY '+sTableName2+'.MateCode, '+sTableName2+'.MateDescription, dbo.TPDT.TPDT_LEAD,');
SQL.Add('dbo.TSBA.TSBA_SC_QTY, dbo.TSBA.TSBA_AVA_QTY, dbo.TSBA.TSBA_PO_QTY, dbo.TPDT.TPDT_SAF_STK, dbo.TPDT.TPDT_ORD_PNT,');
SQL.Add('dbo.TPDT.TPDT_TPCK, dbo.tbl_BASPrdtInfo.TPDT_VEND, dbo.TPDT.TPDT_DES1, dbo.TPDT.TPDT_DES2,');
SQL.Add('dbo.TPDT.TPDT_DES3, dbo.TPDT.TPDT_DES4, dbo.TPDT.TPDT_STK_UOM');
Open;
if not IsEmpty then
begin
i:=2;
while not Eof do
begin
ExcelApp.Cells[i,1].Value := IntToStr(i-1);
ExcelApp.Cells[i,2].Value := trim(FieldByName('MateCode').AsString);
ExcelApp.Cells[i,3].Value := trim(FieldByName('MateDescription').AsString);
ExcelApp.Cells[i,4].Value := trim(FieldByName('ExchangeNum').AsString);
ExcelApp.Cells[i,5].Value := trim(FieldByName('TSBA_PO_QTY').AsString);
ExcelApp.Cells[i,6].Value := trim(FieldByName('TSBA_AVA_QTY').AsString);
ExcelApp.Cells[i,7].Value := trim(FieldByName('TPDT_SAF_STK').AsString);
ExcelApp.Cells[i,8].Value := trim(FieldByName('TPDT_ORD_PNT').AsString);
ExcelApp.Cells[i,9].Value := trim(FieldByName('TPDT_STK_UOM').AsString);
ExcelApp.Cells[i,10].Value := trim(FieldByName('TPDT_LEAD').AsString);
ExcelApp.Cells[i,11].Value := trim(FieldByName('TPDT_TPCK').AsString);
ExcelApp.Cells[i,12].Value := trim(FieldByName('TPDT_VEND').AsString);
Next;
inc(i);
end;
WorkSheets.SaveAs(sTempFileName);
try
WorkBooks.Close(SaveChanges:=False);
except
end;
if not VarIsEmpty(ExcelApp) then
begin
ExcelApp.WorkBooks.Close;
ExcelApp.DisplayAlerts := False;
ExcelApp.Quit;
end;
showmessage('计沮?Θ?旧??');
end
{ExcelApp.ActiveWorkBook.Saved:=True;
ExcelApp.ActiveWorkBook.Save;//玂?
ExcelApp.WorkBooks.Close;
ExcelApp.Quit;
Application.MessageBox('计沮?Θ?旧?!','矗ボ:',MB_OK);
end}
else
Application.MessageBox('琩高挡狦??!','矗ボ:',MB_OK);
end;
end;
end
else
Application.MessageBox('',MB_ICONINFORMATION);
end;