首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

Delphi7+access 用Excelapplication控件导出DBGRID数据到EXCEL,施用"另存为"时出错

2012-08-17 
Delphi7+access 用Excelapplication控件导出DBGRID数据到EXCEL,使用另存为时出错使用另存为后,能保存表

Delphi7+access 用Excelapplication控件导出DBGRID数据到EXCEL,使用"另存为"时出错
使用另存为后,能保存表格,但是程序出现出错提示:‘类 Workbook 的 Saveas 方法无效’主要程序如下:
uses comobj;
{$R *.dfm}

Function DetailToExcel(xl_file:string;grid:Tdbgrid;T1:string='';T2:string='';T3:string=''):integer;
var xlApp:Variant;
  i,j:integer;
  irow,icol:integer;
  TitleLines:integer;
  Titles:array [1..3] of string;
begin
  if (not grid.DataSource.DataSet.Active) or (grid.DataSource.DataSet.RecordCount=0) then exit;

  Try
  xlapp:=createoleobject('Excel.application');
  xlapp.workbooks.Add(-4167);
  xlapp.visible:=true;
  Except
  showmessage('未安装Microsoft Excel !请安装!');
  exit
  end;


  //计算标题:
  TitleLines:=0;
  Titles[1]:=T1;
  Titles[2]:=T2;
  Titles[3]:=T3;

  if T1<>'' then TitleLines:=1;
  if T2<>'' then TitleLines:=2;
  if T3<>'' then TitleLines:=3;

  //填充列标题;
  irow:=grid.DataSource.DataSet.RecordCount;
  icol:=0;
  for i:=0 to grid.Columns.Count-1 do
  begin
  if grid.Columns[i].Visible then
  begin
  inc(icol);
  xlapp.cells[TitleLines+1,Icol]:=grid.Columns[i].Title.Caption;
  end;
  end;


  //填充标题;
  For i:=1 to TitleLines do
  begin
  xlapp.activesheet.cells[i,1]:=Titles[i];
  xlapp.activesheet.range['A'+IntToStr(i)+':'+chr(64+icol)+IntToStr(i)].select;
  xlapp.selection.HorizontalAlignment := xlCenter;
  xlapp.selection.VerticalAlignment := xlCenter;
  xlapp.selection.MergeCells := True;

  if i=1 then
  begin
  xlapp.selection.RowHeight:=26;
  xlapp.selection.Font.size:=18;
  end
  else
  begin
  xlapp.selection.RowHeight:=20;
  xlapp.selection.Font.size:=12;
  end;
  end;

  //设置表格线;
  xlapp.activesheet.range['A'+IntTostr(TitleLines+1)+':'+chr(64+icol)+Inttostr(irow+TitleLines+1)].select;
  xlapp.selection.Borders[xlEdgeBottom].linestyle:=xlContinuous;
  xlapp.selection.Borders[xlEdgetop].linestyle:=xlContinuous;
  xlapp.selection.Borders[xlEdgeright].linestyle:=xlContinuous;
  xlapp.selection.Borders[xlEdgeLeft].linestyle:=xlContinuous;
  xlapp.selection.Borders[xlInsideVertical].linestyle:=xlContinuous;
  xlapp.selection.Borders[xlInsideHorizontal].linestyle:=xlContinuous;


  //填充内容;
  grid.DataSource.DataSet.First;
  i:=TitleLines+2;
  while not grid.DataSource.DataSet.Eof do
  begin
  icol:=0;
  for j:=1 to grid.Columns.Count do
  begin
  if grid.Columns[j-1].Visible then
  begin
  inc(icol);
  xlapp.cells[i,icol]:=grid.Columns[j-1].Field.DisplayText;
  end;
  end;
  grid.DataSource.DataSet.Next;
  inc(i);
  end;


  For i:=1 to icol do
  begin
  xlapp.activesheet.Columns[i].select;
  xlapp.activesheet.Columns[i].EntireColumn.AutoFit;
  end;

  xlapp.activesheet.cells[1,1].select;

  if xl_File<>'' then
  xlapp.workbooks[1].SaveAs(xl_file);

  result:=0;
end;
4、最后,按钮单击事件如下代码:
procedure TDayDutyForm.Button3Click(Sender: TObject);
begin
  if DBGrid1.DataSource.DataSet.RecordCount=0 then
  ShowMessage('没有内容!')


  else
  DetailToExcel(ExtractFilePath(application.ExeName)+'DayDuty.xls',DBgrid1,'日统计');
end;

[解决办法]
保存前判断一下文件是否存在?存在的就先删除旧的。

if fileexists(xl_file) then
deletefile(xl_file);

热点排行