Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'엑셀 파일 생성할 경로
Dim path As String = "C:\Work\Test.xlsx"
Dim xlApp As Excel.Application = New Microsoft.Office.Interop.Excel.Application()
'workbook == 엑셀 통합 파일이름 / worksheet == 스프레드 시트
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
' Missing클래스
Dim misValue As Object = System.Reflection.Missing.Value
'예제를 위해 간단하게 데이터를 넣었음, 파일만 생성하고 싶은 경우에는 이 부분을 삭제하면됨
'--------------------------------------------------------------------------------------------------------
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")
xlWorkSheet.Cells(1, 1) = "Sheet 1 content"
'--------------------------------------------------------------------------------------------------------
xlWorkBook.SaveAs(path, Excel.XlFileFormat.xlOpenXMLWorkbook, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue)
xlWorkBook.Close(True, misValue, misValue)
xlApp.Quit()
' oledb를 통해 데이터를 삽입하고 싶을 경우 이부분에 코드를 삽입해 주면됩니다.
releaseObject(xlWorkSheet)
releaseObject(xlWorkBook)
releaseObject(xlApp)
MessageBox.Show("완료되었습니다")
End Sub
Private Sub releaseObject(ByVal obj As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
obj = Nothing
Catch ex As Exception
obj = Nothing
Finally
GC.Collect()
End Try
End Sub
End Class
댓글