Dalam pemrograman basisdata, sebagai programmer, kita dituntut untuk dapat menghasilkan output berupa laporan. Cara menghasilkan laporan tersebut tentunya berbeda-beda. Salah satu cara menghasilkan output laporan adalah menampilkan laporan tersebut ke excel.
Berikut ini diuraikan langkah-langkah pembuatan output excel sederhana dengan menggunakan Visual Basic 6.0 :
Berikut ini diuraikan langkah-langkah pembuatan output excel sederhana dengan menggunakan Visual Basic 6.0 :
- Buat satu project baru pada visual basic 6.0
- Tambahkan 1 CommandButton pada form dan beri nama Command1
- Buka code viewer pada form yang telah disediakan CommandButton tadi Deklarasikan object untuk menghasilkan file excel
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object - Double Click pada CommandButton tadi untuk menambahkan code pada event click'- Set object yang telah dideklarasikan sebelumnya untuk membuat objek Excel
Set oExcel = CreateObject("Excel.Application")
Set oBook = oExcel.Workbooks.Add
Set oSheet = oBook.Worksheets(1)'- Lakukan pengaturan untuk masing-masing kolom
oExcel.ActiveSheet.Cells(1, 1).ColumnWidth = 10
oExcel.ActiveSheet.Cells(1, 2).ColumnWidth = 25
oExcel.ActiveSheet.Cells(1, 3).ColumnWidth = 16
'- Untuk membuat border pada cell, dapat dilihat pada listing code berikut
'Borders Param -> 1 = left, 2 = right, 3 = top, 4 = bottom
'LineStyle -> 1 = Continuos
oExcel.Range(oExcel.ActiveSheet.Cells(1, 1), oExcel.ActiveSheet.Cells(6, 3)).Borders(1).LineStyle = 1
oExcel.Range(oExcel.ActiveSheet.Cells(1, 1), oExcel.ActiveSheet.Cells(6, 3)).Borders(2).LineStyle = 1
oExcel.Range(oExcel.ActiveSheet.Cells(1, 1), oExcel.ActiveSheet.Cells(6, 3)).Borders(3).LineStyle = 1
oExcel.Range(oExcel.ActiveSheet.Cells(1, 1), oExcel.ActiveSheet.Cells(6, 3)).Borders(4).LineStyle = 1
'- Lakukan pengaturan alignment pada cell
'Set HorizontalAlignment per Cell
oExcel.ActiveSheet.Cells(1, 1).HorizontalAlignment() = 3
oExcel.ActiveSheet.Cells(1, 2).HorizontalAlignment() = 3
oExcel.ActiveSheet.Cells(1, 3).HorizontalAlignment() = 3
'- Pengaturan alignment juga dapat dilakukan sekaligus pada beberapa cell
'Set HorizontalAlignment per Range
oExcel.Range(oExcel.ActiveSheet.Cells(2, 1), oExcel.ActiveSheet.Cells(6, 1)).HorizontalAlignment() = 3
'- Tampilkan data yang ingin dimuat dalam file excel tersebut, data dapat pula diambil dari database
'Set Text into cells
oExcel.ActiveSheet.Cells(1, 1).Value = "Nomor"
oExcel.ActiveSheet.Cells(1, 2).Value = "Nama"
oExcel.ActiveSheet.Cells(1, 3).Value = "Jumlah"
oExcel.ActiveSheet.Cells(2, 1).Value = "1"
oExcel.ActiveSheet.Cells(2, 2).Value = "Nama"
oExcel.ActiveSheet.Cells(2, 3).Value = "Jumlah"
oExcel.ActiveSheet.Cells(3, 1).Value = "2"
oExcel.ActiveSheet.Cells(3, 2).Value = "Nama"
oExcel.ActiveSheet.Cells(3, 3).Value = "Jumlah"
oExcel.ActiveSheet.Cells(4, 1).Value = "3"
oExcel.ActiveSheet.Cells(4, 2).Value = "Nama"
oExcel.ActiveSheet.Cells(4, 3).Value = "Jumlah"
oExcel.ActiveSheet.Cells(5, 1).Value = "4"
oExcel.ActiveSheet.Cells(5, 2).Value = "Nama"
oExcel.ActiveSheet.Cells(5, 3).Value = "Jumlah"
oExcel.ActiveSheet.Cells(6, 1).Value = "5"
oExcel.ActiveSheet.Cells(6, 2).Value = "Nama"
oExcel.ActiveSheet.Cells(6, 3).Value = "Jumlah"
'- Simpan file excel tersebut ke media penyimpanan
'Save Excel File to Drive C
oBook.SaveAs "C:\Book1.xlsx"
'- Tampilkan file excel yang telah dibuat
'Show Excel , and if you wanna hide it, set to false
oExcel.Visible = True
'- Setelah selesai proses pembuatan file, kita dapat keluar dari excel
'Exit from Excel
oExcel.Quit - Lakukan modifikasi sesuai kebutuhan Anda...
Comments
Post a Comment