如何在DELPHI中调用EXCEL中的函数或宏
1个回答
展开全部
代码:
procedure TForm1.Button1Click(Sender: TObject);
var
MyWorkBook: _WorkBook;
FileName: string;
begin
ExcelApplication1.Connect;
ExcelApplication1.Visible[0] := True;
ExcelApplication1.Caption := ‘应用程序调用 Microsoft Excel‘;
FileName:=‘e:\temp\book1.xls‘;
//由于我又时候运行需要一个0,有所时候2个,
//如果报参数不匹配的错误,可以删掉或者添加后面0 的个数
MyWorkBook:= ExcelApplication1.Workbooks.Open(FileName, EmptyParam,
EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, 0,0,0);
ExcelWorkbook1.ConnectTo(MyWorkBook);
ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _WorkSheet);
//调用工作簿中已经存在的名称为 My 的宏
ExcelApplication1.Run(‘My‘);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
MyWorkBook: _WorkBook;
FileName: string;
begin
ExcelApplication1.Connect;
ExcelApplication1.Visible[0] := True;
ExcelApplication1.Caption := ‘应用程序调用 Microsoft Excel‘;
FileName:=‘e:\temp\book1.xls‘;
//由于我又时候运行需要一个0,有所时候2个,
//如果报参数不匹配的错误,可以删掉或者添加后面0 的个数
MyWorkBook:= ExcelApplication1.Workbooks.Open(FileName, EmptyParam,
EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, 0,0,0);
ExcelWorkbook1.ConnectTo(MyWorkBook);
ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _WorkSheet);
//调用工作簿中已经存在的名称为 My 的宏
ExcelApplication1.Run(‘My‘);
end;
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询