如何检查excel是否已经1.安装 2.启动?在线等待(10分)

  • 主题发起人 主题发起人 jcc0728
  • 开始时间 开始时间
J

jcc0728

Unregistered / Unconfirmed
GUEST, unregistred user!
我用的d6,如果先启动EXCEL,则下列代码会强行关闭EXCEL(不提示保存)
求教如何检查EXCEL是否已经安装与启动?
procedure SetDataSetToExcel(aDataSet :TDataSet ) ;
var
i,row:integer;
ExcelApplication1: TExcelApplication;
ExcelWorksheet1: TExcelWorksheet;
ExcelWorkbook1: TExcelWorkbook;
SaveDialog1: TSaveDialog;

begin
//判断是否安装excel,必须保证安装

//判断是否有excel打开,必须保证关闭

//创建所需组件
try

ExcelApplication1 := TExcelApplication.Create(Application);
ExcelWorksheet1:= TExcelWorksheet.Create(Application);
ExcelWorkbook1:= TExcelWorkbook.Create(Application);
SaveDialog1:=TSaveDialog.Create(Application);

//修改SaveDialog1的扩展名,只能为xls
SaveDialog1.Filter := 'Microsoft Excel 工作簿(*.xls)|*.xls';
SaveDialog1.DefaultExt :='xls' ;

if SaveDialog1.Execute then
begin
Screen.Cursor:=crHourGlass;
ExcelApplication1.Connect;
ExcelApplication1.Workbooks.Add(Null,0);
ExcelWorkBook1.ConnectTo(ExcelApplication1.Workbooks[1]);
ExcelWorkSheet1.ConnectTo(ExcelWorkBook1.Sheets[1] as _WorkSheet);
if not aDataSet.Active then
begin
aDataSet.Open;
end;
for i:=0 to DM.CDS_Main.Fields.Count-1 do
ExcelWOrkSheet1.Cells.Item[1,i+1]:=aDataSet.Fields.FieldName;
row:=2;
while not aDataSet.Eof do
begin
for i:=0 to aDataSet.Fields.Count-1 do
begin
ExcelWOrkSheet1.Cells.Item[row,i+1]:=aDataSet.Fields.AsString;
end;
row:=row+1;
aDataSet.Next;
end;
ExcelWorkBook1.SaveCopyAs(SaveDialog1.FileName);
ExcelWOrkBook1.Close(false);
ExcelApplication1.Disconnect;
ExcelApplication1.Quit;
Screen.Cursor:=crDefault;
Application.MessageBox('Export successfully!','Information',0);
end;
finally
ExcelApplication1.Free ;
ExcelWorksheet1.Free ;
ExcelWorkbook1.Free ;
SaveDialog1.Free ;
end;


end;
 
try
ExcelApplication1 := TExcelApplication.Create(Application);
ExcelWorksheet1 := TExcelWorksheet.Create(Application);
ExcelWorkbook1 := TExcelWorkbook.Create(Application);
ExcelApplication1.Connect;
except
Application.Messagebox('Excel 没有安装!','Hello', MB_ICONERROR + mb_Ok);
Abort;
end;
 
楼上答案正确
 
接受答案了.
 
后退
顶部