メソッドに貼り付けるだけで機能するソースコードを多数用意しています。
セル操作
二次元配列をセルに一括出力する
二次元配列の値をそのままセルに一括出力する方法です。
二次元配列の各要素が1つのセルに設定されます。
以下の処理では、適当な二次元配列を作成後、指定したセルの範囲に値を出力しています。
#region "work 貼り付ける値を作成"
// 10行4列イメージの二次元配列
object[,] setValue = new object[9, 4];
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 4; j++)
{
setValue[i, j] = string.Format("{0}行{1}列目", i + 1, j + 1);
}
}
#endregion
// 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();
// 既存のBookを開く
xlBooks = xlApp.Workbooks;
xlBook = xlBooks.Open(System.IO.Path.GetFullPath(@"..\..\..\data\work01.xlsx"));
// シートを選択する
xlSheets = xlBook.Worksheets;
// 1シート目を操作対象に設定する
// ※Worksheets[n]はオブジェクト型を返すため、Worksheet型にキャスト
xlSheet = xlSheets[1] as Microsoft.Office.Interop.Excel.Worksheet;
// 表示
xlApp.Visible = true;
int iColCnt = 0; //列数カウンタ
int iRowCnt = 0; //行数カウンタ
Microsoft.Office.Interop.Excel.Range xlCellsFrom = null; //セル始点(中継用)
Microsoft.Office.Interop.Excel.Range xlRangeFrom = null; //セル始点
Microsoft.Office.Interop.Excel.Range xlCellsTo = null; //セル終点(中継用)
Microsoft.Office.Interop.Excel.Range xlRangeTo = null; //セル終点
Microsoft.Office.Interop.Excel.Range xlTarget = null; //配列設定対象レンジ
//配列の要素数取得
iRowCnt = setValue.GetLength(0) - 1;
iColCnt = setValue.GetLength(1) - 1;
// 貼り付け位置
int col = 4;
int row = 5;
//始点セル取得
xlCellsFrom = xlSheet.Cells;
xlRangeFrom = xlCellsFrom[row, col] as Microsoft.Office.Interop.Excel.Range;
//終点セル取得
xlCellsTo = xlSheet.Cells;
xlRangeTo = xlCellsTo[row + iRowCnt, col + iColCnt] as Microsoft.Office.Interop.Excel.Range;
//貼り付け対象レンジ
xlTarget = xlSheet.Range[xlRangeFrom, xlRangeTo];
//◆値設定◆
xlTarget.Value = setValue;
// ■■■以下、COMオブジェクトの解放■■■
// cell解放
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlTarget);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlRangeTo);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlCellsTo);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlRangeFrom);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlCellsFrom);
// 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操作用オブジェクト
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();
// 既存のBookを開く
xlBooks = xlApp.Workbooks;
xlBook = xlBooks.Open(System.IO.Path.GetFullPath(@"..\..\..\data\work01.xlsx"));
// シートを選択する
xlSheets = xlBook.Worksheets;
// 1シート目を操作対象に設定する
// ※Worksheets[n]はオブジェクト型を返すため、Worksheet型にキャスト
xlSheet = xlSheets[1] as Microsoft.Office.Interop.Excel.Worksheet;
// 表示
xlApp.Visible = true;
// セルのオブジェクト
Microsoft.Office.Interop.Excel.Range xlRange = null;
Microsoft.Office.Interop.Excel.Range xlCells = null;
// B3セルを指定
xlCells = xlSheet.Cells;
xlRange = xlCells[3, 2] as Microsoft.Office.Interop.Excel.Range;
// ◆現在の値を表示◆
MessageBox.Show(xlRange.Value);
// ◆値を設定◆
xlRange.Value = "変更後の値";
// 変更後の値を表示
MessageBox.Show(xlRange.Value);
// ■■■以下、COMオブジェクトの解放■■■
// cell解放
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlRange);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlCells);
// 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);