Delphi里如何保存并关闭excel

来源:互联网 发布:中南远程网络教育平台 编辑:程序博客网 时间:2024/05/12 10:02
procedure TForm1.Button6Click(Sender: TObject);
var
   xlstest, thesheet:Variant;
begin
  try
    xlstest:=CreateOleObject('Excel.Application');
  except
    Exit;
  end;
  xlstest.WorkBooks.Open(ExtractFilePath(Paramstr(0))+'tt.xls');
  thesheet:= xlstest.Workbooks[1].Worksheets['sheet1'];
  thesheet.cells.item[2,2]:= 'aa'//memo1.text;
  xlstest.DisplayAlerts:=false;
  xlstest.ActiveWorkBook.SaveAs(ExtractFilePath(Paramstr(0))+'tt.xls');
  xlstest.Quit;

end;


procedure TForm1.Button6Click(Sender: TObject);
var
   xlstest, thesheet:Variant;
begin
  try
    xlstest:=CreateOleObject('Excel.Application');
  except
    Exit;
  end;
  xlstest.WorkBooks.Open(ExtractFilePath(Paramstr(0))+'tt.xls');
  thesheet:= xlstest.Workbooks[1].Worksheets['sheet1'];
  thesheet.cells.item[2,2]:= 'aa'//memo1.text;
  xlstest.DisplayAlerts:=false;
  xlstest.ActiveWorkBook.SaveAs(ExtractFilePath(Paramstr(0))+'tt.xls');
  xlstest.Quit;
end;



if self.OpenDialog1.Execute then
    filename:=self.OpenDialog1.FileName;
  if filename='' then
    Exit;
  //打开Excel报表
  form1.teminate_excel;
  try
    Self.ExcelApplication1:=TExcelApplication.Create(Self);
    Self.ExcelApplication1.Connect;
  except
    messagebox(application.Handle,'无法生成Excel报表,请确定安装了Excel后重试','信息',mb_ok or mb_iconinformation);
    exit;
  end;
  Self.ExcelApplication1.Visible[0]:=False;
  Self.ExcelApplication1.DisplayAlerts[0]:=False;//这句屏蔽提示对话框,可以实现保存时不出现是否覆盖的提示
  self.ExcelApplication1.Workbooks.Open(filename,EmptyParam,
                                         EmptyParam,EmptyParam,EmptyParam,EmptyParam,
                                         EmptyParam,EmptyParam,EmptyParam,EmptyParam,
                                         EmptyParam,EmptyParam,EmptyParam,0);
  self.ExcelWorkbook1.ConnectTo(Self.ExcelApplication1.Workbooks[1]);
  self.ExcelWorksheet1:=TExcelWorkSheet.Create(self);
  self.ExcelWorksheet1.ConnectTo(Self.ExcelWorkbook1.Worksheets[1as _worksheet);
 
  self.ExcelWorksheet1.SaveAs('f:\统计报表.xls');//这句实现保存



0 0