Tips kali ini kita akan menampilkan Data Grafik Excel 2007 pada design Form Visual Basic 2008. Hasil tampilan visualnya seperti gambar di atas.
Ikuti langkah-langkah di bawah ini. ToolBox yang digunakan sebagai berikut:
Rangcanglah ToolBox di atas seperti Design Form seperti gambar di bawah ini:
Data Grafik Excel yang akan di tampilkan seperti gambar di bawah ini.
Ketiklah source program di bawah ini, dengan mengarahkan kursor pada design, mengklik kanan lalu pilih View Code.
Source Program:
Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
Try
xlApp = New Excel.ApplicationClass
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")
REM menampilkan data
xlWorkSheet.Cells(1, 1) = ""
xlWorkSheet.Cells(1, 2) = "Tangguh"
xlWorkSheet.Cells(1, 3) = "Prihandono"
xlWorkSheet.Cells(1, 4) = "V. Hutabalian"
xlWorkSheet.Cells(2, 1) = "Semester 1"
xlWorkSheet.Cells(2, 2) = "80"
xlWorkSheet.Cells(2, 3) = "65"
xlWorkSheet.Cells(2, 4) = "45"
xlWorkSheet.Cells(3, 1) = "Semester 2"
xlWorkSheet.Cells(3, 2) = "78"
xlWorkSheet.Cells(3, 3) = "72"
xlWorkSheet.Cells(3, 4) = "60"
xlWorkSheet.Cells(4, 1) = "Semester 3"
xlWorkSheet.Cells(4, 2) = "82"
xlWorkSheet.Cells(4, 3) = "80"
xlWorkSheet.Cells(4, 4) = "65"
xlWorkSheet.Cells(5, 1) = "Semester 4"
xlWorkSheet.Cells(5, 2) = "75"
xlWorkSheet.Cells(5, 3) = "82"
xlWorkSheet.Cells(5, 4) = "68"
REM menampilkan gambar
Dim chartPage As Excel.Chart
Dim xlCharts As Excel.ChartObjects
Dim myChart As Excel.ChartObject
Dim chartRange As Excel.Range
xlCharts = xlWorkSheet.ChartObjects
myChart = xlCharts.Add(10, 80, 300, 250)
chartPage = myChart.Chart
chartRange = xlWorkSheet.Range("A1", "d5")
chartPage.SetSourceData(Source:=chartRange)
chartPage.ChartType = Excel.XlChartType.xlColumnClustered
REM mengekspor gambar ke file di bawah ini
xlWorkSheet.ChartObjects(1).chart.Export (FileName:="C:\Users\Compaq\Documents\excel_chart_export.bmp", FilterName:="BMP")
REM menjalankan gambar di picturebox
PictureBox1.Image = New System.Drawing.Bitmap("C:\Users\Compaq\Documents\excel_chart_export.bmp")
xlWorkSheet.SaveAs("C:\Users\Compaq\Documents\vbexcel.xlsx")
xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
MsgBox("Chart File Exported !")
Catch ex As Exception
MsgBox("Export Excel Error " & ex.Message)
Finally
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default
xlApp = Nothing
End Try
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
End Class
Kemudian tekanlah tombol Tampilkan, maka visual grafik seperti gambar di bawah ini.
0 komentar:
Posting Komentar