Report Excel
Report yang dibuat menggunakan
Crystal Report kadangkala dirasa tidak flexible, karena pengguna kebanyakan
lebih familiar dengan Microsoft Office, Word atau Excel. Pengguna ingin report
mereka langsung terbentuk dalam format Microsoft Excel sehingga dapat
diubah-ubah setiap waktu sesuai kebutuhan dan memiliki bentuk yang tepat
seperti yang mereka inginkan.
Untuk membuat Report menggunakan
Microsoft Excel, program yang dibuat harus dapat memanggil dan mengendalikan
Microsoft Excel. Untuk melakukan hal ini program harus diberi tambahan
Reference Library Microsoft Excel maupun Microsoft Office, caranya klik menu
Project – Add Reference…
Setelah muncul Dialog Add
Reference, pilih library Microsoft Excel Object Library dan Microsoft Office
Object Library, pilih versi yang paling baru. Klik OK.
Setelah Reference ditambahkan
dapat dimulai penulisan kode program. Pada bagian teratas (imports), ketikkan
Imports Excel, apabila tidak ada ketikkan Imports excel = Microsoft.Office.Interop.Excel.
Kemudian kode program untuk memanggil dan mengendalikan Microsoft Excel dapat
dicoba dan dilihat pada fasilitas Macro Microsoft Excel (VBA / Visual Basic
Application), kode program selengkapnya di bawah ini :
Imports System.IO
Imports Excel
'Imports excel = Microsoft.Office.Interop.Excel
Private Sub bExcel_Click(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles bExcel.Click
Dim xlApp As
Excel.Application
Dim
xlWorkbook As Excel.Workbook
Dim
xlWorksheet As Excel.Worksheet
Dim
misValue As Object
= System.Reflection.Missing.Value
xlApp = New
Excel.Application
'xlWorkbook =
xlApp.Workbooks.Open("c:\Data.xls", misValue, misValue, misValue,
misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue,
misValue, misValue, misValue)
xlWorkbook =
xlApp.Workbooks.Add(misValue)
xlWorksheet = xlWorkbook.Sheets("Sheet1")
xlWorksheet.Columns("A:A").ColumnWidth = 3.86
xlWorksheet.Columns("B:B").ColumnWidth = 22.29
xlWorksheet.Columns("C:C").ColumnWidth = 28.86
xlWorksheet.Columns("D:D").ColumnWidth = 16.71
xlWorksheet.Range("C3").FormulaR1C1 = "Contact List"
With
xlWorksheet.Range("C3").Characters(Start:=1,
Length:=12).Font
.Name = "Arial"
.FontStyle = "Bold"
.Size = 14
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline =
Excel.XlUnderlineStyle.xlUnderlineStyleNone
.ColorIndex =
Excel.XlColorIndex.xlColorIndexAutomatic
End With
xlWorksheet.Range("B5").FormulaR1C1
= "Name"
xlWorksheet.Range("C5").FormulaR1C1 = "Address"
xlWorksheet.Range("D5").FormulaR1C1 = "Phone"
With
xlWorksheet.Range("B5:D5").Interior
.ColorIndex = 15
.Pattern =
Excel.XlPattern.xlPatternSolid
.PatternColorIndex =
Excel.XlColorIndex.xlColorIndexAutomatic
End With
Dim i As Integer
For i =
0 To (bs.Count - 1)
xlWorksheet.Range("B" & (i + 6).ToString).FormulaR1C1 =
bs.Item(i)("name").ToString
xlWorksheet.Range("C" & (i + 6).ToString).FormulaR1C1 =
bs.Item(i)("address").ToString
xlWorksheet.Range("D" & (i + 6).ToString).FormulaR1C1 =
bs.Item(i)("phone").ToString
Next
With
xlWorksheet.Range("B5:D" &
(bs.Count + 5).ToString).Borders(XlBordersIndex.xlEdgeLeft)
.LineStyle =
XlLineStyle.xlContinuous
.Weight = XlBorderWeight.xlThin
.ColorIndex =
XlColorIndex.xlColorIndexAutomatic
End With
With xlWorksheet.Range("B5:D"
& (bs.Count + 5).ToString).Borders(XlBordersIndex.xlEdgeRight)
.LineStyle =
XlLineStyle.xlContinuous
.Weight = XlBorderWeight.xlThin
.ColorIndex =
XlColorIndex.xlColorIndexAutomatic
End With
With
xlWorksheet.Range("B5:D" &
(bs.Count + 5).ToString).Borders(XlBordersIndex.xlEdgeTop)
.LineStyle =
XlLineStyle.xlContinuous
.Weight = XlBorderWeight.xlThin
.ColorIndex =
XlColorIndex.xlColorIndexAutomatic
End With
With
xlWorksheet.Range("B5:D" &
(bs.Count + 5).ToString).Borders(XlBordersIndex.xlEdgeBottom)
.LineStyle =
XlLineStyle.xlContinuous
.Weight = XlBorderWeight.xlThin
.ColorIndex = XlColorIndex.xlColorIndexAutomatic
End With
With
xlWorksheet.Range("B5:D" &
(bs.Count + 5).ToString).Borders(XlBordersIndex.xlInsideHorizontal)
.LineStyle =
XlLineStyle.xlContinuous
.Weight = XlBorderWeight.xlThin
.ColorIndex =
XlColorIndex.xlColorIndexAutomatic
End With
With
xlWorksheet.Range("B5:D" &
(bs.Count + 5).ToString).Borders(XlBordersIndex.xlInsideVertical)
.LineStyle =
XlLineStyle.xlContinuous
.Weight = XlBorderWeight.xlThin
.ColorIndex =
XlColorIndex.xlColorIndexAutomatic
End With
xlApp.Visible = True
'xlWorkbook.Save()
Try
xlWorksheet.SaveAs(Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath)
& "\Report.xls")
Catch
ex As Exception
MsgBox("Report
Masih Terbuka", MsgBoxStyle.Exclamation Or
MsgBoxStyle.OkOnly, "Konfirmasi")
End Try
'xlWorkbook.Close()
'xlApp.Quit()
End Sub
Klik
menu Debug – Start Debugging (F5) untuk menjalankan program, kemudian klik
Tombol untuk mencoba penggunaan Microsoft Excel dari kode program Visual
Basic.NET
Microsoft
Excel akan dipanggil dan ditulisi report di dalamnya sesuai isi database dan
dengan format / style sesuai kode program yang dituliskan. Document Excel
tersebut secara otomatis juga akan tersimpan pada file Report.xls.