VB.NETでExcelを操作してみる
Private Sub Button2_Click(
ByVal sender
As System.
Object,
ByVal e
As System.EventArgs)
Handles Button2.Click
Try Dim objExcel
As Excel.Application =
Nothing Dim objBook1
As Excel.Workbook =
Nothing Dim objSheet1
As Excel.Worksheet =
Nothing objExcel =
CType(CreateObject(
"Excel.Application"), Excel.Application)
objBook1 =
CType(objExcel.Workbooks.Open(
"C:\test.xls"), Excel.Workbook)
objSheet1 =
CType(objBook1.Worksheets(
"Sheet1"), Excel.Worksheet)
objExcel.DisplayAlerts =
False With objSheet1
.Cells(
1,
2) =
"1,2" .Cells(
2,
2) =
"2,2" .Cells(
3,
1) =
"3,1" End With objBook1.Save()
If Not objSheet1
Is Nothing Then System.Runtime.InteropServices.Marshal.ReleaseComObject(objSheet1)
End If If Not objBook1
Is Nothing Then Try objBook1.Close()
Finally System.Runtime.InteropServices.Marshal.ReleaseComObject(objBook1)
End Try End If If Not objExcel
Is Nothing Then Try objExcel.Quit()
Finally System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcel)
End Try End If objSheet1 =
Nothing objBook1 =
Nothing objExcel =
Nothing GC.Collect(
2)
Catch ex
As Exception
MessageBox.Show(ex.ToString)
End Try