如何在Delphi中使用Word和Excel? [英] How work with Word and Excel in Delphi?

查看:226
本文介绍了如何在Delphi中使用Word和Excel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows 10 64位上使用Delphi X10和Word 2016 64位. 我一直使用ComObj. 例如:

I use Delphi X10 and Word 2016 64-bit on Windows 10 64-bit. I always used ComObj. for example:

uses ComObj;


procedure TForm1.RzBitBtn1Click(Sender: TObject);
var Excel: variant;  i, j: word;
begin
  Excel := CreateOleObject('Excel.Application');
  Excel.Workbooks.Open('file.xls');
  for i := 1 to 5 do
  for j := 1 to 5 do
  StringGrid1.Cells[j, i] := Excel.Sheets[1].Cells[i, j].Text;
end;

但是,我想为Excel方法使用自动完成功能.我读了应该导入类型库的内容,但是在系统中找不到类型库文件.

But, i want to use autocompleate for Excel methods. I read what i should import type library, but i cant find type library file in my system.

推荐答案

只要正确注册Excel anmd Word的类型库,这很容易做到.

This is quite easy to do, as long as the type libraries for Excel anmd Word are correctly registered.

转到Delphi安装下的OCX \ Servers文件夹,然后找到Word2010.Pas.将其添加到新的VCL项目中,并在主窗体的单元中使用它.在主表单上声明一个表单变量A: WordApplication.

Go to the OCX\Servers folder under your Delphi install and find Word2010.Pas. Add it to a new VCL project, and USE it in the main form's unit. Declare a form variable A: WordApplication on your main form.

完成后,您应该可以进入Form1的Form.Create,键入

With that done, you should be able to go to Form1's Form.Create, type

A.

和自动完成功能应该为您提供WordApplication的方法和属性的可能性.

and autocomplete should offer you the possibilities for WordApplication's methods and properties.

现在,打开Word2010.Pas并找到该部分

Now, open Word2010.Pas and find the section

// File generated on 2/20/2013 5:56:45 PM from Type Library described below.

// ************************************************************************  //
// Type Lib: c:\Program Files\Microsoft Office\Office14\msword.olb (1)
// LIBID: {00020905-0000-0000-C000-000000000046}

这会告诉您用于生成Word2010.Pas的类型库的名称和位置.

That tells you the name and location of the type library which was used to generate Word2010.Pas.

现在,除非您需要使用自2010版本以来已添加到Word/Excel的某些方法/属性,否则可能只需要Word2010.Pas和Excel2010.Pas.但是,您可以使用Delphi导入较新版本的类型库,并为其生成一个导入单元.确切的操作方式取决于您的Delphi版本,但对于Delphi Seattle.您只需转到Component |从IDE主菜单中导入组件,然后在弹出窗口中选择Import type library并按照向导的提示进行操作.

Now, unless you need to use some method/property that has been added to Word/Excel since the 2010 version, Word2010.Pas and Excel2010.Pas may be all you need. However, you can import the type library of a more recent version using Delphi and have it generate an import unit for it. Exactly how to do that depends on your Delphi version, but for Delphi Seattle. you simply go to Component | Import Component from the IDE main menu, then select Import type library in the pop-up and follow the wizard's prompts.

一旦生成了导入单元,显然您只需在项目中使用它即可,而不是MSWord2010.

Once you've generated the import unit, obviously you simply USE it in your project instead of MSWord2010.

顺便说一句,严格来说,没有"Delphi X10",最近的版本(自XE8起)是西雅图,柏林和东京.

Btw, strictly speaking, there is no "Delphi X10", recent versions (since XE8) have been Seattle, Berlin and Tokyo.

这篇关于如何在Delphi中使用Word和Excel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