C#でExcelを扱う場合、参照にCOMオブジェクトを追加すれば使うことができるが、開発環境のExcelのバージョンに依存してしまうため、環境が異なる場合は動作しなくなる。
この問題は、Type.GetTypeFromProgID()でクラスタイプを解決し、Activator.CreateInstance()でインスタンスを作る、という方法で解決できる。
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace temp
{
// Excel Wrapper Class
public class ExcelWrapper : IDisposable
{
const int PARAM_NUM_1 = 1; // パラメータ数(1)
const int PARAM_NUM_2 = 2; // パラメータ数(2)
const int PARAM_NUM_3 = 3; // パラメータ数(3)
const int PARAM_NUM_4 = 4; // パラメータ数(4)
const int PARAM_NUM_5 = 5; // パラメータ数(5)
const int PARAM_NUM_6 = 6; // パラメータ数(6)
const int PARAM_NUM_7 = 7; // パラメータ数(7)
const int PARAM_NUM_8 = 8; // パラメータ数(8)
const int PARAM_NUM_9 = 9; // パラメータ数(9)
const int PARAM_NUM_10 = 10; // パラメータ数(10)
const int PARAM_NUM_11 = 11; // パラメータ数(11)
const int PARAM_NUM_12 = 12; // パラメータ数(12)
const int PARAM_NUM_13 = 13; // パラメータ数(13)
const int PARAM_NUM_14 = 14; // パラメータ数(14)
const int PARAM_NUM_15 = 15; // パラメータ数(15)
const int PARAM_NUM_16 = 16; // パラメータ数(16)
const int PARAM_NUM_17 = 17; // パラメータ数(17)
const int PARAM_NUM_18 = 18; // パラメータ数(18)
const int PARAM_NUM_19 = 19; // パラメータ数(19)
const int PARAM_NUM_20 = 20; // パラメータ数(20)
const int PARAM_NUM_21 = 21; // パラメータ数(21)
const int PARAM_NUM_22 = 22; // パラメータ数(22)
const int PARAM_NUM_23 = 23; // パラメータ数(23)
const int PARAM_NUM_24 = 24; // パラメータ数(24)
const int PARAM_NUM_25 = 25; // パラメータ数(25)
const int PARAM_NUM_26 = 26; // パラメータ数(26)
const int PARAM_NUM_27 = 27; // パラメータ数(27)
const int PARAM_NUM_28 = 28; // パラメータ数(28)
const int PARAM_NUM_29 = 29; // パラメータ数(29)
const int PARAM_NUM_30 = 30; // パラメータ数(30)
const int PARAM_NUM_31 = 31; // パラメータ数(31)
const int PARAM_NUM_32 = 32; // パラメータ数(32)
const int PARAM_NUM_33 = 33; // パラメータ数(33)
const int PARAM_NUM_34 = 34; // パラメータ数(34)
const int PARAM_NUM_35 = 35; // パラメータ数(35)
const int PARAM_NUM_36 = 36; // パラメータ数(36)
// Excelアプリケーションオブジェクト
private object xlsApplication = null;
// Workbooksオブジェクト
private object xlsBooks = null;
// Excelアプリケーションオブジェクト
protected object XlsApplication
{
get
{
// 存在しない場合は作成する
if (xlsApplication == null)
{
Type classType = Type.GetTypeFromProgID("Excel.Application");
xlsApplication = Activator.CreateInstance(classType);
}
return xlsApplication;
}
}
// バージョン
public string Version
{
get
{
object versionObj = XlsApplication.GetType().InvokeMember("Version", BindingFlags.GetProperty, null, XlsApplication, null);
return versionObj.ToString();
}
}
// 保存時の確認メッセージ有効
public bool DisplayAlerts
{
set
{
object[] parameters = new object[PARAM_NUM_1];
parameters[0] = value;
XlsApplication.GetType().InvokeMember("DisplayAlerts", BindingFlags.SetProperty, null, XlsApplication, parameters);
}
}
// アプリケーション可視
public bool Visible
{
set
{
object[] parameters = new object[PARAM_NUM_1];
parameters[0] = value;
XlsApplication.GetType().InvokeMember("Visible", BindingFlags.SetProperty, null, XlsApplication, parameters);
}
}
// Workbooksオブジェクト
protected object Workbooks
{
get
{
if (xlsBooks == null)
{
xlsBooks = XlsApplication.GetType().InvokeMember("Workbooks", BindingFlags.GetProperty, null, XlsApplication, null);
}
return xlsBooks;
}
}
// 破棄
public void Dispose()
{
ReleaseComObject(xlsBooks);
xlsBooks = null;
ReleaseComObject(xlsApplication);
xlsApplication = null;
}
// COMオブジェクトのリリース
private static void ReleaseComObject(object target)
{
try
{
if ((target != null))
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(target);
}
}
finally
{
target = null;
}
}
// Bookを取得
private object GetBook(int index)
{
object[] parameters = new object[PARAM_NUM_1];
parameters[0] = index;
return Workbooks.GetType().InvokeMember("Item", BindingFlags.GetProperty, null, Workbooks, parameters);
}
// Sheetsを取得
private object GetSheets(object book)
{
return book.GetType().InvokeMember("Worksheets", BindingFlags.GetProperty, null, book, null);
}
// Sheetを取得
private object GetSheet(object sheets, string sheetName)
{
object[] parameters = new object[PARAM_NUM_1];
parameters[0] = sheetName;
return sheets.GetType().InvokeMember("Item", BindingFlags.GetProperty, null, sheets, parameters);
}
// Rangeを取得
private object GetRange(object sheet, string range)
{
object[] parameters = new Object[PARAM_NUM_2];
parameters[0] = range;
parameters[1] = Type.Missing;
return sheet.GetType().InvokeMember("Range", BindingFlags.GetProperty, null, sheet, parameters);
}
// Cellsを取得
private object GetCells(object sheet)
{
return sheet.GetType().InvokeMember("Cells", BindingFlags.GetProperty, null, sheet, null);
}
// Cellを取得
private object GetCell(object cells, int row, int column)
{
object[] parameters = new Object[PARAM_NUM_2];
parameters[0] = row;
parameters[1] = column;
return cells.GetType().InvokeMember("Item", BindingFlags.GetProperty, null, cells, parameters);
}
private object GetCellText(object cell)
{
return cell.GetType().InvokeMember("Text", BindingFlags.GetProperty, null, cell, null);
}
// Cellのテキストを取得
private void SetCellText(object cell, string value)
{
object[] parameters = new Object[PARAM_NUM_1];
parameters[0] = value;
cell.GetType().InvokeMember("Value", BindingFlags.SetProperty, null, cell, parameters);
}
// Excelファイルを開く
public void Open(string xlsFilePath)
{
object[] parameters = new object[PARAM_NUM_15];
parameters[0] = xlsFilePath;
parameters[1] = Type.Missing;
parameters[2] = Type.Missing;
parameters[3] = Type.Missing;
parameters[4] = Type.Missing;
parameters[5] = Type.Missing;
parameters[6] = Type.Missing;
parameters[7] = Type.Missing;
parameters[8] = Type.Missing;
parameters[9] = Type.Missing;
parameters[10] = Type.Missing;
parameters[11] = Type.Missing;
parameters[12] = Type.Missing;
parameters[13] = Type.Missing;
parameters[14] = Type.Missing;
Workbooks.GetType().InvokeMember("Open", BindingFlags.InvokeMethod, null, Workbooks, parameters);
}
// マクロを起動
public void Run(string bookName, string methodName)
{
object[] parameters = new object[PARAM_NUM_31];
parameters[0] = "'" + bookName + "'!" + methodName;
parameters[1] = Type.Missing;
parameters[2] = Type.Missing;
parameters[3] = Type.Missing;
parameters[4] = Type.Missing;
parameters[5] = Type.Missing;
parameters[6] = Type.Missing;
parameters[7] = Type.Missing;
parameters[8] = Type.Missing;
parameters[9] = Type.Missing;
parameters[10] = Type.Missing;
parameters[11] = Type.Missing;
parameters[12] = Type.Missing;
parameters[13] = Type.Missing;
parameters[14] = Type.Missing;
parameters[15] = Type.Missing;
parameters[16] = Type.Missing;
parameters[17] = Type.Missing;
parameters[18] = Type.Missing;
parameters[19] = Type.Missing;
parameters[20] = Type.Missing;
parameters[21] = Type.Missing;
parameters[22] = Type.Missing;
parameters[23] = Type.Missing;
parameters[24] = Type.Missing;
parameters[25] = Type.Missing;
parameters[26] = Type.Missing;
parameters[27] = Type.Missing;
parameters[28] = Type.Missing;
parameters[29] = Type.Missing;
parameters[30] = Type.Missing;
XlsApplication.GetType().InvokeMember("Run", BindingFlags.InvokeMethod, null, XlsApplication, parameters);
}
// マクロを起動
public void Run(string bookName, string methodName, string param1, string param2)
{
object[] parameters = new object[PARAM_NUM_31];
parameters[0] = "'" + bookName + "'!" + methodName;
parameters[1] = param1;
parameters[2] = param2;
parameters[3] = Type.Missing;
parameters[4] = Type.Missing;
parameters[5] = Type.Missing;
parameters[6] = Type.Missing;
parameters[7] = Type.Missing;
parameters[8] = Type.Missing;
parameters[9] = Type.Missing;
parameters[10] = Type.Missing;
parameters[11] = Type.Missing;
parameters[12] = Type.Missing;
parameters[13] = Type.Missing;
parameters[14] = Type.Missing;
parameters[15] = Type.Missing;
parameters[16] = Type.Missing;
parameters[17] = Type.Missing;
parameters[18] = Type.Missing;
parameters[19] = Type.Missing;
parameters[20] = Type.Missing;
parameters[21] = Type.Missing;
parameters[22] = Type.Missing;
parameters[23] = Type.Missing;
parameters[24] = Type.Missing;
parameters[25] = Type.Missing;
parameters[26] = Type.Missing;
parameters[27] = Type.Missing;
parameters[28] = Type.Missing;
parameters[29] = Type.Missing;
parameters[30] = Type.Missing;
XlsApplication.GetType().InvokeMember("Run", BindingFlags.InvokeMethod, null, XlsApplication, parameters);
}
// Excel終了
public void Quit()
{
XlsApplication.GetType().InvokeMember("Quit", BindingFlags.InvokeMethod, null, XlsApplication, null);
}
// 保存
public void Save(int bookIndex)
{
object book = null;
try
{
// WorkbooksオブジェクトからBookオブジェクトを取得
book = GetBook(bookIndex);
// ブックを保存
book.GetType().InvokeMember("Save", BindingFlags.InvokeMethod, null, book, null);
}
finally
{
ReleaseComObject(book);
}
}
// Cellの値を文字列で取得
public string GetCellValue(int bookIndex, string sheetName, int row, int column)
{
object book = null;
object sheets = null;
object sheet = null;
object cells = null;
object cell = null;
try
{
// WorkbooksオブジェクトからBookオブジェクトを取得
book = GetBook(bookIndex);
// BookオブジェクトからSheetsオブジェクトを取得
sheets = GetSheets(book);
// SheetsオブジェクトからSheetオブジェクトを取得
sheet = GetSheet(sheets, sheetName);
// SheetオブジェクトからCellsオブジェクトを取得
cells = GetCells(sheet);
// CellsオブジェクトからCellオブジェクトを取得
cell = GetCell(cells, row, column);
// CellオブジェクトからTextを取得
object text = GetCellText(cell);
return text.ToString();
}
finally
{
ReleaseComObject(cell);
ReleaseComObject(cells);
ReleaseComObject(sheet);
ReleaseComObject(sheets);
ReleaseComObject(book);
}
}
// Cellに値を設定
public void SetCellValue(int bookIndex, string sheetName, int row, int column, string value)
{
object book = null;
object sheets = null;
object sheet = null;
object cells = null;
object cell = null;
try
{
// WorkbooksオブジェクトからBookオブジェクトを取得
book = GetBook(bookIndex);
// BookオブジェクトからSheetsオブジェクトを取得
sheets = GetSheets(book);
// SheetsオブジェクトからSheetオブジェクトを取得
sheet = GetSheet(sheets, sheetName);
// SheetオブジェクトからCellsオブジェクトを取得
cells = GetCells(sheet);
// CellsオブジェクトからCellオブジェクトを取得
cell = GetCell(cells, row, column);
// Cellオブジェクトに値を設定
SetCellText(cell, value);
}
finally
{
ReleaseComObject(cell);
ReleaseComObject(cells);
ReleaseComObject(sheet);
ReleaseComObject(sheets);
ReleaseComObject(book);
}
}
// 最終行のIndexを取得
public int GetLastRowIndex(int bookIndex, string sheetName)
{
object book = null;
object sheets = null;
object sheet = null;
object cells = null;
object specialCells = null;
try
{
// WorkbooksオブジェクトからBookオブジェクトを取得
book = GetBook(bookIndex);
// BookオブジェクトからSheetsオブジェクトを取得
sheets = GetSheets(book);
// SheetsオブジェクトからSheetオブジェクトを取得
sheet = GetSheet(sheets, sheetName);
// SheetオブジェクトからCellsオブジェクトを取得
cells = GetCells(sheet);
// Cellsオブジェクトから最終行Indexを取得
object[] parameters = new object[PARAM_NUM_2];
parameters[0] = 11; //xlCellTypeLastCell
parameters[1] = Type.Missing;
specialCells = cells.GetType().InvokeMember("SpecialCells", BindingFlags.InvokeMethod, null, cells, parameters);
return (int)specialCells.GetType().InvokeMember("Row", BindingFlags.GetProperty, null, specialCells, null);
}
finally
{
ReleaseComObject(specialCells);
ReleaseComObject(cells);
ReleaseComObject(sheet);
ReleaseComObject(sheets);
ReleaseComObject(book);
}
}
// 最終列のIndexを取得
public int GetLastColumnIndex(int bookIndex, string sheetName)
{
object book = null;
object sheets = null;
object sheet = null;
object cells = null;
object specialCells = null;
try
{
// WorkbooksオブジェクトからBookオブジェクトを取得
book = GetBook(bookIndex);
// BookオブジェクトからSheetsオブジェクトを取得
sheets = GetSheets(book);
// SheetsオブジェクトからSheetオブジェクトを取得
sheet = GetSheet(sheets, sheetName);
// SheetオブジェクトからCellsオブジェクトを取得
cells = GetCells(sheet);
// Cellsオブジェクトから最終行Indexを取得
object[] parameters = new object[PARAM_NUM_2];
parameters[0] = 11; //xlCellTypeLastCell
parameters[1] = Type.Missing;
specialCells = cells.GetType().InvokeMember("SpecialCells", BindingFlags.InvokeMethod, null, cells, parameters);
return (int)specialCells.GetType().InvokeMember("Column", BindingFlags.GetProperty, null, specialCells, null);
}
finally
{
ReleaseComObject(specialCells);
ReleaseComObject(cells);
ReleaseComObject(sheet);
ReleaseComObject(sheets);
ReleaseComObject(book);
}
}
}
}