我在做从EXCEL中导入数据的时候用了以下三个控件:
    ExcelApplication1: TExcelApplication;
    ExcelWorksheet1: TExcelWorksheet;
    ExcelWorkbook1: TExcelWorkbook;
但是,我用以下语句,在系统进统里仍有EXCEL.EXE这个进程,
    ExcelWorksheet1.Disconnect ;
    ExcelWorkbook1.Disconnect ;
    ExcelWorkbook1.Close;
    ExcelApplication1.Disconnect;
    ExcelApplication1.Quit;
    EParam :=Unassigned;
    DocuType :=Unassigned;我的代码如下:procedure TFrmGroupManage.FlatButton5Click(Sender: TObject);
var
  i,j,DoubleCount,Ord_Qty : integer;
  EParam,DocuType : OleVariant;
  wkbk : _WorkBook;
  sqlstr :string;
  ExeHandle:Thandle;
begin
  opendialog1.InitialDir:=ExtractFileDir(paramstr(0));
  //文件打开的初始路径
  if opendialog1.Execute then
  begin  Try
    ExcelApplication1.Connect;
  Except
    msshow('Excel文件打开失败!');
    Exit;
  End;  ExcelApplication1.Visible[0]:=False;
  ExcelApplication1.Caption:='Excel数据文件';  EParam:=EmptyParam;
  DocuType:=0;
  try
    wkBk:=ExcelApplication1.Workbooks.Add(EParam, DocuType);    wkBk:=ExcelApplication1.WorkBooks.Open(opendialog1.FileName,EmptyParam,EmptyParam,
      EmptyParam,EmptyParam,EmptyParam,EmptyParam,
      EmptyParam,EmptyParam,
      EmptyParam,EmptyParam,DocuType,EmptyParam,DocuType);    ExcelWorkbook1.ConnectTo(ExcelApplication1.ActiveWorkbook);//ExcelWorkbook1与Eexcelapplication1建立连接
    ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _Worksheet);//Excelworksheet1与Excelworkbook1建立连接  except
    begin
      ExcelWorkbook1.Close;     //出现异常情况时关闭
      ExcelApplication1.Disconnect;
      ExcelApplication1.Quit;
      msshow('导入EXCEL数据表格失败!');
      exit;
    end;
  end;
  //开始从EXCEL中取数,放到stringgrid中,取完数后关闭EXCEL
  try
    for i:=11 to 50 do
    begin
      if trim(excelworksheet1.cells.item[i,1])<>'' then
        begin
          GroupD.Insert;
          GroupD.FieldByName('Item_No').AsInteger :=GroupD.RecordCount +1;
          GroupD.fieldbyname('Single_No').AsString  := GroupM.fieldbyname('Single_No').AsString;
          GroupD.FieldByName('ShipTicketNo').asstring :=GroupD.fieldbyname('Single_No').AsString+'-'+inttostr(GroupD.RecordCount+1);
          GroupD.FieldbyName('UserName').asstring :=UserName ;
          GroupD.FieldByName('Luggage').AsInteger :=0;
          GroupD.FieldByName('Weight').AsInteger :=0;
          GroupD.FieldByName('IsThrough').AsBoolean :=FlatCheckBox4.Checked  ;
          GroupD.FieldByName('ischk').AsBoolean :=False ;
          GroupD.FieldByName('IsLeave').AsBoolean :=False ;
          GroupD.FieldByName('E_Ticket').AsBoolean :=False ;
          GroupD.FieldByName('Cons').AsBoolean :=False ;
          GroupD.fieldbyname('OriDate').AsString  := GroupM.fieldbyname('OriDate').AsString;
          GroupD.fieldbyname('OriHB').AsString  := GroupM.fieldbyname('OriHB').AsString;
          GroupD.fieldbyname('Camer').AsString  := GroupM.fieldbyname('Camer').AsString;
          GroupD.fieldbyname('FlightNo').AsString  := GroupM.fieldbyname('FlightNo').AsString;
          GroupD.fieldbyname('Origin').AsString  := GroupM.fieldbyname('Origin').AsString;
          GroupD.fieldbyname('Dest').AsString  := GroupM.fieldbyname('Dest').AsString;
          GroupD.fieldbyname('Type').AsString  := GroupM.fieldbyname('Type').AsString;
          GroupD.fieldbyname('Ticket').AsString  := GroupM.fieldbyname('Ticket').AsString;
          GroupD.fieldbyname('U_Price').AsString  := GroupM.fieldbyname('U_Price').AsString;          GroupD.fieldbyname('SurName').value:=ExcelWorksheet1.Cells.Item[i,5];
          GroupD.FieldByName('GivenName').value :=ExcelWorksheet1.Cells.Item[i,6];
          GroupD.FieldByName('PassportNo').value :=ExcelWorksheet1.Cells.Item[i,7];
          GroupD.fieldbyname('DateOfBidh').value:=ExcelWorksheet1.Cells.Item[i,8];
          GroupD.FieldByName('Nationality').value :=ExcelWorksheet1.Cells.Item[i,9];
          GroupD.Post;
        end
    end;
  finally
    msshow('数据导入完毕!');
  end;  try
    ExcelWorksheet1.Disconnect ;
    ExcelWorkbook1.Disconnect ;
    ExcelWorkbook1.Close;
    ExcelApplication1.Disconnect;
    ExcelApplication1.Quit;
    EParam :=Unassigned;
    DocuType :=Unassigned;
  except  end;end;

