Option Strict On '無関係部分省略 Private Sub ExcelTest() Dim xlApp As Excel.Application Dim xlBooks As Excel.Workbooks Dim xlBook As Excel.Workbook Dim xlSheets As Excel.Sheets Dim xlSheet As Excel.Worksheet Dim xlRange As Excel.Range Dim xlCells As Excel.Range Const csSheetName As String = "Sheet1" Const csPath As String = "E:\Data\Office\Excel\vb2008Test.xls" 'ファイルを削除しておく Try System.IO.File.Delete(csPath) Catch ex As Exception '何もしない End Try xlApp = New Excel.Application xlBooks = xlApp.Workbooks xlBook = xlBooks.Add() My.Application.DoEvents() xlSheets = xlBook.Worksheets xlSheet = DirectCast(xlSheets(csSheetName), Excel.Worksheet) xlCells = DirectCast(xlSheet.Cells, Excel.Range) 'データ入力 For i As Integer = 1 To 5 xlRange = DirectCast(xlCells(i, 1), Excel.Range) xlRange.Value = i * 100 ReleaseComObject(xlRange) Next xlApp.Visible = True xlBook.SaveAs(csPath) xlBook.Close(False) 'xlBook を閉じる '開放処理 ReleaseComObject(xlCells) 'xlCells の解放 xlCells = Nothing ReleaseComObject(xlSheet) 'xlSheet の解放 xlSheet = Nothing ReleaseComObject(xlSheets) 'xlSheets の解放 xlSheets = Nothing ReleaseComObject(xlBook) 'xlBook の解放 xlBook = Nothing ReleaseComObject(xlBooks) 'xlBooks の解放 xlBooks = Nothing xlApp.Quit() 'Excel終了 ReleaseComObject(xlApp) 'xlApp を解放 xlApp = Nothing End Sub