Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用C+将图片插入Excel+/CLI并获得100x警告C4691_C#_C++_.net_Excel - Fatal编程技术网

C# 使用C+将图片插入Excel+/CLI并获得100x警告C4691

C# 使用C+将图片插入Excel+/CLI并获得100x警告C4691,c#,c++,.net,excel,C#,C++,.net,Excel,我的任务是对现有的C++/CLI(带有Excel自动化)应用程序(目标框架:.NET4.0,IDE:VS2010)进行“小”修改。任务:将两张图片(*.jpg)插入Excel工作表。我很高兴在stackoverflow这里找到了一个线程,在这个线程中,这个任务正是用C#解决的。以下是链接: 在上面的线程中,我按照用户JMK的回答中提供的说明进行操作。代码编译时没有错误,可以正常工作!事实上,代码成功地将图片放置在工作表中。不幸的是,我还获得了100个C4691编译器警告 我的代码: /

我的任务是对现有的C++/CLI(带有Excel自动化)应用程序(目标框架:.NET4.0,IDE:VS2010)进行“小”修改。任务:将两张图片(*.jpg)插入Excel工作表。我很高兴在stackoverflow这里找到了一个线程,在这个线程中,这个任务正是用C#解决的。以下是链接:

在上面的线程中,我按照用户JMK的回答中提供的说明进行操作。代码编译时没有错误,可以正常工作!事实上,代码成功地将图片放置在工作表中。不幸的是,我还获得了100个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”:未引用的程序集“Office”中应包含引用的类型,而使用了当前转换单元中定义的类型。从程序集“Microsoft.Office.Interop.Excel,版本=12.0.0.0,区域性=中性,PublicKeyToken=71e9bce111e9429c”导入类型“Microsoft:Office:Interop:Excel:ApplicationClass”时发生此诊断

MSDN对此编译器警告的描述(对我来说)并没有特别的帮助。我不想简单地忽略警告(愚蠢),也不想使用pragma warning(邋遢)来“关闭”警告

如何消除警告C4691?所有的建议、评论,甚至(建设性的)批评,我们都将不胜感激。谢谢

    xlApp = gcnew Excel::Application();
这里有相当数量的自动发生,这种类型在C++ IDE中实际上并不是很好。与VB.NET和C#IDE不同,这些语言通常用于编写Office互操作代码

应用程序是一个接口,而不是一个类。当然,永远不可能创建接口的实例,它是一种抽象类型。当然,C++/CLI编译器知道这是不可能的,因此寻找实现该接口的类。它通过在接口类型上查找[CoClassAttribute]属性找到它,它指向Excel::ApplicationClass,一个由类型库导入器生成的合成.NET类。值得注意的是,IntelliSense解析器不知道这个技巧,并在语句下面放置红色的波形。您可以通过自己进行替换来摆脱它:

    xlApp = gcnew Excel::ApplicationClass();
继续执行C4691警告。Excel类型库使用另一个包含常用Office类型声明的类型库。您显然已经解决了这个问题,并正确地添加了对MSO.DLL的引用,从而在Microsoft::Office::Core命名空间中获取类型。但这是一个映射的命名空间名称,类型库名称实际上是“Office”,而不是“Microsoft.Office.Core”

因此C++/CLI编译器注意到了差异,ApplicationClass引用了Office类型库中名为Assistant的接口,因此它希望名为“Office”的程序集可用。这是将类型库名称映射到程序集名称的正常方式。换句话说,它不知道名称空间名称是否已映射。它也不知道,它没有做那个映射。它是由一个注册表项完成的

这是一个警告,而不是一个错误,因为事实证明它最终是正确的,它确实正确地将它映射到一个可用的程序集引用。您希望使用的是Microsoft.Office.Core。这是一场小小的赌博,从技术上讲,您可能会忘记添加对MSO.DLL的引用,而使用另一个名为“助手”的互操作类型。Boomshakalaka如果忽略该警告,则使用该界面

因此,请继续将激光设置为眩晕:

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

我更改了using语句,您引用的语句是错误的,不允许使用“Excel::Application”和“Core::MsoTriState”,这是另一个富有成效的编译错误源。

OK。为了方便起见,我把激光设为眩晕。C4691不见了。顺便说一下,我忘了在原始问题中包含以下代码行: