有关保存EXCEL文件的问题
当只打开一个EXCEL文件时可以保存,但是当打开多个EXCEL文件保存则老是提示“要保存的文件与打开的文件重名”而导致保存失败。(可以确认打开的是不同的文件)。有什么办法可以打开一个EXCEL文件保存后,关闭该EXCEL文件,再打开另一个EXCEL文件,保存后再关闭。我的代码如下:
procedure TForm1.open_excel;
var
filepath:string;
begin
//打开Excel文档模板
filepath:=extractfilepath(application.ExeName);
excelapp.Disconnect;
excelapp.Connect;
//如果目标Excel文件已经存在,则打开现存文件
if fileexists(filepath+'report\'+excel_name+'.xls') then
excelapp.Workbooks.Open(filepath+'report\200310\'+excel_name+'.xls')
else
excelapp.Workbooks.Open(filepath+'Model\'+excel_name+'.xls');
excelbook.ConnectTo(excelapp.Workbooks[1] as _workbook);
excelsheet.ConnectTo(excelbook.Worksheets[1] as _worksheet);
excelsheet.Activate;
excelapp.Visible[1]:=true;
end;
procedure TForm1.save_excel;
var
filepath:string;
begin
//将写入新数据的Excel文件另存,如果目标文件已存在,则仅做保存操作
filepath:=extractfilepath(application.ExeName);
if fileexists(filepath+'report\200310\'+excel_name+'.xls') then
excelbook.Save;
else
begin
excelsheet.SaveAs(filepath+'report\200310\'+excel_name+'.xls');
excelsheet.Disconnect;
end;
end;