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 ToolStripLabel2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripLabel2.Click
koneksi()
Try
Dim i, j As Integer
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
xlApp = New Microsoft.Office.Interop.Excel.ApplicationClass
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")
Dim sql As String = "SELECT * FROM dbo.komputer"
Dim dscmd As New SqlDataAdapter(sql, conn.ConnectionString)
dscmd.Fill(ds)
xlWorkSheet.Cells(1, 1).Value = "No_alat"
xlWorkSheet.Cells(1, 2).Value = "Merk"
xlWorkSheet.Cells(1, 3).Value = "Jenis"
xlWorkSheet.Cells(1, 4).Value = "Spesifikasi"
xlWorkSheet.Cells(1, 5).Value = "Lokasi"
xlWorkSheet.Cells(1, 6).value = "Kondisi"
For i = 0 To ds.Tables(0).Rows.Count - 1
'Column
For j = 0 To ds.Tables(0).Columns.Count - 1
' this i change to header line cells >>>
xlWorkSheet.Cells(i + 3, j + 1) = _
ds.Tables(0).Rows(i).Item(j)
Next
Next
Dim myStream As Stream
Dim saveFileDialog1 As New SaveFileDialog()
'berfungsi untuk kita mau menyimpan data nya di direktori mana...hheeeu
If saveFileDialog1.Filter = " txt files (*.xlsx)|*.xlsx|All files (*.*)|*.*" Then
myStream = saveFileDialog1.OpenFile()
If (myStream IsNot Nothing) Then
' Code to write the stream goes here.
myStream.Close()
Else
MsgBox("Silahkan di baca")
End If
Else
End If
xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
Catch
MsgBox(e.ToString())
End Try
End sub
import system.data.sqlcient
Public Sub koneksi() ....... Cara membuat koneksi ()
Try
conn = New SqlClient.SqlConnection("Data Source=DAGOENG-24\SQLEXPRESS;Initial Catalog=db_kantor;Integrated Security=True")
conn.Open()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
klik export to ms excel
Tidak ada komentar:
Posting Komentar