メソッドに貼り付けるだけで機能するソースコードを多数用意しています。
新規BOOK
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);