解决方案 »

  1.   

    建議用CreatOleObject 方式來操作Excel直接用Delphi的控件有很多問題的.你可以在Csdn搜索下unit uExcel;interfaceuses SysUtils, DB, ComObj, Variants, Excel2000, DBGrids, Forms, Controls,
      FGauge;const
      C_Default_Font_Size = 10;
      C_Default_Font_Name = 'Times New Roman';
      C_Default_Row_Height = 20;
      C_Default_Margin = 40;type
      TxlBorder = set of (xlbTop, xlbBottom, xlbLeft, xlbRight, xlbBox,
        xlbInsideVertical, xlbInsideHorizontal);  TExcel = class
      private
        FExcel: Variant;
        FRow: integer;
        FCol: integer;
        FDBGrid: TDBGrid;
        FSaveAs: string;
        FFrmGauge: TFrmGauge;    function CNoToCol(aCol_n: integer): string;
        procedure CreateExcelObj(aTemplateFileName: string);
        procedure ReleaseExcelObj;
        procedure SetCellsFormat(aFontName: string; aFontSize, aRowHeight: integer);
        procedure ExportFromDBGrid;
        procedure SetDBGrid(const Value: TDBGrid);
      public
        constructor Create(aTemplateFileName: string = ''; aSaveAs: string = '');
        destructor Destroy; override;
        function NoToRange(aR1, aC1: Word; aR2: Word = 0; aC2: Word = 0): string;
        property Excel: Variant read FExcel;
        property Row: integer read FRow write FRow;
        property Col: integer read FCol write FCol;
        property DBGrid: TDBGrid read FDBGrid write SetDBGrid;
        procedure SetValue(aRow, aCol: integer; aValue: variant); overload;
        procedure SetValue(aValue: variant); overload;
        procedure SetPageOrientation(aOrientation: Cardinal = xlPortrait);
        procedure SetColFormat(aCol: integer; aField: TField);
        procedure SetFontSize(aFontSize: integer);
        procedure MergeCells(aStartRow, aStartCol, aEndRow, aEndCol: integer);
        procedure SetHAlignment(aAlignment: Cardinal);
        procedure RangeAutoFit(aStartRow, aStartCol, aEndRow, aEndCol: integer);
        procedure SetSelection(aStartRow, aStartCol, aEndRow, aEndCol: integer);
        procedure SetRowsHeight(aStartRow, aEndRow, aRowHeight: integer);
        procedure DrawBox(xlb: TxlBorder);
        procedure SetSheetName(aSheetName: string);
        procedure SetTitle(aTitle: string);
      end;
      

  2.   

    implementationuses ToolLib;{ TExcel }procedure TExcel.DrawBox(xlb: TxlBorder);
    begin
      if (xlbLeft in xlb) or (xlbBox in xlb) then
      begin
        FExcel.Selection.Borders[xlEdgeLeft].LineStyle := xlContinuous;
        FExcel.Selection.Borders[xlEdgeLeft].Weight := xlThin;
      end; //if
      if (xlbTop in xlb) or (xlbBox in xlb) then
      begin
        FExcel.Selection.Borders[xlEdgeTop].LineStyle := xlContinuous;
        FExcel.Selection.Borders[xlEdgeTop].Weight := xlThin;
      end; //if
      if (xlbBottom in xlb) or (xlbBox in xlb) then
      begin
        FExcel.Selection.Borders[xlEdgeBottom].LineStyle := xlContinuous;
        FExcel.Selection.Borders[xlEdgeBottom].Weight := xlThin;
      end; //if
      if (xlbRight in xlb) or (xlbBox in xlb) then
      begin
        FExcel.Selection.Borders[xlEdgeRight].LineStyle := xlContinuous;
        FExcel.Selection.Borders[xlEdgeRight].Weight := xlThin;
      end; //if
      if (xlbInsideVertical in xlb) or (xlbBox in xlb) then
      begin
        FExcel.Selection.Borders[xlInsideVertical].LineStyle := xlContinuous;
        FExcel.Selection.Borders[xlInsideVertical].Weight := xlThin;
      end;
      if (xlbInsideHorizontal in xlb) or (xlbBox in xlb) then
      begin
        FExcel.Selection.Borders[xlInsideHorizontal].LineStyle := xlContinuous;
        FExcel.Selection.Borders[xlInsideHorizontal].Weight := xlThin;
      end;
    end;function TExcel.CNoToCol(aCol_n: integer): string;
    begin
      if aCol_n > 256 then
        raise Exception.Create('Invalid column number (' + IntToStr(aCol_n)
          + '), the maximum column number is 256.');
      if aCol_n > 26 then
      begin
        Result := CHR((aCol_n div 26) + ORD('A') - 1);
        aCol_n := (aCol_n mod 26);
        if aCol_n = 0 then
        begin
          Result := CHR(ORD(Result[1]) - 1) + 'Z';
          Exit;
        end; //if
      end
      else
        Result := '';
      Result := Result + CHR(aCol_n + ORD('A') - 1);
    end;constructor TExcel.Create(aTemplateFileName, aSaveAs: string);
    begin
      FFrmGauge := TFrmGauge.Execute('正在開啟Excel, 請稍候...');
      FSaveAs := aSaveAs;
      CreateExcelObj(aTemplateFileName);
    end;procedure TExcel.CreateExcelObj(aTemplateFileName: string);
    var
      lErrMsg: string;
    begin
      try
        lErrMsg := '初始化Excel失敗可能沒有安裝Excel或其它錯誤﹐請重試﹗';
        FExcel := CreateOLEObject('Excel.Application');
        FExcel.Application.DisplayAlerts := False;
        FExcel.Visible := False;
        if (aTemplateFileName <> '')
          and FileExists(aTemplateFileName) then
        begin
          FExcel.WorkBooks.Open(aTemplateFileName);
          FExcel.ActiveSheet.Unprotect(Password := '123');
        end else
        begin
          FExcel.WorkBooks.Add;
          SetCellsFormat(C_Default_Font_Name, C_Default_Font_Size, C_Default_Row_Height);
        end;
        FExcel.ActiveWindow.DisplayGridlines := True;
      except
        raise Exception.Create(lErrMsg);
        if VarIsEmpty(FExcel) then
          FExcel := unAssigned;
      end;
    end;destructor TExcel.Destroy;
    begin
      ReleaseExcelObj;
      FFrmGauge.Free;
      inherited;
    end;procedure TExcel.MergeCells(aStartRow, aStartCol, aEndRow,
      aEndCol: integer);
    begin
      FExcel.Range[NotoRange(aStartRow, aStartCol, aEndRow, aEndCol)].Select;
      FExcel.Selection.Merge;
    end;
      

  3.   

    function TExcel.NoToRange(aR1, aC1, aR2, aC2: Word): string;
    begin
      if (aR2 = 0) or (aC2 = 0) then
        Result := CNoToCol(aC1) + IntToStr(aR1)
      else
        Result := CNoToCol(aC1) + IntToStr(aR1) + ':' + CNoToCol(aC2) + IntToStr(aR2);
    end;procedure TExcel.ReleaseExcelObj;
    begin
      if not VarIsEmpty(FExcel) then
      begin
        FExcel.Cells[1, 1].Select;
        //FExcel.ActiveSheet.Protect(Password := '123');
        if FSaveAs <> '' then
          FExcel.ActiveWorkbook.SaveAs(FileName := FSaveAs);
        FExcel.Application.DisplayAlerts := True;
        FExcel.Visible := True;
        FExcel := UnAssigned;
      end;
    end;procedure TExcel.SetCellsFormat(aFontName: string; aFontSize,
      aRowHeight: integer);
    begin
      FExcel.Cells.Select;
      FExcel.Selection.Font.Name := aFontName;
      FExcel.Selection.Font.Size := aFontSize;
      //FExcel.Selection.WrapText := True;
      FExcel.Selection.HorizontalAlignment := xlLeft;
      FExcel.Selection.VerticalAlignment := xlCenter;
      FExcel.Selection.Rows.RowHeight := aRowHeight;  FExcel.ActiveSheet.PageSetup.Orientation := xlPortrait;
      FExcel.ActiveSheet.PageSetup.CenterHorizontally := True;
      FExcel.ActiveSheet.PageSetup.LeftMargin := C_Default_Margin;
      FExcel.ActiveSheet.PageSetup.RightMargin := C_Default_Margin;
      FExcel.ActiveSheet.PageSetup.TopMargin := C_Default_Margin;
      FExcel.ActiveSheet.PageSetup.BottomMargin := C_Default_Margin;
    end;procedure TExcel.SetColFormat(aCol: integer; aField: TField);
    begin
      if UpperCase(aField.FieldName) = 'SEQUENCE' then
      begin
        FExcel.Columns[aCol].NumberFormatLocal := TFloatField(aField).DisplayFormat;
        FExcel.Columns[aCol].HorizontalAlignment := xlCenter;
        exit;
      end else if aField.DataType in [ftinteger, ftFloat, ftBcd, ftCurrency] then
      begin
        FExcel.Columns[aCol].NumberFormatLocal := TFloatField(aField).DisplayFormat;
        FExcel.Columns[aCol].HorizontalAlignment := xlRight;
      end
      else if aField.DataType in [ftDate, ftDatetime] then
        FExcel.Columns[aCol].NumberFormatLocal := TFloatField(aField).DisplayFormat
      else if aField.DataType in [ftString, ftWideString] then
        FExcel.Columns[aCol].NumberFormatLocal := '@';
    end;procedure TExcel.SetValue(aRow, aCol: integer; aValue: variant);
    begin
      FExcel.Cells[aRow, aCol].Value := aValue;
    end;procedure TExcel.SetHAlignment(aAlignment: Cardinal);
    begin
      FExcel.Selection.HorizontalAlignment := aAlignment;
    end;procedure TExcel.SetValue(aValue: variant);
    begin
      FExcel.Selection := aValue;
    end;procedure TExcel.RangeAutoFit(aStartRow, aStartCol, aEndRow,
      aEndCol: integer);
    begin
      FExcel.Range[NotoRange(aStartRow, aStartCol, aEndRow, aEndCol)].Select;
      FExcel.Selection.Columns.Autofit;
    end;procedure TExcel.SetFontSize(aFontSize: integer);
    begin
      FExcel.Selection.Font.Size := aFontSize;
    end;procedure TExcel.SetPageOrientation(aOrientation: Cardinal);
    begin
      FExcel.ActiveSheet.PageSetup.Orientation := aOrientation;
      FExcel.ActiveSheet.PageSetup.CenterHorizontally := True
    end;procedure TExcel.SetRowsHeight(aStartRow, aEndRow, aRowHeight: integer);
    begin
      FExcel.Range[NotoRange(aStartRow, 1, aEndRow, 1)].Select;
      FExcel.Selection.Rows.RowHeight := aRowHeight;
    end;procedure TExcel.SetSelection(aStartRow, aStartCol, aEndRow,
      aEndCol: integer);
    begin
      FExcel.Range[NotoRange(aStartRow, aStartCol, aEndRow, aEndCol)].Select;
    end;procedure TExcel.ExportFromDBGrid;
    var
      i: integer;
      lValue: string;
    begin
      FFrmGauge._Hint := '正在輸出標師欄...';
      for i := 0 to FDBGrid.Columns.Count - 1 do
        SetValue(FRow, i + FCol, FDBGrid.Columns[i].Title.Caption);  FFrmGauge.Init(FDBGrid.DataSource.DataSet.RecordCount);
      FFrmGauge._Hint := '正在輸出數據...';
      inc(FRow);
      FDBGrid.DataSource.DataSet.DisableControls;
      with FDBGrid.DataSource.DataSet do
      try
        First;
        while not Eof do
        begin
          FFrmGauge.Progress := FFrmGauge.Progress + 1;
          for i := 0 to FDBGrid.Columns.Count - 1 do
          begin
            if not Assigned(FindField(FDBGrid.Columns[i].FieldName)) then
              continue;
            if FieldByName(FDBGrid.Columns[i].FieldName).DataType = ftFloat then
              if TFloatField(FieldByName(FDBGrid.Columns[i].FieldName)).DisplayFormat <> '' then
                lValue := FormatFloat(TFloatField(FieldByName(FDBGrid.Columns[i].FieldName)).DisplayFormat
                  , FieldByName(FDBGrid.Columns[i].FieldName).AsFloat)
              else
                //lValue := FormatFloat(DF_2D, FieldByName(FDBGrid.Columns[i].FieldName).AsFloat)
                lValue := FieldByName(FDBGrid.Columns[i].FieldName).AsString
            else
              lValue := '''' + FieldByName(FDBGrid.Columns[i].FieldName).AsString;
            SetValue(FRow, i + FCol, lValue);
          end;
          Next;
          inc(FRow);
        end;
      finally
        FDBGrid.DataSource.DataSet.EnableControls;
      end;
    end;procedure TExcel.SetDBGrid(const Value: TDBGrid);
    begin
      FDBGrid := Value;
      ExportFromDBGrid;
    end;procedure TExcel.SetSheetName(aSheetName: string);
    begin
      FFrmGauge._Hint := '正在設置表名...';
      FExcel.ActiveSheet.Name := aSheetName;
    end;procedure TExcel.SetTitle(aTitle: string);
    begin
      FFrmGauge._Hint := '正在設置標題...';
      SetValue(FRow, FCol, aTitle);
      Inc(FRow);
    end;end.
      

  4.   

    http://item.365key.com/1235/1235677.shtm调用Excel组件不能结束进程的解决方法 解决方法是一样的
      

  5.   

    kill进程的方法,我也知道,我只是想知道控件的问题不能解决吗?
      

  6.   

    ExcelApp:=unassigned;
    就可以结束Excel.exe进程了