现在有个问题:
  我连接Excel是采用Ole的方式,但有时启动了一次 Excel后,再启动就死机了,这是怎么回事呢?
原代码如下:
ex:=CreateOleObject('Excel.Application');
  ex.WorkBooks.open(ExtractFileDir(Application.ExeName)+'\template\main.xls');
//从表处理
  dex:=CreateOleObject('Excel.Application');
  dex.WorkBooks.open(ExtractFileDir(Application.ExeName)+'\template\detail.xls');
  sql:='select * from main where rq='+'#'+datetostr(edate.Date)+'#'+' and bm_show='+''''+'yes'+'''';
  ado_main.Close;
  ado_main.SQL.Clear;
  ado_main.Connection:=ado_conn;
  ado_main.SQL.Add(sql);
  ado_main.Open;
  k:=2;
  i:=2;
  while not ado_main.Eof do
  begin
  //主表处理
    ex.Cells[k,1]:=ado_main.FieldList[9].AsString;
    ex.Cells[k,2]:=ado_main.FieldList[1].AsString;
    ex.Cells[k,3]:=ado_main.FieldList[2].AsString;
    ex.Cells[k,4]:=ado_main.FieldList[3].AsString;
    ex.Cells[k,5]:=ado_main.FieldList[4].AsString;
    ex.Cells[k,6]:=ado_main.FieldList[5].AsString;
    ex.Cells[k,7]:=ado_main.FieldList[6].AsString;
    ex.Cells[k,8]:=ado_main.FieldList[7].AsString;
    ex.Cells[k,9]:=ado_main.FieldList[8].AsString;
    ex.Cells[k,10]:=ado_main.FieldList[12].AsString;
    ex.Cells[k,11]:=ado_main.FieldList[11].AsString;
    ex.Cells[k,12]:=ado_main.FieldList[13].AsString;
    k:=k+1;
  //主表处理完毕 ,//从表处理开始
    ado_detail.Close;
    ado_detail.SQL.Clear;
    ado_detail.Connection:=ado_conn;
    ado_detail.SQL.Add('select * from detail where bmd_bh='+''''+ado_main.FieldList[9].AsString+'''');
    ado_detail.Open;
    while not ado_detail.Eof do
    begin
      dex.Cells[i,1]:=ado_main.FieldList[9].AsString;
      dex.Cells[i,2]:=ado_detail.FieldList[2].AsString;
      dex.Cells[i,3]:=ado_detail.FieldList[3].AsString;
      dex.Cells[i,4]:=ado_detail.FieldList[4].AsString;
      dex.Cells[i,5]:=ado_detail.FieldList[5].AsString;
      dex.Cells[i,6]:=ado_detail.FieldList[6].AsString;
      dex.Cells[i,7]:=ado_detail.FieldList[7].AsString;
      i:=i+1;
      ado_detail.Next;
    end;
    ado_detail.Close;
    //从表处理完成
    ado_main.Next;
  end;
  ado_main.Close;
  //保存从表
    dex.DisplayAlerts:=False;
    dex.ActiveWorkBook.Saved:=false;
    dex.WorkBooks[1].SaveAs(ExtractFileDir(Application.ExeName)+'\bmxls\sd'+datetostr(edate.Date)+'.xls');
    dex.ActiveWorkBook.Close ;
    dex.Quit;
    dex:=unassigned;
  //保存主表
    ex.DisplayAlerts:=False;
    ex.ActiveWorkBook.Saved:=false;
    ex.WorkBooks[1].SaveAs(ExtractFileDir(Application.ExeName)+'\bmxls\sm'+datetostr(edate.Date)+'.xls');
    ex.ActiveWorkBook.Close ;
    ex.Quit;
    ex:=unassigned;
    Application.MessageBox('系统提示:导出数据成功','系统提示',MB_OK);
    application.ProcessMessages;
    frmshow.Close;