Top > .NET > COMでExcelを操作する

※VS2008からOfficeを操作するにはVSTOという手段もあるらしい。
Visual Studio Tools for Officeで作るOfficeアプリケーション(1/5):CodeZine
http://codezine.jp/article/detail/2481

[参照の追加]で[COM]タブの[Microsoft Excel 11.0 Object Library]を追加しておく。

            // Object for missing (or optional) arguments.
	        object oMissing = System.Reflection.Missing.Value;
            
            Excel.ApplicationClass oExcel = new Excel.ApplicationClass();
            Excel.Workbooks oBooks = oExcel.Workbooks;
            Excel._Workbook oBook = null;

            oBook = oBooks.Open("c:\\hoge.xls", oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
            
            //oBook.Sheets
            Excel.Range range = (Excel.Range)((Excel.Worksheet)oBook.Sheets[1]).Cells[1, 1];
            range.Value2 = "この文字列はCOMによって書かれました。" + DateTime.Now.ToString();

            // Quit Excel and clean up.
            oBook.Close(true, oMissing, oMissing);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(oBook);
            oBook = null;
            System.Runtime.InteropServices.Marshal.ReleaseComObject(oBooks);
            oBooks = null;
            oExcel.Quit();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(oExcel);
            oExcel = null;


URL B I U SIZE Black Maroon Green Olive Navy Purple Teal Gray Silver Red Lime Yellow Blue Fuchsia Aqua White

Reload   New Lower page making Edit Freeze Diff Upload Copy Rename   Front page List of pages Search Recent changes Backup Referer   Help   RSS of recent changes