Dim xlApp As New Microsoft.Office.Interop.Excel.Application
Dim xlSheet1 As Microsoft.Office.Interop.Excel.Worksheet
Dim xlBook As Microsoft.Office.Interop.Excel.Workbook
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports System.IO
Imports Excel = Microsoft.Office.Interop.Excel
Namespace ExcelC
Public Partial Class frmExcel
Inherits Form
Public Sub New()
InitializeComponent()
End Sub
Private Sub btnExcel_Click(sender As Object, e As EventArgs)
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim mis As Object = Type.Missing
Dim strFileName As String = "C:\ExcelC\Xls\GenExcel.xls"
xlApp = New Excel.ApplicationClass()
xlWorkBook = xlApp.Workbooks.Add(mis)
'xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.Add(mis, mis, mis, mis); /*** for Add New Sheet ***/
xlWorkSheet = DirectCast(xlWorkBook.Worksheets.get_Item(1), Excel.Worksheet)
xlWorkSheet.Name = "My Sheet"
xlWorkSheet.Cells(1, 1) = "www.ThaiCreate.Com"
xlWorkSheet.Cells(2, 1) = "Mr.Weerachai Nukitram"
xlWorkBook.SaveAs(strFileName, mis, mis, mis, mis, mis, _
Excel.XlSaveAsAccessMode.xlExclusive, mis, mis, mis, mis, mis)
xlWorkBook.Close(True, mis, mis)
xlApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook)
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkSheet)
xlApp = Nothing
xlWorkBook = Nothing
xlWorkSheet = Nothing
GC.Collect()
MessageBox.Show("Generate Successfully")
End Sub
End Class
End Namespace