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
Dim xlapp As New Excel.Application
Dim ra As Excel.Range
On Error Resume Next
xlapp = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
xlapp = New Excel.Application
End If
xlapp.Visible = True
xlapp.Workbooks.Add()
ra = xlapp.ActiveCell
With DataGridView1
For i As Integer = 0 To .RowCount - 1
For j As Integer = 0 To .ColumnCount - 1
ra.Offset(i, j).Value = .Rows(i).Cells(j).Value
Next
Next
End With