« 的を得る:× | メイン | .NETでDoEvents »
2006年7月24日
C#.NETでExcelファイル(XP2003)を操作
1.参照の追加で「COM」から「Microsoft Excel xx.x Object Library」を追加。
2.using 句に「using Excel = Microsoft.Office.Interop.Excel;」を追加。コーディング省力化のため
3.既存のExcelファイルを開くサンプル
Type.Missingはパラメータを省略できる場合、VBは単純にパラメータを書かなければいいけど、C#の場合にはこれを指定すると省略されたことになる。
Excel.Application objExcelApp = new Excel.Application();
Excel.Workbooks objExcelBooks = (Excel.Workbooks)ExcelApp.Workbooks;
// 既存ファイルオープン
objExcelBooks.Open("C:\\TEMP\\SAMPLE.XLS",
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing);
// Excelファイルを非表示にする
objExcelApp.Visible = true;
// ここにExcelファイルの操作を記述
// 操作開始
// 操作終了
// 終了
objExcelApp.Quit();
// COMオブジェクト解放
System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelBooks);
System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelApp);
投稿者 danna : 2006年7月24日 10:05
トラックバック
このエントリーのトラックバックURL:
http://www.namibuta.net/cgi-bin/mt/mt-tb.cgi/443