可以试试下面的代码,一个项目里用过:uses ComObj, ActiveXfunction My_CreateOleObject(const ClassName: string; out Ole_Handle: IDispatch): Boolean; var ClassID: TCLSID; l_Result: HResult; begin Result := False; l_Result := CLSIDFromProgID(PWideChar(WideString(ClassName)), ClassID); if (l_Result and $80000000) = 0 then begin l_Result := CoCreateInstance(ClassID, nil, CLSCTX_INPROC_SERVER or CLSCTX_LOCAL_SERVER, IDispatch, Ole_Handle); if (l_Result and $80000000) = 0 then Result := True; end; end; procedure TForm1.btn1Click(Sender: TObject); var XLApp: OleVariant; l_Excel_Handle: IDispatch; begin try XLApp := CreateOleObject('Excel.Application'); XLApp.WorkBooks.Open('c:\test.xls'); XLApp.visible := False; except on E: Exception do begin if not My_CreateOleObject('Excel.Application', l_Excel_Handle) then begin XLApp := Unassigned; raise exception.Create('启动Excel失败,可能没有安装Excel!'); Exit; end; end; end;还有不知道你为什么要用OLE的方式访问Excel,如果是想导入数据库的话,可以试试ADO的方式
win7是excel2007
在xp+delphi7程序引用是comobj,excel97,excel2000;
没有excel2007,也加不了
UAC 我已经右键选择 管理员运行 还试了兼容模式运行 都是一样我下载的是精简版 ,有些人说 要注册组件? 怎么注册呢?而且如果客户是精简版的话 不可能叫客户重装个office吧
他非要用精简版本的话,你就直接操作xlsx(ZIP文件,可以解压开,里面全是XML)文件。他不装都行。
var
ClassID: TCLSID;
l_Result: HResult;
begin
Result := False;
l_Result := CLSIDFromProgID(PWideChar(WideString(ClassName)), ClassID);
if (l_Result and $80000000) = 0 then
begin
l_Result := CoCreateInstance(ClassID, nil, CLSCTX_INPROC_SERVER or
CLSCTX_LOCAL_SERVER, IDispatch, Ole_Handle);
if (l_Result and $80000000) = 0 then
Result := True;
end;
end;
procedure TForm1.btn1Click(Sender: TObject);
var
XLApp: OleVariant;
l_Excel_Handle: IDispatch;
begin
try
XLApp := CreateOleObject('Excel.Application');
XLApp.WorkBooks.Open('c:\test.xls');
XLApp.visible := False;
except
on E: Exception do
begin
if not My_CreateOleObject('Excel.Application', l_Excel_Handle) then
begin
XLApp := Unassigned;
raise exception.Create('启动Excel失败,可能没有安装Excel!');
Exit;
end;
end;
end;还有不知道你为什么要用OLE的方式访问Excel,如果是想导入数据库的话,可以试试ADO的方式
参考
win7 不行啊 提示"启动Excel失败,可能没有安装Excel!"..........
win7 不行啊 提示"启动Excel失败,可能没有安装Excel!"
要注册哪些组件呢? 这精简版都有VBA
createOleObject('Excel.exe');
createOleObject('Excel.exe');
createOleObject('Excel.exe');