共用方式為


逐步解說:從 Microsoft Office 組件內嵌型別資訊 (C# 和 Visual Basic)

如果將型別資訊內嵌於參考 COM 物件的應用程式中,您就不再需要主要 Interop 組件 (PIA)。 此外,內嵌的型別資訊還可以讓您達到應用程式版本獨立的目的。 也就是說,撰寫程式時,可以讓它使用來自多個 COM 程式庫版本的型別,而不需要每個版本的特定 PIA。 這在使用 Microsoft Office 程式庫物件的應用程式中是常見的案例。 內嵌型別資訊讓程式的相同組建可以在不同電腦上使用不同版本的 Microsoft Office,而不需要針對每個 Microsoft Office 版本重新部署程式或 PIA。

在這個逐步解說中,您將執行下列工作:

  • 建立使用 Microsoft Office Automation 物件的應用程式,並內嵌 Microsoft Office COM 程式庫的型別資訊。

  • 發行並使用多個 Microsoft Office 版本來執行不含 PIA 的應用程式。

注意事項注意事項

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

必要條件

本逐步解說需要下列項目:

  • 一部已安裝 Visual Studio 及 Microsoft Excel 的電腦。

  • 一部已安裝 .NET Framework 4 及不同版本 Excel 的電腦。

若要建立可搭配多個 Microsoft Office 版本運作的應用程式

  1. 在安裝 Excel 的電腦上啟動 Visual Studio。

  2. 在 [檔案] 功能表上,指向 [新增],然後按一下 [專案]。

  3. 在 [新增專案] 對話方塊的 [專案類型] 窗格中,確定已選取 [Windows]。 選取 [主控台應用程式] 中的 [範本] 窗格。 在 [名稱] 方塊中,輸入 CreateExcelWorkbook,然後按一下 [確定]。 接著會建立新專案。

  4. 如果您是使用 Visual Basic,請以滑鼠右鍵按一下 [CreateExcelWorkbook] 專案,然後按一下 [屬性]。 按一下 [參考] 索引標籤。 按一下 [加入] 按鈕。 如果您是使用 Visual C#,請以滑鼠右鍵按一下 [方案總管] 中的 [參考] 資料夾,然後按一下 [加入參考]。

  5. 在 [.NET] 索引標籤上,按一下最新版的 Microsoft.Office.Interop.Excel, 例如 [Microsoft.Office.Interop.Excel 14.0.0.0], 按一下 [確定]。

  6. 在 CreateExcelWorkbook 專案的參考清單中,選取您在上一個步驟中加入的 Microsoft.Office.Interop.Excel 參考。 在 [屬性] 視窗中,確定 Embed Interop Types 屬性已設為 True。

    注意事項注意事項

    本逐步解說中建立的應用程式是因為內嵌的 Interop 型別資訊而能與不同版本的 Microsoft Office 一起執行。 如果 Embed Interop Types 屬性設定為 False,您就必須針對與應用程式一起執行的每個 Microsoft Office 版本,將 PIA 包含在應用程式中。

  7. 如果您是使用 Visual Basic,請按兩下 Module1.vb 檔。 如果您是使用 Visual C#,請按兩下 Program.cs 檔。 使用下列程式碼取代檔案中的程式碼。

    Imports Excel = Microsoft.Office.Interop.Excel
    
    Module Module1
    
        Sub Main()
            Dim values = {4, 6, 18, 2, 1, 76, 0, 3, 11}
    
            CreateWorkbook(values, "C:\SampleFolder\SampleWorkbook.xls")
        End Sub
    
        Sub CreateWorkbook(ByVal values As Integer(), ByVal filePath As String)
            Dim excelApp As Excel.Application = Nothing
            Dim wkbk As Excel.Workbook
            Dim sheet As Excel.Worksheet
    
            Try
                ' Start Excel and create a workbook and worksheet.
                excelApp = New Excel.Application
                wkbk = excelApp.Workbooks.Add()
                sheet = CType(wkbk.Sheets.Add(), Excel.Worksheet)
                sheet.Name = "Sample Worksheet"
    
                ' Write a column of values.
                For i = 1 To values.Length - 1
                    sheet.Cells(i, 1) = values(i)
                Next
    
                ' Suppress any alerts and save the file. Create the directory 
                ' if it does not exist. Overwrite the file if it exists.
                excelApp.DisplayAlerts = False
                Dim folderPath = My.Computer.FileSystem.GetParentPath(filePath)
                If Not My.Computer.FileSystem.DirectoryExists(folderPath) Then
                    My.Computer.FileSystem.CreateDirectory(folderPath)
                End If
                wkbk.SaveAs(filePath)
        Catch
    
            Finally
                sheet = Nothing
                wkbk = Nothing
    
                ' Close Excel.
                excelApp.Quit()
                excelApp = Nothing
            End Try
    
        End Sub
    End Module
    
    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, @"C:\SampleFolder\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;
                }
            }
        }
    }
    
  8. 儲存專案。

  9. 按 CTRL+F5 建置並執行專案。 確認已經在範例程式碼所指定的位置建立 Excel 工作簿:C:\SampleFolder\SampleWorkbook.xls。

若要將應用程式發行至已安裝不同版本 Microsoft Office 的電腦上

  1. 開啟這個逐步解說在 Visual Studio 中建立的專案。

  2. 在 [建置] 功能表上,按一下 [發行 CreateExcelWorkbook]。 按照發行精靈的步驟建立應用程式的可安裝版本。 如需詳細資訊,請參閱發行精靈

  3. 在已安裝 .NET Framework 4 及不同版本 Excel 的電腦上安裝應用程式。

  4. 當安裝完成時,執行這個程式。

  5. 確認已經在範例程式碼所指定的位置建立 Excel 工作簿:C:\SampleFolder\SampleWorkbook.xls。

請參閱

工作

逐步解說:從 Managed 組件內嵌型別 (C# 和 Visual Basic)

參考

/link (Visual Basic)

/link (C# 編譯器選項)