贴出整个导出程序,共大家参考!
注: 本程序按模版文件导出,模版文件大家可以自己作一个!
procedure actExport_E_VExecute(Sender: TObject);
const
msoFalse = $00000000; //------ 定义该变量,就可以不用引用Office2000单元 -----------
msoCTrue = $00000001;
var
sFile,si,sf : string;
i,f,iCount : Integer;
begin
inherited;
PostQuery;
ShowMsg('注意: 导出前请先确认已经关闭 Excel 软件!',False);
//CloseAllExcelApps;
if qryData.IsEmpty or qryGoods.IsEmpty then
begin
ShowMsg('没有数据,不能导出!',False);
Exit;
end;
try
EApp.Connect; //EA.Visible[0] := True; {Show}
except
ShowMsg('Excel may not be installed!',False);
Exit;
end;
sFile := Get_xls_MB_FileName('MB2.xls');
if not FileExists(sFile) then
begin
ShowMsg('没有发现模版文件:' + sFile + ',不能导出!',False);
Exit;
end;
try
EApp.Workbooks.Open(sFile,null,null,null,null,null,null,null,null,null,null,null,null,0);
except
EApp.Disconnect;//出现异常情况时关闭
EApp.Quit;
ShowMsg('打开文件: ' + sFile + ' 出错!',False);
exit;
end;
try
Screen.Cursor := crHourGlass;
EBooks.ConnectTo(EApp.Workbooks[1]); //EWB.ConnectTo(EA.Workbooks.Add(EmptyParam,0));
ESheet.ConnectTo(EBooks.Sheets[1] as _Worksheet);
//ESheet.Shapes.AddPicture('J:/222.jpg',msoFalse,msoCTrue,10,10,300,300);
ESheet.Cells.Item[1,9] := qryData.FieldByName('QHNO').AsString;
ESheet.Cells.Item[2,9] := DateToStr(qryData.FieldByName('INPUTDATE').AsDateTime);
ESheet.Range['A8','D11'].Value := qryData.FieldByName('ENGNAME').AsString + #10 + //不能加#13
qryData.FieldByName('CAddr').AsString + #10 +
qryData.FieldByName('EADDR1').AsString + #10 +
qryData.FieldByName('EADDR2').AsString ;
ESheet.Range['E8','I11'].Value := qryData.FieldByName('PAYCOND').AsString ;
ESheet.Range['G12','I12'].Value := DateToStr(qryData.FieldByName('INVALIDDATE').AsDateTime);
ESheet.Range['C13','D13'].Value := qryData.FieldByName('FORMPORTNAME').AsString;
ESheet.Range['G13','I13'].Value := qryData.FieldByName('CLOSEDAY').AsString + ' Days';
ESheet.Range['C14','D14'].Value := qryData.FieldByName('TOPORTNAME').AsString;
qryGoods.First;
i := 18;
while not qryGoods.Eof do
begin
si := IntToStr(i);
ESheet.Range['A'+ si ,'A' + si].Value := qryGoods.FieldByName('GOODSNO').AsString;
//ESheet.Range['B'+ si ,'C' + si].Select;
//ESheet.Range['B'+ si ,'C' + si].RowHeight := 100;
ESheet.Range['B'+ si ,'C' + si].MergeCells := True;
ESheet.Range['B'+ si ,'C' + si].WrapText := True;
//------ 以下设置格子的高度,也可以算出最高的,然后一次性付值 ----------------
ESheet.Range['B'+ si ,'C' + si].RowHeight := 15 * GetHeight_xls(qryGoods.FieldByName('QGOODSNAME').AsString); //--- 一个字6个像素 ----------}
ESheet.Range['B'+ si ,'C' + si].Value := qryGoods.FieldByName('QGOODSNAME').AsString;{ + #10 + ' aaa' ;}
ESheet.Range['D'+ si ,'E' + si].MergeCells := True;
ESheet.Range['D'+ si ,'E' + si].WrapText := True;
ESheet.Range['D'+ si ,'E' + si].Value := qryGoods.FieldByName('PGPNAME').AsString;
ESheet.Range['F'+ si ,'F' + si].Value := FloatToStr(qryGoods.FieldByName('MIN_QTY').AsFloat);
ESheet.Range['G'+ si ,'G' + si].Value := FloatToStr(qryGoods.FieldByName('OUT_V').AsFloat);
//ESheet.Range['H'+ si ,'H' + si].Value := FloatToStr(qryGoods.FieldByName('V_Goods').AsFloat);
ESheet.Range['H'+ si ,'H' + si].Value := FloatToStr(qryGoods.FieldByName('ILENGTH').AsFloat) + 'x' +
FloatToStr(qryGoods.FieldByName('WIDTH').AsFloat) + 'x' +
FloatToStr(qryGoods.FieldByName('HEIGHT').AsFloat) ;
ESheet.Range['I'+ si ,'I' + si].Value := FloatToStr(qryGoods.FieldByName('PAY_PRICE').AsFloat);
i := i + 1;
qryGoods.Next;
end;
sFile := Get_xls_Cust_FileName(qryData.FieldByName('SNO').AsString);// GetCurrentDir + '/Test' + FormatDateTime('hhnnss',now) + '.xls';
ESheet.SaveAs(sFile);
finally
ESheet.Disconnect;
EBooks.Close;
EBooks.Disconnect;
EApp.Disconnect; //EA.Quit; //EA := unassigned; //EA := null;
//CloseAllExcelApps;
Screen.Cursor := crDefault;
ShowMsg('成功导出报价文件到:' + sFile + ' !',False);
end;
end;