チュートリアル: Microsoft Office アセンブリからの型情報の埋め込み (C# および Visual Basic)
COM オブジェクトを参照するアプリケーションに型情報を埋め込むと、プライマリ相互運用機能アセンブリ (PIA: Primary Interop Assembly) を使用する必要がなくなります。また、埋め込み型情報を使用することで、バージョンに依存しないアプリケーションを作成できます。つまり、複数のバージョンの COM ライブラリの型を使用するようにプログラムを記述でき、バージョンごとに固有の PIA が不要になります。この方法は、Microsoft Office ライブラリのオブジェクトを使用するアプリケーション向けの一般的なシナリオです。型情報を埋め込むと、プログラムの同じビルドで、異なるコンピューター上にある異なるバージョンの Microsoft Office と連携できます。Microsoft Office のバージョンごとにプログラムや PIA を再配置する必要はありません。
このチュートリアルでは、次のタスクを行います。
複数のバージョンの Microsoft Office と連携するアプリケーションを作成するには.
別のバージョンの Microsoft Office がインストールされているコンピューターにアプリケーションを発行するには.
[!メモ]
お使いのマシンで、Visual Studio ユーザー インターフェイスの一部の要素の名前や場所が、次の手順とは異なる場合があります。これらの要素は、使用している Visual Studio のエディションや独自の設定によって決まります。詳細については、「Visual Studio の設定」を参照してください。
必須コンポーネント
このチュートリアルの前提条件は次のとおりです。
Visual Studio および Microsoft Excel がインストールされたコンピューター。
.NET Framework 4 および別のバージョンの Excel がインストール 2 台目のコンピューター。
複数のバージョンの Microsoft Office と連携するアプリケーションを作成するには
Excel がインストールされているコンピューターで Visual Studio を起動します。
[ファイル] メニューで、[新規]、[プロジェクト] クリックします。
[新しいプロジェクト] ダイアログ ボックスの [プロジェクトの種類] ペインで、[Windows] が選択されていることを確認します。[テンプレート] ペインの [コンソール アプリケーション] を選択します。[名前] ボックスに、CreateExcelWorkbookを入力し、を [OK] のボタンをクリックします。新しいプロジェクトが作成されます。
Visual Basicを使用する場合は、CreateExcelWorkbook プロジェクトのショートカット メニューを開き、[プロパティ] を選択します。[参照] のタブをクリックします。[追加] ボタンをクリックします。[参照] のフォルダーに [ソリューション エクスプローラー]、更新が Visual C#、ショートカット メニューを使用して、[参照の追加] を選択します。
[.NET] のタブで、最新バージョンの Microsoft.Office.Interop.Excel を選択します。たとえば、[Microsoft.Office.Interop.Excel 14.0.0.0] をクリックします。[OK] を選択します。
CreateExcelWorkbook プロジェクトの参照の一覧で、前の手順で追加した Microsoft.Office.Interop.Excel の参照を選択します。[プロパティ] ウィンドウで、Embed Interop Types プロパティが True に設定されていることを確認します。
[!メモ]
このチュートリアルで作成したアプリケーションは、相互運用の型情報が埋め込まれているため、異なるバージョンの Microsoft Office と連携して動作します。Embed Interop Types プロパティを False に設定した場合は、このアプリケーションと連携して動作する Microsoft Office のバージョンごとに PIA を組み込む必要があります。
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. ' In the For loop, both the row index and array index start at 1. ' Therefore the value of 4 at array index 0 is not included. 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. // In the For loop, both the row index and array index start at 1. // Therefore the value of 4 at array index 0 is not included. 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; } } } }
プロジェクトを保存します。
Ctrl キーを押しながら F5 キーを押して、プロジェクトをビルドおよび実行します。プログラム例で指定した場所 (C:\SampleFolder\SampleWorkbook.xls) に Excel ブックが作成されていることを確認します。
別のバージョンの Microsoft Office がインストールされているコンピューターにアプリケーションを発行するには
Visual Studio で、このチュートリアルで作成したプロジェクトを開きます。
[ビルド] で、メニューの [Publish CreateExcelWorkbook] を選択します。発行ウィザードの手順に従って、アプリケーションのインストール可能なバージョンを作成します。詳細については、「発行ウィザード (Visual Studio での Office 開発)」を参照してください。
.NET Framework 4 および別のバージョンの Excel がインストールされているコンピューターにアプリケーションをインストールします。
インストールが完了したら、インストールしたプログラムを実行します。
プログラム例で指定した場所 (C:\SampleFolder\SampleWorkbook.xls) に Excel ブックが作成されていることを確認します。
参照
処理手順
チュートリアル: マネージ アセンブリからの型の埋め込み (C# および Visual Basic)