[VS2010] 型別等價實例:在免部署 Office PIAs (Primary Interop Assemblies) 的情況下存取 Office 物件模型

[VS2010] 型別等價實例:在免部署 Office PIAs (Primary Interop Assemblies) 的情況下存取 Office 物件模型

在前一篇型別等價 (Type Equivalence) 的文章中,說明了如何使用 Managed code 實作型別等價的功能,但最能夠發揮型別等價的地方是在 COM 元件互通,最典型的範例就是 Microsoft Office 的物件模型存取,當引用 Office 型別函式庫 (type libraries) 時,都需要額外部署 PIAs 才能正常使用,但是 PIAs 有相容性的問題,每個 Office 應用程式所用的 PIAs 都不同,因此就算是具有相同的介面,仍然無法完全相容於不同的版本,通常保險的方式是隨不同的版本部署不同的 Office PIAs,到了 Visual Studio 2010 與 .NET Framework 4.0 後,這個現象終於可以獲得實質上的解決了,型別等價功能可以將 Office PIAs 物件介面匯入到用戶端應用程式,可以不用再將有點大的 Office PIAs 包裝在安裝程式中一起部署。

 

下列程序可以建立一個具有型別等價的 Office 用戶端應用程式,本例以 Office 2007 為腳本 (參考文件使用的是 Office 2010 Beta)。

 

請注意:若要執行這個程序,電腦中必須要安裝 Visual Studio 2010 以及 Office PIAs (例如 Office 2010 或 2007),並且準備另一台電腦安裝 .NET Framework 4.0 Beta 2,以及與開發電腦不同版本的 Office (例如 Office 2003)

 

A. 建立一個新的主控台應用程式,命名為 TypeEquivalenceForOfficePIAs。

 

image

 

B. 加入 Excel 12.0 Object Library 的參考:

 

image

 

加入以後,請到 References 中瀏覽 Microsoft.Office.Core 以及 Microsoft.Office.Excel.Interop 兩個型別庫,可以發現 Visual Studio 2010 已經自動將這兩個型別庫設為 Embed Interop Type 為 TRUE:

 

image

 

C. 加入下列程式碼到 Program.cs 中:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Excel = Microsoft.Office.Interop.Excel;

namespace CreateExcelWorkbook
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] values = {4, 6, 18, 2, 1, 76, 0, 3, 11};

            CreateWorkbook(values, Environment.CurrentDirectory + @"\SampleWorkbook.xls");
        }

        static void CreateWorkbook(int[] values, string filePath)
        {
            Excel.Application excelApp = null;
            Excel.Workbook wkbk;
            Excel.Worksheet sheet;

            try
            {
                    // Start Excel and create a workbook and worksheet.
                    excelApp = new Excel.Application();
                    wkbk = excelApp.Workbooks.Add();
                    sheet = wkbk.Sheets.Add() as Excel.Worksheet;
                    sheet.Name = "Sample Worksheet";

                    // Write a column of values.
                    for (int i = 1; i < values.Length; i++)
                    {
                        sheet.Cells[i, 1] = values[i];
                    }

                    // Suppress any alerts and save the file. Create the directory
                    // if it does not exist. Overwrite the file if it exists.
                    excelApp.DisplayAlerts = false;
                    string folderPath = Path.GetDirectoryName(filePath);
                    if (!Directory.Exists(folderPath))
                    {
                        Directory.CreateDirectory(folderPath);
                    }
                    wkbk.SaveAs(filePath);
            }
            catch
            {
            }
            finally
            {
                sheet = null;
                wkbk = null;

                // Close Excel.
                excelApp.Quit();
                excelApp = null;
            }
        }
    }
}

 

D. 建置這個專案並執行 (觀察一下,應該看不到 Microsoft.Office.Core.dll 以及 Microsoft.Office.Excel.Interop.dll),應可以看到新建好的 SampleWorkbook.xls。

E. 將建置出來的程式檔案複製到另一台安裝不同版本的 Office 電腦中執行,應該也可以得到相同的結果。

 

參考資料:

Walkthrough: Embedding Type Information from Microsoft Office Assemblies (C# and Visual Basic)

http://msdn.microsoft.com/en-us/library/ee317478(VS.100).aspx