aptana

vbtoxlsx

Aug 4th, 2018
1,254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.48 KB | None | 0 0
  1. Imports System.Data.OleDb
  2. Public Class Form1
  3.     Dim Conn As OleDbConnection
  4.     Dim cmd As OleDbCommand
  5.     Dim da As OleDbDataAdapter
  6.     Dim ds As New DataSet
  7.     Dim dr As OleDbDataReader
  8.     Dim LokasiDB As String
  9.     Sub Koneksi()
  10.         LokasiDB = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=dbjnm.accdb"
  11.         Conn = New OleDbConnection(LokasiDB)
  12.         If Conn.State = ConnectionState.Closed Then Conn.Open()
  13.     End Sub
  14.  
  15.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  16.         Koneksi()
  17.         da = New OleDbDataAdapter("Select * from barang", Conn)
  18.         ds = New DataSet
  19.         ds.Clear()
  20.         da.Fill(ds, "barang")
  21.         DataGridView1.DataSource = (ds.Tables("barang"))
  22.         DataGridView1.Enabled = True
  23.         Conn.Close()
  24.     End Sub
  25.     Sub excel()
  26.         Dim xlApp As Microsoft.Office.Interop.Excel.Application
  27.         Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook
  28.         Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
  29.         Dim misValue As Object = System.Reflection.Missing.Value
  30.         Dim i As Integer
  31.         Dim j As Integer
  32.  
  33.         xlApp = New Microsoft.Office.Interop.Excel.ApplicationClass
  34.         xlWorkBook = xlApp.Workbooks.Add(misValue)
  35.         xlWorkSheet = xlWorkBook.Sheets("sheet1")
  36.  
  37.  
  38.         For i = 0 To DataGridView1.RowCount - 2
  39.             For j = 0 To DataGridView1.ColumnCount - 1
  40.                 For k As Integer = 1 To DataGridView1.Columns.Count
  41.                     xlWorkSheet.Cells(1, k) = DataGridView1.Columns(k - 1).HeaderText
  42.                     xlWorkSheet.Cells(i + 2, j + 1) = DataGridView1(j, i).Value.ToString()
  43.                 Next
  44.             Next
  45.         Next
  46.         SaveFileDialog1.Filter = "XLSX Files (*.xlsx)|*.xlsx"
  47.         If SaveFileDialog1.ShowDialog = System.Windows.Forms.DialogResult.OK Then xlWorkSheet.SaveAs(SaveFileDialog1.FileName)
  48.         xlWorkBook.Close()
  49.         xlApp.Quit()
  50.  
  51.         releaseObject(xlApp)
  52.         releaseObject(xlWorkBook)
  53.         releaseObject(xlWorkSheet)
  54.  
  55.     End Sub
  56.     Private Sub releaseObject(ByVal obj As Object)
  57.         Try
  58.             System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
  59.             obj = Nothing
  60.         Catch ex As Exception
  61.             obj = Nothing
  62.         Finally
  63.             GC.Collect()
  64.         End Try
  65.  
  66.     End Sub
  67.  
  68.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  69.         excel()
  70.     End Sub
  71. End Class
Advertisement
Add Comment
Please, Sign In to add comment