メソッドに貼り付けるだけで機能するソースコードを多数用意しています。
ブック操作
Excelを開いたまま処理を終了させる
Excelが開いた状態でメソッドやアプリを終了しても、プロセスが残らないコードです。
以下の処理を実行すると、Excelの新規Bookが1つ起動します。
処理自体は最後まで流れますが、表示したExcelは開いたままになっており、マウス操作でExcelを閉じてもタスクマネージャにプロセスは残りません。
ポイントは、処理の最後でExcelの終了処理(BOOKやApplicationクラスの終了処理)を行わず、COMオブジェクトの解放のみを行う点です。
// Excel操作用オブジェクト
Microsoft.Office.Interop.Excel.Application xlApp = null;
Microsoft.Office.Interop.Excel.Workbooks xlBooks = null;
Microsoft.Office.Interop.Excel.Workbook xlBook = null;
Microsoft.Office.Interop.Excel.Sheets xlSheets = null;
Microsoft.Office.Interop.Excel.Worksheet xlSheet = null;
// Excelアプリケーション生成
xlApp = new Microsoft.Office.Interop.Excel.Application();
// 新規のExcelブックを開く
// Addメソッド
xlBooks = xlApp.Workbooks;
xlBook = xlBooks.Add();
// シートを選択する
xlSheets = xlBook.Worksheets;
xlSheet = xlSheets[1] as Microsoft.Office.Interop.Excel.Worksheet; // 1シート目を操作対象に設定する
// 表示
xlApp.Visible = true;
// ■■■以下、COMオブジェクトの解放■■■
// Sheet解放
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheets);
// ◆プロセスが残らないようにメソッドを抜けるには、
// ★の部分をコメントアウトする
// Book解放
//xlBook.Close(); //★
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBooks);
// Excelアプリケーションを解放
//xlApp.Quit(); //★
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
ExcelBook、アプリケーションを閉じる
Excelを閉じる処理です。
「◆Bookを閉じる◆」で、WorkBookを閉じる処理を行い、
「◆Excelアプリケーション閉じる◆」で、アプリケーションの終了処理を行っています。
{
// Excel操作用オブジェクト
Application xlApp = null;
Workbooks xlBooks = null;
Workbook xlBook = null;
Sheets xlSheets = null;
Worksheet xlSheet = null;
// Excelアプリケーション生成
xlApp = new Application();
xlApp.DisplayAlerts = false; // 確認メッセージを非表示に設定
// 新規のExcelブックを開く
xlBooks = xlApp.Workbooks;
xlBook = xlBooks.Add();
// シートを選択する
xlSheets = xlBook.Worksheets;
xlSheet = xlSheets[1] as Worksheet; // 1シート目を操作対象に設定する
// 確認のためExcelを表示させて3秒停止
xlApp.Visible = true;
System.Threading.Thread.Sleep(3000);
// ■■■以下、COMオブジェクトの解放■■■
// Sheet解放
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheets);
// ◆Bookを閉じる◆
// Closeメソッド
xlBook.Close();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBooks);
// アラートを元に戻す
xlApp.DisplayAlerts = true;
// ◆Excelアプリケーション閉じる◆
// Quitメソッド
xlApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
}
既存のExcelファイルを開く
既存のExcelファイルを開く処理です。
新規ファイルを開く際はAddメソッドを使用しましたが、既存ファイルを開く場合はOpenメソッドを使用します。
xlBooks.Open(fileNm)の、[fileNm]には、対象ファイルの絶対パスを指定します。
// Excel操作用オブジェクト
Microsoft.Office.Interop.Excel.Application xlApp = null;
Microsoft.Office.Interop.Excel.Workbooks xlBooks = null;
Microsoft.Office.Interop.Excel.Workbook xlBook = null;
Microsoft.Office.Interop.Excel.Sheets xlSheets = null;
Microsoft.Office.Interop.Excel.Worksheet xlSheet = null;
// Excelアプリケーション生成
xlApp = new Microsoft.Office.Interop.Excel.Application();
// ◆操作対象のExcelブックを開く◆
// Openメソッド
xlBooks = xlApp.Workbooks;
xlBook = xlBooks.Open(System.IO.Path.GetFullPath(@"..\..\..\data\work01.xlsx"));
// シートを選択する
xlSheets = xlBook.Worksheets;
xlSheet = xlSheets[1] as Microsoft.Office.Interop.Excel.Worksheet; // 1シート目を操作対象に設定する
// 表示
xlApp.Visible = true;
// ■■■以下、COMオブジェクトの解放■■■
// Sheet解放
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheets);
// Book解放
//xlBook.Close();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBooks);
// Excelアプリケーションを解放
//xlApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
Excelファイルを新規で作成する
Excelファイルを新規で開く処理です。
以下の処理を実行すると、Excelの新規Bookが1つ起動します。
// Excel操作用オブジェクト
Microsoft.Office.Interop.Excel.Application xlApp = null;
Microsoft.Office.Interop.Excel.Workbooks xlBooks = null;
Microsoft.Office.Interop.Excel.Workbook xlBook = null;
Microsoft.Office.Interop.Excel.Sheets xlSheets = null;
Microsoft.Office.Interop.Excel.Worksheet xlSheet = null;
// Excelアプリケーション生成
xlApp = new Microsoft.Office.Interop.Excel.Application();
// ◆新規のExcelブックを開く◆
// Addメソッド
xlBooks = xlApp.Workbooks;
xlBook = xlBooks.Add();
// シートを選択する
xlSheets = xlBook.Worksheets;
xlSheet = xlSheets[1] as Microsoft.Office.Interop.Excel.Worksheet; // 1シート目を操作対象に設定する
// 表示
xlApp.Visible = true;
// 3秒停止
System.Threading.Thread.Sleep(3000);
// ■■■以下、COMオブジェクトの解放■■■
// Sheet解放
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheets);
// Book解放
xlBook.Close();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBooks);
// Excelアプリケーションを解放
xlApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);