HOW TO:使用 Visual C# 2010 功能存取 Office Interop 物件 (C# 程式設計手冊)

Visual C# 2010 引進了可簡化 Office API 物件存取的新功能。 這些新功能包括具名和選擇性引數、稱為 dynamic 的新型別,以及可將引數傳遞給 COM 方法中的參考參數 (將其視為實值參數) 的能力。

在本主題中,您將使用新功能撰寫程式碼,建立並顯示一份 Microsoft Office Excel 工作表。 接著再撰寫程式碼,加入一份 Office Word 文件,其中含有連結至該 Excel 工作表的圖示。

若要完成本逐步解說,您的電腦必須安裝 Microsoft Office Excel 2007 和 Microsoft Office Word 2007。

如果您使用的作業系統比 Windows Vista 舊,請務必安裝 .NET Framework 2.0。

注意事項注意事項

您的電腦可能會在下列說明中,以不同名稱或位置顯示某些 Visual Studio 使用者介面項目。 您所擁有的 Visual Studio 版本以及使用的設定會決定這些項目。 如需詳細資訊,請參閱 Visual Studio 設定

若要建立新的主控台應用程式

  1. 啟動 Visual Studio。

  2. 在 [檔案] 功能表上,指向 [新增],然後按一下 [專案]。 [新增專案] 對話方塊隨即出現。

  3. 在 [已安裝的範本] 窗格中,展開 [Visual C#],然後按一下 [Windows]。

  4. 查看 [新增專案] 對話方塊的頂端,確定已選取 [.NET Framework 4] 做為目標 Framework。

  5. 按一下 [範本] 窗格中的 [主控台應用程式]。

  6. 在 [名稱] 欄位中,輸入專案的名稱。

  7. 按一下 [確定]。

    新專案即會出現於 [方案總管] 中。

若要加入參考

  1. 在 [方案總管] 中,以滑鼠右鍵按一下您的專案名稱,然後按一下 [加入參考]。 [加入參考] 對話方塊隨即出現。

  2. 在 [.NET] 頁面的 [元件名稱] 清單中選取 [Microsoft.Office.Interop.Word],然後按住 CTRL 鍵並選取 [Microsoft.Office.Interop.Excel]。

  3. 按一下 [確定]。

若要加入必要的 using 指示詞

  1. 在 [方案總管] 中,以滑鼠右鍵按一下 [Program.cs] 檔案,再按一下 [檢視程式碼]。

  2. 在程式碼檔案的頂端加入下列 using 指示詞。

    using Excel = Microsoft.Office.Interop.Excel;
    using Word = Microsoft.Office.Interop.Word;
    

若要建立銀行帳戶清單

  1. 將下列類別定義貼到 Program.cs 的 Program 類別下方。

    public class Account
    {
        public int ID { get; set; }
        public double Balance { get; set; }
    }
    
  2. 將下列程式碼加入至 Main 方法中,建立包含兩個帳戶的 bankAccounts 清單。

    // Create a list of accounts.
    var bankAccounts = new List<Account> {
        new Account { 
                      ID = 345678,
                      Balance = 541.27
                    },
        new Account {
                      ID = 1230221,
                      Balance = -127.44
                    }
    };
    

若要宣告將帳戶資訊匯出至 Excel 的方法

  1. 將下列方法加入至 Program 類別,設定 Excel 工作表。

    方法 Add 有一個選擇性參數,可以用來指定特定範本。 選擇性參數是 Visual C# 2010 的新增功能,如果您想使用參數的預設值,即可省略該參數的引數。 由於下列程式碼並未傳送引數,所以 Add 將使用預設範本並建立新活頁簿。 舊版 C# 中的相等陳述式需要替代符號引數:ExcelApp.Workbooks.Add(Type.Missing)。

    static void DisplayInExcel(IEnumerable<Account> accounts)
    {
        var excelApp = new Excel.Application();
        // Make the object visible.
        excelApp.Visible = true;
    
        // Create a new, empty workbook and add it to the collection returned 
        // by property Workbooks. The new workbook becomes the active workbook.
        // Add has an optional parameter for specifying a praticular template. 
        // Because no argument is sent in this example, Add creates a new workbook. 
        excelApp.Workbooks.Add();
    
        // This example uses a single workSheet. The explicit type casting is
        // removed in a later procedure.
        Excel._Worksheet workSheet = (Excel.Worksheet)excelApp.ActiveSheet;
    }
    
  2. 在 DisplayInExcel 結尾加入下列程式碼。 此程式碼會在工作表中第一列的前兩欄插入值。

    // Establish column headings in cells A1 and B1.
    workSheet.Cells[1, "A"] = "ID Number";
    workSheet.Cells[1, "B"] = "Current Balance";
    
  3. 在 DisplayInExcel 結尾加入下列程式碼。 foreach 迴圈會將帳戶清單中的資訊放入工作表中後續兩列的前兩欄。

    
    var row = 1;
    foreach (var acct in accounts)
    {
        row++;
        workSheet.Cells[row, "A"] = acct.ID;
        workSheet.Cells[row, "B"] = acct.Balance;
    }
    
  4. 將下列程式碼加入至 DisplayInExcel 的結尾,調整資料行寬度以符合內容。

    workSheet.Columns[1].AutoFit();
    workSheet.Columns[2].AutoFit();
    

    舊版 C# 需要將這些作業明確轉型,因為 ExcelApp.Columns[1] 傳回 Object,而 AutoFit 是 Excel Range 方法。 下列程式碼行會顯示此轉型。

    ((Excel.Range)workSheet.Columns[1]).AutoFit();
    ((Excel.Range)workSheet.Columns[2]).AutoFit();
    

    如果組件被 /link 編譯器選項參考,或相當於 Excel [內嵌 Interop 型別] 屬性設為 true 時,Visual C# 2010 會自動將傳回的 Object 轉換為 dynamic。 True 是此屬性的預設值。

若要執行專案

  1. 在 Main 結尾加入下列程式碼行。

    // Display the list in an Excel spreadsheet.
    DisplayInExcel(bankAccounts);
    
  2. 按下 CTRL+F5 鍵。

    接著會出現包含兩個帳戶資料的 Excel 工作表。

若要加入 Word 文件

  1. 為了說明 Visual C# 2010 增強 Office 程式設計的其他方式,下列程式碼會開啟 Word 應用程式,並建立一個連結至 Excel 工作表的圖示。

    請將本步驟稍後提供的 CreateIconInWordDoc 方法貼入 Program 類別中。 CreateIconInWordDoc 使用具名和選擇性引數,降低 AddPasteSpecial 方法呼叫的複雜度。 這些呼叫合併了另外兩個 Visual C# 2010 新功能,簡化對具有參考參數之 COM 方法的呼叫。 首先,您可以將引數傳遞給參考參數 (將其視為實值參數)。 也就是說,您可以直接傳送值,而不需要為每個參考參數建立變數。 編譯器會產生暫存變數用來存放引數值,並在您從呼叫返回時捨棄這些變數。 其次,您可以省略引數清單中的 ref 關鍵字。

    Add 方法有 4 個參考參數,全部都是選擇性參數。 在 Visual C# 2010 中,如果您想使用這些參數的預設值,即可省略任一或全部參數的引數。 至於 Visual C# 2008 (含) 以前版本中,則需為每個參數提供引數,而引數必須是變數,因為這些參數是參考參數。

    PasteSpecial 方法會插入剪貼簿的內容。 此方法有 7 個參考參數,全部都是選擇性參數。 下列程式碼指定其中兩個引數:Link 用來建立連至剪貼簿內容的來源連結,DisplayAsIcon 用來將該連結顯示為圖示。 在 Visual C# 2010 中,您可以為這兩個引數使用具名引數,並省略其他引數。 雖然這些是參考參數,但您不需使用 ref 關鍵字,也不需建立變數以做為引數傳送。 您可以直接傳送值。 至於 Visual C# 2008 (含) 以前版本中,則需為每個參考參數傳送變數引數。

    static void CreateIconInWordDoc()
    {
        var wordApp = new Word.Application();
        wordApp.Visible = true;
    
        // The Add method has four reference parameters, all of which are 
        // optional. Visual C# 2010 allows you to omit arguments for them if
        // the default values are what you want.
        wordApp.Documents.Add();
    
        // PasteSpecial has seven reference parameters, all of which are 
        // optional. This example uses named arguments to specify values 
        // for two of the parameters. Although these are reference 
        // parameters, you do not need to use the ref keyword, or to create 
        // variables to send in as arguments. You can send the values directly.
        wordApp.Selection.PasteSpecial( Link: true, DisplayAsIcon: true);
    }
    

    在 Visual C# 2008 或此語言的以前版本中,需用到下列較為複雜的程式碼。

    static void CreateIconInWordDoc2008()
    {
        var wordApp = new Word.Application();
        wordApp.Visible = true;
    
        // The Add method has four parameters, all of which are optional. 
        // In Visual C# 2008 and earlier versions, an argument has to be sent 
        // for every parameter. Because the parameters are reference  
        // parameters of type object, you have to create an object variable
        // for the arguments that represents 'no value'. 
    
        object useDefaultValue = Type.Missing;
    
        wordApp.Documents.Add(ref useDefaultValue, ref useDefaultValue,
            ref useDefaultValue, ref useDefaultValue);
    
        // PasteSpecial has seven reference parameters, all of which are
        // optional. In this example, only two of the parameters require
        // specified values, but in Visual C# 2008 an argument must be sent
        // for each parameter. Because the parameters are reference parameters,
        // you have to contruct variables for the arguments.
        object link = true;
        object displayAsIcon = true;
    
        wordApp.Selection.PasteSpecial( ref useDefaultValue,
                                        ref link,
                                        ref useDefaultValue,
                                        ref displayAsIcon,
                                        ref useDefaultValue,
                                        ref useDefaultValue,
                                        ref useDefaultValue);
    }
    
  2. 在 Main 結尾加入下列陳述式:

    // Create a Word document that contains an icon that links to
    // the spreadsheet.
    CreateIconInWordDoc();
    
  3. 在 DisplayInExcel 結尾加入下列陳述式: Copy 方法會將工作表加入至剪貼簿。

    // Put the spreadsheet contents on the clipboard. The Copy method has one
    // optional parameter for specifying a destination. Because no argument  
    // is sent, the destination is the Clipboard.
    workSheet.Range["A1:B3"].Copy();
    
  4. 按下 CTRL+F5 鍵。

    隨即出現包含圖示的 Word 文件。 在圖示上按兩下,可將工作表帶到前景。

若要設定內嵌 Interop 型別屬性

  1. 當您呼叫的 COM 型別在執行階段不需要主要 Interop 組件 (PIA) 時,可以有額外的加強功能。 由於不需依賴 PIA,因此可以達到版本獨立,也更容易部署。 如需不使用 PIA 之程式設計優點的詳細資訊,請參閱逐步解說:從 Managed 組件內嵌型別 (C# 和 Visual Basic)

    此外,由於 COM 方法所必要和傳回的型別可以使用型別 dynamic (而非 Object) 來代表,因此也更容易進行程式設計。 具有型別 dynamic 的變數直到執行階段才會進行評估,因此不需要明確轉型。 如需詳細資訊,請參閱 使用動態型別 (C# 程式設計手冊)

    Visual C# 2010 的預設行為是內嵌型別資訊,而非使用 PIA。 由於這項預設值的關係,幾個上述範例因為不需明確轉型而得以簡化。 例如,DisplayInExcel 中的 worksheet 宣告會被撰寫為 Excel._Worksheet workSheet = excelApp.ActiveSheet,而非 Excel._Worksheet workSheet = (Excel.Worksheet)excelApp.ActiveSheet。 在同一個方法中的 AutoFit 呼叫也需要使用不含預設值的明確轉型,因為 ExcelApp.Columns[1] 傳回 Object,而 AutoFit 是 Excel 方法。 下列程式碼顯示轉型。

    ((Excel.Range)workSheet.Columns[1]).AutoFit();
    ((Excel.Range)workSheet.Columns[2]).AutoFit();
    
  2. 若要變更預設值並改用 PIA (而非內嵌型別資訊),請展開 [方案總管] 中的 [參考] 節點,然後選取 [Microsoft.Office.Interop.Excel] 或 [Microsoft.Office.Interop.Word]。

  3. 如果您看不到 [屬性] 視窗,請按 F4

  4. 在屬性清單中尋找 [內嵌 Interop 型別],然後將其值變更為 [False]。 這相當於在命令提示字元改用 /reference 編譯器選項,取代 /link

若要將其他格式加入至資料表

  1. 使用下列陳述式取代 DisplayInExcel 中的兩個 AutoFit 呼叫。

    // Call to AutoFormat in Visual C# 2010.
    workSheet.Range["A1", "B3"].AutoFormat(
        Excel.XlRangeAutoFormat.xlRangeAutoFormatClassic2);
    

    AutoFormat 方法有 7 個實值參數,而且全都是選擇性參數。 具名和選擇性引數可讓您選擇不提供引數、提供部分引數或全部引數。 在上述陳述式中,只提供一個參數的引數:Format。 因為 Format 是參數清單中的第一個參數,所以不需要提供參數名稱。 但是,如果加入參數名稱,陳述式應該比較容易讓人了解,如下列程式碼所示。

    // Call to AutoFormat in Visual C# 2010.
    workSheet.Range["A1", "B3"].AutoFormat(Format:
        Excel.XlRangeAutoFormat.xlRangeAutoFormatClassic2);
    
  2. 按 CTRL+F5 可查看結果。 其他格式會在 XlRangeAutoFormat (英文) 列舉型別中列出。

  3. 請將步驟 1 的陳述式與下列程式碼進行比較,下列程式碼顯示 Visual C# 2008 (含) 以前版本中的所需引數。

    // The AutoFormat method has seven optional value parameters. The
    // following call specifies a value for the first parameter, and uses 
    // the default values for the other six. 
    
    // Call to AutoFormat in Visual C# 2008. This code is not part of the
    // current solution.
    excelApp.get_Range("A1", "B4").AutoFormat(Excel.XlRangeAutoFormat.xlRangeAutoFormatTable3, 
        Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 
        Type.Missing);
    

範例

下列程式碼顯示完整範例。

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


namespace OfficeProgramminWalkthruComplete
{
    class Walkthrough
    {
        static void Main(string[] args)
        {
            // Create a list of accounts.
            var bankAccounts = new List<Account> 
            {
                new Account { 
                              ID = 345678,
                              Balance = 541.27
                            },
                new Account {
                              ID = 1230221,
                              Balance = -127.44
                            }
            };

            // Display the list in an Excel spreadsheet.
            DisplayInExcel(bankAccounts);

            // Create a Word document that contains an icon that links to
            // the spreadsheet.
            CreateIconInWordDoc();
        }

        static void DisplayInExcel(IEnumerable<Account> accounts)
        {
            var excelApp = new Excel.Application();
            // Make the object visible.
            excelApp.Visible = true;

            // Create a new, empty workbook and add it to the collection returned 
            // by property Workbooks. The new workbook becomes the active workbook.
            // Add has an optional parameter for specifying a praticular template. 
            // Because no argument is sent in this example, Add creates a new workbook. 
            excelApp.Workbooks.Add();

            // This example uses a single workSheet. 
            Excel._Worksheet workSheet = excelApp.ActiveSheet;

            // Earlier versions of C# require explicit casting.
            //Excel._Worksheet workSheet = (Excel.Worksheet)excelApp.ActiveSheet;

            // Establish column headings in cells A1 and B1.
            workSheet.Cells[1, "A"] = "ID Number";
            workSheet.Cells[1, "B"] = "Current Balance";

            var row = 1;
            foreach (var acct in accounts)
            {
                row++;
                workSheet.Cells[row, "A"] = acct.ID;
                workSheet.Cells[row, "B"] = acct.Balance;
            }

            workSheet.Columns[1].AutoFit();
            workSheet.Columns[2].AutoFit();

            // Call to AutoFormat in Visual C# 2010. This statement replaces the 
            // two calls to AutoFit.
            workSheet.Range["A1", "B3"].AutoFormat(
                Excel.XlRangeAutoFormat.xlRangeAutoFormatClassic2);

            // Put the spreadsheet contents on the clipboard. The Copy method has one
            // optional parameter for specifying a destination. Because no argument  
            // is sent, the destination is the Clipboard.
            workSheet.Range["A1:B3"].Copy();
        }

        static void CreateIconInWordDoc()
        {
            var wordApp = new Word.Application();
            wordApp.Visible = true;

            // The Add method has four reference parameters, all of which are 
            // optional. Visual C# 2010 allows you to omit arguments for them if
            // the default values are what you want.
            wordApp.Documents.Add();

            // PasteSpecial has seven reference parameters, all of which are 
            // optional. This example uses named arguments to specify values 
            // for two of the parameters. Although these are reference 
            // parameters, you do not need to use the ref keyword, or to create 
            // variables to send in as arguments. You can send the values directly.
            wordApp.Selection.PasteSpecial(Link: true, DisplayAsIcon: true);
        }
    }

    public class Account
    {
        public int ID { get; set; }
        public double Balance { get; set; }
    }
}

請參閱

工作

HOW TO:在 Office 程式設計中使用具名和選擇性引數 (C# 程式設計手冊)

參考

dynamic (C# 參考)

Type.Missing

概念

具名和選擇性引數 (C# 程式設計手冊)

其他資源

使用動態型別 (C# 程式設計手冊)