Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.Data.OleDb
- Public Class Form1
- Dim Conn As OleDbConnection
- Dim cmd As OleDbCommand
- Dim da As OleDbDataAdapter
- Dim ds As New DataSet
- Dim dr As OleDbDataReader
- Dim LokasiDB As String
- Sub Koneksi()
- LokasiDB = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=dbjnm.accdb"
- Conn = New OleDbConnection(LokasiDB)
- If Conn.State = ConnectionState.Closed Then Conn.Open()
- End Sub
- Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- Koneksi()
- da = New OleDbDataAdapter("Select * from barang", Conn)
- ds = New DataSet
- ds.Clear()
- da.Fill(ds, "barang")
- DataGridView1.DataSource = (ds.Tables("barang"))
- DataGridView1.Enabled = True
- Conn.Close()
- End Sub
- Sub excel()
- Dim xlApp As Microsoft.Office.Interop.Excel.Application
- Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook
- Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
- Dim misValue As Object = System.Reflection.Missing.Value
- Dim i As Integer
- Dim j As Integer
- xlApp = New Microsoft.Office.Interop.Excel.ApplicationClass
- xlWorkBook = xlApp.Workbooks.Add(misValue)
- xlWorkSheet = xlWorkBook.Sheets("sheet1")
- For i = 0 To DataGridView1.RowCount - 2
- For j = 0 To DataGridView1.ColumnCount - 1
- For k As Integer = 1 To DataGridView1.Columns.Count
- xlWorkSheet.Cells(1, k) = DataGridView1.Columns(k - 1).HeaderText
- xlWorkSheet.Cells(i + 2, j + 1) = DataGridView1(j, i).Value.ToString()
- Next
- Next
- Next
- SaveFileDialog1.Filter = "XLSX Files (*.xlsx)|*.xlsx"
- If SaveFileDialog1.ShowDialog = System.Windows.Forms.DialogResult.OK Then xlWorkSheet.SaveAs(SaveFileDialog1.FileName)
- xlWorkBook.Close()
- xlApp.Quit()
- releaseObject(xlApp)
- releaseObject(xlWorkBook)
- releaseObject(xlWorkSheet)
- 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
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- excel()
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment