使用C /CLI将图片插入Excel中,并获得100倍警告C4691

Inserted a picture into Excel using C++/CLI and got 100x warning C4691

本文关键字:100倍 C4691 警告 Excel CLI 插入 使用      更新时间:2023-10-16

i被分配了对现有的C /CLI(带有Excel Automation)应用程序进行"小"修改的任务(目标框架:.NET4.0,IDE:VS2010)。任务:将几张图片(*.jpg)插入Excel工作表中。我很高兴在stackoverflow上找到一个线程,在该线程中,该任务在C#中得到了解决。链接在这里:请参阅此问题的结尾

在上述线程中,我遵循用户JMK答案中提供的说明。代码没有错误并起作用!确实,代码成功地将图片放在工作表中。不幸的是,我还获得了一百C4691编译器警告。

我的代码:

    //attributes - used by several different methods
private:
    Excel::Application^ xlApp;
    Excel::_Workbook^ xlBook;
    Excel::Workbooks^ xlBooks;
    Excel::Sheets^ xlSheets;
    Excel::_Worksheet^ xlSheet;
    Excel::Range^ range;
    String^ templateFilename;
    String^ picFilename;
private: System::Void buttonRunExcel_Click(System::Object^  sender, System::EventArgs^  e)
{
//create the Excel Application
xlApp = gcnew Excel::Application();//55 C4691 compiler warnings for this line of code
xlBooks = xlApp->Workbooks;//no compiler warnings here
//open the Excel template - 30 C4691 compiler warnings for the next line of code
xlBook = xlApp->Workbooks->Open(templateFilename, Type::Missing, false, Type::Missing, Type::Missing, Type::Missing, Type::Missing, Type::Missing, Type::Missing, Type::Missing, Type::Missing, Type::Missing, Type::Missing, Type::Missing, Type::Missing);
xlSheets = xlBook->Worksheets;//no compiler warnings here
//set the active worksheet to sheet 3
xlSheet = (Excel::_Worksheet^)xlSheets->Item[3];//no compiler warnings here
//insert the picture - 15 C4691 compiler warnings for the next line of code
xlSheet->Shapes->AddPicture(picFilename, Core::MsoTriState::msoFalse, Core::MsoTriState::msoCTrue, 100, 200, 640, 480);

}

我的参考文献:

    using namespace System::Reflection;
using namespace System::Runtime::InteropServices;
using namespace Microsoft::Office::Core;
using namespace Microsoft::Office::Interop::Excel;

样本编译器警告:

警告C4691:" Microsoft :: Office :: Core :: Assistant :: Assistant":未参考的组件" Office",类型为" Office",类型为当前翻译单元中使用的类型。此诊断是在导入类型的" Microsoft:Office:Interop:excel:applicatclass"中发生的诊断。

MSDN对此编译器警告的描述并不特别有用(对我来说)。我不想简单地忽略警告(愚蠢),也不想使用Pragma警告(sloppy)"关闭"警告。

如何消除警告C4691?感谢所有建议,评论,甚至(建设性的)批评。谢谢

    xlApp = gcnew Excel::Application();

这里发生了相当多的自动化,这种自动化实际上在C IDE中的工作方式并不是很好。与vb.net和c#ides不同,通常用于编写Office Interop代码的语言。

excel ::应用程序是接口,而不是类。当然,永远不可能创建接口的实例,它是一种抽象类型。C /CLI编译器当然知道这是不可能的,并且会寻找实现界面的A 。它通过在接口类型上查找[CoclassAttribute]属性来找到它,它指向Excel :: ApplicationClass,applicationClass,这是一个由类型库进口商生成的合成.NET类。值得注意的是,IntelliSense解析器对此技巧不了解,并将红色的脚尖置于该声明之下。您会通过自己进行替代来摆脱它:

    xlApp = gcnew Excel::ApplicationClass();

向C4691警告开始。Excel类型库使用另一个类型库,其中包含通用的Office类型声明。显然,您已经弄清楚了这一点,并正确添加了对mso.dll的参考,在Microsoft :: Office :: Core namepace中获取类型。但这是一个映射的名称名称,类型库名称实际上是" Office",而不是" Microsoft.office.core"。

因此,C /CLI编译器会注意到差异,ApplicationClass是指在Office类型库中名为Assistant的接口,因此它希望可以使用一个名为" Office"的程序集。这是类型库名称映射到汇编名称的正常方式。换句话说,它不知道命名空间名称已映射。它也不知道,它没有进行映射。它是由注册表密钥完成的。

这是一个警告,而不是错误,因为它实际上确实是正确的,最终确实正确地将其映射到了可用的汇编参考之一。您期望使用的一个,microsoft.office.cor。这是正确发生的是一场赌博,从技术上讲,您可能会忘记将参考添加到mso.dll上,并拥有另一种名为"助手"的Interop类型。双向Boomshakalaka如果您忽略警告,然后使用接口。

因此,请继续将激光设置为昏迷:

    #pragma warning(disable:4691)
    using namespace Microsoft::Office;
    using namespace Microsoft::Office::Interop;

我使用使用语句更改了,您引用的语句是错误的,不允许使用" Excel :: application :: corep :: msotristate",这是另一种富有成果的编译错误来源。<<<<<<<<<<<<<<<<<<<</p>