要全部释放variant变量 ,否则Excel进程无法关闭
- var J: integer; begin Screen.Cursor := crHourGlass; //创建Ole对象 try ExcelApplication := CreateOleObject('Excel.Application'); except ExcelApplication := Null; Screen.Cursor := crDefault; MSShow('请先安装MicrSoft Office Excel97/2000。'); Exit; end; ExcelApplication.Visible := False; try //加入WorkBook对象 ExcelName:=ExcelApplication.WorkBooks.Add(ExtractFilePath(Application.ExeName)+'ReportModal\Manifest template.xls'); //加入Sheet对象 ExcelSheet := ExcelName.Worksheets[1]; except Screen.Cursor := crDefault; MSShow('没有找到报表模板,请保证Modal.xls在程序文件的ReportModal文件夹内!'); Abort; end; try J := 14; ADOQuery1.First; while not ADOQuery1.eof do begin ExcelSheet.cells(J,1) := ADOQuery1.RecNo; ExcelSheet.cells(J,2) := ADOQuery1.fieldbyname('OriHB').Value; ExcelSheet.cells(j,3) := Trim(ADOQuery1.FieldByName('Camer').AsString ); ExcelSheet.cells(j,4) := Trim(ADOQuery1.FieldByName('FlightNo').AsString); ExcelSheet.cells(j,5) := Trim(ADOQuery1.FieldByName('SurName').AsString); ExcelSheet.cells(j,6) := Trim(ADOQuery1.FieldByName('GivenName').AsString); ExcelSheet.cells(j,7) := Trim(ADOQuery1.FieldByName('PassportNo').AsString); ExcelSheet.cells(j,8) := FormatDateTime('yyyy/mm/dd', ADOQuery1.fieldbyname('DateOfBidh').AsDateTime ); //ExcelSheet.cells(j,8) :=ADOQuery1.fieldbyname('DateOfBidh').AsDateTime; ExcelSheet.cells(j,9) := DelHead(ADOQuery1.FieldByName('Nationality').AsString); ExcelSheet.cells(j,10) := Trim(ADOQuery1.FieldByName('Dest').AsString); ExcelSheet.cells(j,11) := 'HKG'; ExcelSheet.cells(j,12) := Trim(ADOQuery1.FieldByName('Luggage').AsString); ExcelSheet.cells(j,13) := Trim(ADOQuery1.FieldByName('LuggageTag').AsString) ; ExcelSheet.cells(j,14) := Trim(ADOQuery1.FieldByName('Weight').AsString); //if ADOQuery1.fieldbyname('Cons').AsBoolean then ExcelSheet.cells(j,15) := '√'; //if ADOQuery1.fieldbyname('E_Ticket').AsBoolean then ExcelSheet.cells(j,16) := '√'; {if ADOQuery1.FieldByName('Type').AsString='团体' then ExcelSheet.cells(j,17) := 'G' else ExcelSheet.cells(j,17) := 'P'; } //ExcelSheet.cells(j,17) := Trim(ADOQuery1.FieldByName('ReturnTax').AsString); inc(J); ADOQuery1.next; end; //ExcelSheet.cells(3,8) := 'ZYK'; //Terminal Port: ExcelSheet.range['H4'].NumberFormatLocal :='yyyy/mm/dd'; ExcelSheet.cells(4,8) := FormatDatetime('YYYY/MM/DD', DateTimePicker1.Date); //Departure Date ExcelSheet.cells(5,8) := Label4.Caption;//Trip No. ExcelSheet.cells(6,8) := FlatComboBox1.Text; //Departure Time ExcelSheet.cells(7,8) := ADOQuery1.RecordCount; //Total no. of Passenger ExcelSheet.cells(8,8) := CurrToStr(DBSumLuggage.SumCollection.Items[0].SumValue); //Total no. of Baggage: ExcelSheet.cells(9,8) := CurrToStr(DBSumV_BoradPass.SumCollection.Items[0].SumValue); //No. of upstream check-in pax ExcelSheet.cells(10,8) := CurrToStr(DBSumLuggage1.SumCollection.Items[0].SumValue); //No. of Through Checked (Tag Through) Baggage //设置单元格格式 Ranges:=ExcelSheet.range['H14:H' + inttostr(J -1)]; Ranges.NumberFormatLocal :='yyyy/mm/dd'; //左对齐2,居中对齐3,右对齐4 Ranges:=ExcelSheet.range['G14:G' + inttostr(J -1)]; Ranges.HorizontalAlignment :=2; //画线框 Ranges:=ExcelSheet.range['A13:P' + inttostr(J -1)]; Ranges.Borders.LineStyle := 1; except MsShow('在往EXCEL写入数据时出现错误,请重新发送Email!'); Screen.Cursor := crDefault; abort; end; try //保存数据 ExcelName.SaveAs(ReportName); except MSShow('无法保存数据'); Screen.Cursor := crDefault; abort; end; //全部释放variant变量 ,否则Excel进程无法关闭 try ExcelName.Close; ExcelApplication.Quit; ExcelSheet :=Unassigned ; ExcelName := Unassigned; ExcelApplication:=Unassigned; Ranges :=Unassigned; except end;