Imports System.Data
Imports System.Data.OleDb
Imports System.IO
Imports System.Data.SqlClient
Imports Excel = Microsoft.Office.Interop.Excel
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
Dim i As Integer
Dim j As Integer
xlApp = New Excel.Application
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")
For i = 0 To DataGridView.RowCount - 2
For j = 0 To DataGridView.ColumnCount - 1
xlWorkSheet.Cells(i + 1, j + 1) = _
DataGridView(j, i).Value.ToString()
Next
Next
Dim ExcelName As String 'ตั้งชื่อให้ file excel
ExcelName = txtExcelName.Text
xlWorkSheet.SaveAs("D:\file vb excel\" & ExcelName & ".xlsx")
xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
MsgBox("You can find the file D:\file vb excel\" & ExcelName & ".xlsx")
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
ช่วยผมหน่อยนะคับ..
Tag : .NET, Ms Access, Excel (Excel.Application), VS 2010 (.NET 4.x)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
Dim i As Integer
Dim j As Integer
xlApp = New Excel.Application
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")
For j = 0 To DataGridView1.ColumnCount - 1
xlWorkSheet.Cells(1, j + 1) = DataGridView1.Columns(j).HeaderText.ToString()
Next
For i = 1 To DataGridView1.RowCount - 2
For j = 0 To DataGridView1.ColumnCount - 1
xlWorkSheet.Cells(i + 1, j + 1) = DataGridView1(j, i - 1).Value.ToString()
Next
Next
Dim ExcelName As String 'ตั้งชื่อให้ file excel
ExcelName = "test"
xlWorkSheet.SaveAs("D:\" & ExcelName & ".xlsx")
xlWorkBook.Close()
xlApp.Quit()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
Dim i As Integer
Dim j As Integer
xlApp = New Excel.Application
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")
For j = 0 To DataGridView1.ColumnCount - 1
xlWorkSheet.Cells(1, j + 1) = DataGridView1.Columns(j).HeaderText.ToString()
Next
For i = 1 To DataGridView1.RowCount - 2
For j = 0 To DataGridView1.ColumnCount - 1
xlWorkSheet.Cells(i + 1, j + 1) = DataGridView1(j, i).Value.ToString() 'เดิมมันเป็น (j,i-1)
Next
Next
Dim ExcelName As String 'ตั้งชื่อให้ file excel
ExcelName = "test"
xlWorkSheet.SaveAs("D:\" & ExcelName & ".xlsx")
xlWorkBook.Close()
xlApp.Quit()
End Sub