procedure LeadExcel(adbGrid: TDBGridEH); stdcall; export //导出数据
var
  j, k, col: integer;
  sheet, exele: variant;
begin
  with adbGrid.DataSource.DataSet do
    if not Active or IsEmpty then Exit;
  try
    exele := CreateOleObject('Excel.Application');
  except
    MessageBox(adbGrid.handle, PChar('你使用的系统没有装Excel!'), '提示', MB_OK + MB_ICONSTOP);
    exit;
  end;
  exele.Workbooks.Add;
  exele.Worksheets[1].Name := 'sheet1';
  sheet := exele.Workbooks[1].Worksheets['sheet1'];
  exele.WorkSheets[1].Activate;
  sheet.Cells[1, 5].Value := '查询数据表';
  col := 0;
  for j := 0 to adbGrid.fieldcount - 1 do //生成表头 dbegStat是TDBGrid
  begin
    if adbGrid.Columns[j].Visible then
    begin
      sheet.Cells[2, col + 1].Value := adbGrid.Columns[j].Title.Caption;
      inc(col);
    end;
  end;
  k := 3; //从Excel表的第5行开始添加数据。
  //开始导出数据
  with adbGrid.DataSource.DataSet do
  begin
    if not IsEmpty then
    begin
      first;
      while not Eof do
      begin
        col := 0;
        for j := 0 to adbGrid.Columns.Count - 1 do
          if (adbGrid.Columns[j].Visible) and (adbGrid.Columns[j].Field <> nil) then
          begin
            if adbGrid.Columns[j].Field.DataType in [ftString] then
              sheet.Cells[k, col + 1].Value := '''' + adbGrid.Columns[j].Field.AsString
            else
              sheet.Cells[k, col + 1].Value := adbGrid.Columns[j].Field.AsString;
            inc(col);
          end;
        inc(k);
        next;
      end;
    end;
    exele.visible := true;
  end;
end;这个过程,在程序中,不会出错,
可是放在DLL中,可以调用(用这种方法调用procedure LeadExcel(adbGrid: TDBGridEh); stdcall; external 'PPublicFunction.dll';)。但在关闭程序时,出错了。
出错信息为:
raised too many consecutive exceptions:'access violation at Ox00r21f3f':read of address Ox012058e8.Process Stopped.请问为什么,如何解决。谢谢!

解决方案 »

  1.   

    释放变量试试`~`    Sheet :=Unassigned ;
        exele:=Unassigned;
      

  2.   

    to ron_xin(星雨):还是不行啊!
      

  3.   

    发现一调用这条语句就会出错,
        sheet.Cells[k, col + 1].Value := adbGrid.Columns[j].Field.AsString;
    如如改成
        sheet.Cells[k, col + 1].Value := 'aaa';
    就不会了。
      

  4.   

    不能把对象传给dll?
    你试试把接口改了看看。
      

  5.   

    ExcelWksheet.Cells[j,i]:=DBGrid1.Columns.Items[i-1].Field.AsString;    
     或
    ExcelWksheet.Cells[j,i]:=DBGrid1.Fields[i-1].AsString
      

  6.   

    谢谢lixianxiang(活到老学到老) 
    但还不知为什么。