一个简单的问题,可我百思不得其解!(50分)

  • 主题发起人 主题发起人 little_feeling
  • 开始时间 开始时间
L

little_feeling

Unregistered / Unconfirmed
GUEST, unregistred user!
各位大哥大姐们,小妹在DELHPI中想调用EXCEL,可我不能打开一个已经存在的EXCEL文档,程序如下:
if not OpenDialog1.Execute then exit;
Filename := OpenDialog1.filename;

ExcelApplication1.Workbooks.Open(Filename);或
ExcelApplication1.Workbooks.Open(Filename,EmptyParam,EmptyParam,EmptyParam,
EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,
EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam);
这两种方法都不能打开EXCEL文档。我百思不得其解?????请各位高手请教!
 
www.borland.com.cn上有例子。
 
Filename := OpenDialog1.filename;
ExcelApplication1.Workbooks.Add(Filename, 0);
 
with ExcelApplication do begin
Connect;
ExcelApplication.Tag :=1;
Workbooks.Open(wFileName,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,0);
ExcelApplication.Visible[0] :=True;
end;
 
是不是ExcelApplication没有connect?
 
ExcelApplication1.Workbooks.Open(filename
, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, 1);
ExcelWorkBook1.ConnectTo(iUnknown(Excel_App.Workbooks[1]) as ExcelWorkBook);
ExcelWorkSheet1.ConnectTo(iUnknown(Excel_WorkBook.Worksheets[sheetname]) as ExcelWorkSheet);
ExcelApplication1.Visible[0] := true;
 
你是想用EXECL打开一个EXCEL文档,还是要在自己的EXCEL窗口里打开?
不管怎么样,你的机器必须装有EXCEL,因为它是OLE对象
 
有两种方法应用excel:
1.excelapplication、excelworkbook、excelworksheet控件
应用此种方法打开excel方法如下:
Begin
Try
ExcelApplication1.Connect;
Except
MessageDlg('Excel may not be installed',mtError, [mbOk], 0);
Abort;
End;
ExcelApplication1.Visible[0]:=TRUE;
ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]);
ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _Worksheet);
........//对excel表的操作

ExcelApplication1.Quit; //释放excel
End;
2.用createoleobject 方法
Var
ExcelApp,MyWorkBook:Variant;
filename: string;
Begin
Try
ExcelApp:=CreateOleObject('Excel.Application');
Except
MessageDlg('Excel may not be installed',mtError, [mbOk], 0);
Abort;
End;
filename:='d:/book.xls'; //要打开的excel文件
ExcelApp.workBooks.Open(filename);
MyworkBook1:=ExcelApp.workBooks[1];
......//对excel进行操作

MyWorkBook1.saveas(filename);
MyWorkBook1.close; //保存操作

ExcelApp.Quit;
ExcelApp:=Unassigned; //释放excel
End;
 
function Tfrm_main.Openexcel: Boolean;
var
Curexcel: string;
begin
Curexcel := GetRegString(HKEY_CLASSES_ROOT, '/excel.Application/CurVer', '', '');
if Curexcel = '' then
begin
Application.MessageBox('请先安装 excel 97 或 excel 2000!或 excel XP',
PChar(self.Caption), MB_OK + MB_ICONSTOP);
Result := False;
Exit;
end;

if (Curexcel <> 'excel.Application.8') and (Curexcel <> 'excel.Application.9') then
begin
{
Curexcel := GetRegString(HKEY_CLASSES_ROOT, '/excel.Application.8', '', '');
if Curexcel = '' then
begin
Curexcel := GetRegString(HKEY_CLASSES_ROOT, '/excel.Application.9', '', '');
if Curexcel <> '' then Curexcel := 'excel.Application.9';
end
else
begin
Curexcel := 'excel.Application.8';
end;
}
Curexcel := GetRegString(HKEY_CLASSES_ROOT, '/excel.Application.10', '', '');
if Curexcel <> '' then Curexcel := 'excel.Application.10';
if Curexcel = '' then
begin
Application.MessageBox('无法运行 excel 97 或 excel 2000,' + #13#10
+ '请检查 excel 97 或 excel 2000 是否已经正确安装。',
PChar(self.Caption), MB_OK + MB_ICONSTOP);
Result := False;
Exit;
end;
end;

try
Fexcel := CreateOleObject(Curexcel);
if VarIsEmpty(Fexcel) then
begin
Application.MessageBox('无法运行 excel 97 或 excel 2000,' + #13#10
+ '请检查 excel 97 或 excel 2000 是否已经正确安装。',
PChar(self.Caption), MB_OK + MB_ICONSTOP);
Result := False;
Exit;
end;
except
Application.MessageBox('无法运行 excel 97 或 excel 2000,' + #13#10
+ '请检查 excel 97 或 excel 2000 是否已经正确安装。',
PChar(self.Caption), MB_OK + MB_ICONSTOP);
Result := False;
Exit;
end;

try
begin
Fexcel.DisplayAlerts := False;

Fexcel.workbooks.Open(ExtractFilePath(Application.EXEName)+'Chinese Turnover Tax Macro Model (On 1997 Data).xls');
Fexcel.caption:='中国流转税宏观模型';
Fexcel.Visible := True;
Result := True;
end;
except
Application.MessageBox('分析失败,',
PChar(self.Caption), MB_OK + MB_ICONSTOP);
Result := False;
end;
end;
如何!
 
后退
顶部