VS2019 C++调用EXCEL COM接口

Excel是日常办公中的关键工具,在excel中搞开发通常使用宏,也就是VBA。除此之外,还可以使用C#。去年,我学过几天的C#,总体而言C#与VBA在操作方面很相似,也很容易掌握。但是,C#需要依赖.net framework运行库,.net库需要额外安装。如果能够通过C++调用COM接口来操作excel的话,就可以避免安装额外的运行库。

不幸的是,网上搜索C++调用excel com百度给出的搜索结果:一,大部分是VS老版本的导入方法,而在VS2019中我安装MFC组件后,右键“类向导”只有“MFC类”这一个选型。二,都在讲MFC项目中怎么加载excel,可是我不会也不想做MFC,就想在命令行窗口中实现。三,虽然有人提到了如何调用,但是模棱两可,看不懂。

经过我三番五次的试错,现在来跟大家分享下怎么导入EXCEL COM库。

一、了解#import:

VS2019已经废弃了从类型库中导入类这个功能,而且除了MFC项目也没有多少可用性。在一般情况下,导入COM接口,可以直接使用#import。

关于#import的用法可以自己百度。

二、关于.tli/.tlh文件

使用#import “…./EXCEL.EXE”类似的语句,会在#import有错误提示——“找不到…excel.tlh文件”。相关的.tlh和.tli文件会在第一次编译时生成在debug或release文件夹下,当然要注意了它们不会在VS“解决方案管理器”下出现,你可以去项目文件夹下找到他。

三、关于导入excel的问题

如果单纯import了excel.exe,会一直提示找不到.tlh文件,如果debug编译的话,你会发现它确实在debug文件夹下生成了(不过报错的路径并不是debug,如果你尝试把tli和tlh文件放在报错信息所指的目录下,它仍旧会报错)。

不要怀疑,这并不是你的过错。要调用excel还需要在这之前导入两个库。而这些代码便是百度结果中一些CSDN博客提到的:

#import "C:\Program Files (x86)\Common Files\microsoft shared\OFFICE15\MSO.DLL"\
    rename("RGB","MsoRGB") \
    rename("SearchPath","MsoSearchPath")
#import "C:\Program Files (x86)\Common Files\microsoft shared\VBA\VBA6\VBE6EXT.OLB"
#import "C:\Program Files\Microsoft Office\Office15\EXCEL.EXE"\
    rename( "DialogBox", "ExcelDialogBox" ) \
    rename( "RGB", "ExcelRGB" ) \
    rename( "CopyFile", "ExcelCopyFile" ) \
    rename( "ReplaceText", "ExcelReplaceText" ) \
    exclude( "IFont", "IPicture" ) no_dual_interfaces

四、OLEVIEWER工具

第三节我们提到了,关于import单独调用excel会有报错信息的问题,原因在于excel有两个需要提前import调用的依赖项。

下面,我要介绍下如何确定excel有两个依赖项。这需要用到oleviewer.exe,一个COM接口查看工具。这个工具在安装完vs2019 C++组件(主要是windows sdk)后,可以在如下路径中找到:

C:\Program Files (x86)\Windows Kits\10\bin\10.0.XXXXX.0\x86

打开工具时右击以管理员身份运行。

当我们想了解excel中的信息时,找到菜单栏File列,选择View Typelib。这时,会弹出文件浏览窗口,找到你office安装路径下的excel.exe打开。

我们会看见一个新的窗口,其中有一段:

// Generated .IDL file (by the OLE/COM Object Viewer)
// 
// typelib filename: EXCEL.EXE

[
  uuid(00020813-0000-0000-C000-000000000046),
  version(1.8),
  helpstring("Microsoft Excel 15.0 Object Library"),
  helpfile("VBAXL10.CHM"),
  helpcontext(0x0000ffff),
  custom(0F21F359-AB84-41E8-9A78-36D110E6D2F9, "Microsoft.Office.Interop.Excel.dll")

]
library Excel
{
    // TLib : Microsoft Visual Basic for Applications Extensibility 5.3 : {0002E157-0000-0000-C000-000000000046}
    importlib("VBE6EXT.OLB");
    // TLib : Microsoft Office 15.0 Object Library : {2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}
    importlib("MSO.DLL");
    // TLib : OLE Automation : {00020430-0000-0000-C000-000000000046}
    importlib("stdole2.tlb");

    // Forward declare all types defined in this typelib
    interface Adjustments;
    interface CalloutFormat;
    interface ColorFormat;
    interface LineFormat;
    interface ShapeNode;
    interface ShapeNodes;
    interface PictureFormat;
    interface ShadowFormat;
    interface TextEffectFormat;
    interface ThreeDFormat;
    interface FillFormat;
    interface DiagramNodes;
    interface DiagramNodeChildren;
    interface DiagramNode;
    interface IRTDUpdateEvent;
    interface IRtdServer;
    interface TextFrame2;
    interface IFont;
    interface IWindow;
    interface IWindows;
    interface IAppEvents;
    interface _Application;
    interface IWorksheetFunction;
    interface IRange;
    interface IChartEvents;
    interface _Chart;

library Excel下的Tlib项便是需要的依赖项。

标签