簡體   English   中英

使用C ++ / CLI將圖片插入Excel並收到100x警告C4691

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

我的任務是對現有C ++ / CLI(具有Excel自動化)應用程序(目標框架:.NET4.0,IDE:VS2010)進行“小”修改。 任務:在Excel工作表中插入幾張圖片(* .jpg)。 我很高興在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”:在未引用的程序集“辦公室”中期望引用的類型,而是在當前翻譯單元中定義的類型。 從程序集“ Microsoft.Office.Interop.Excel,版本= 12.0.0.0,區域性=中性,PublicKeyToken = 71e9bce111e9429c”中導入類型“ Microsoft:Office:Interop:Excel:ApplicationClass”時,發生了此診斷。

MSDN對此編譯器警告的描述對我而言不是特別有用。 我既不想簡單地忽略警告(愚蠢的),也不想使用編譯指示(草率)來“關閉”警告。

如何消除警告C4691? 感謝所有建議,評論,甚至(建設性)批評。 謝謝

    xlApp = gcnew Excel::Application();

這里發生了很多自動魔術,這種魔術實際上在C ++ IDE中不能很好地起作用。 與VB.NET和C#IDE不同,這些語言通常用於編寫Office互操作代碼。

Excel :: Application是一個接口 ,而不是一個類。 當然,永遠不可能創建接口的實例,它是抽象類型。 當然,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並擁有另一個名為“ Assistant”的互操作類型。 Boomshakalaka如果您忽略警告,然后使用界面。

因此,繼續進行操作,使激光眩暈:

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

我更改了using語句,您引用的語句是錯誤的,並且不允許使用“ Excel :: Application”和“ Core :: MsoTriState”,這是另一個富有成效的編譯錯誤源。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM