- This topic explains, how to create Excel chart in VB.NET PictureBox control.
- How to add Excel Object model to your application: Select Project/Add Reference. Click the COM tab and select “Microsoft Excel 12.0 Object Library” (or whatever version you have on your system).
- Tools needed: Button1 (Button1).
Microsoft Excel – Create Excel Chart in VB.NET PictureBox
- Microsoft Excel is a spreadsheet application developed by Microsoft.
- Microsoft Excel features calculation, graphing tools, pivot tables, and a macro programming language called Visual Basic for Applications.
Application Interface – Create Excel Chart in VB.NET PictureBox
- Application interface represent the entire Microsoft Excel application.
- Syntax for Application Interface for Excel
|
1 2 3 |
'Declaration Public Interface Application Inherits _Application, AppEvents_Event |
Workbook Interface – Create Excel Chart in VB.NET PictureBox
- Workbook interface represent the Microsoft Excel workbook.
- Syntax for Workbook Interface for Excel
|
1 2 3 |
'Declaration Public Interface Workbook Inherits _Workbook, WorkbookEvents_Event |
Worksheet Interface – Create Excel Chart in VB.NET PictureBox
- It represents a Microsoft Excel worksheet.
- Syntax for Worksheet Interface for Excel
|
1 2 3 |
'Declaration Public Interface Worksheet Inherits _Worksheet, DocEvents_Event |
Create Excel Chart in VB.NET PictureBox – Complete Code
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
Imports System.Data Imports ExcelDoc = Microsoft.Office.Interop.Excel Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim xlApp As ExcelDoc.Application Dim xlWorkBook As ExcelDoc.Workbook Dim xlWorkSheet As ExcelDoc.Worksheet Dim misValue As Object = System.Reflection.Missing.Value xlApp = New ExcelDoc.ApplicationClass xlWorkBook = xlApp.Workbooks.Add(misValue) xlWorkSheet = xlWorkBook.Sheets("sheet1") 'add data to spreadsheet xlWorkSheet.Cells(1, 1) = "" xlWorkSheet.Cells(1, 2) = "Student1" xlWorkSheet.Cells(1, 3) = "Student2" xlWorkSheet.Cells(1, 4) = "Student3" xlWorkSheet.Cells(2, 1) = "Term1" xlWorkSheet.Cells(2, 2) = "90" xlWorkSheet.Cells(2, 3) = "85" xlWorkSheet.Cells(2, 4) = "60" xlWorkSheet.Cells(3, 1) = "Term2" xlWorkSheet.Cells(3, 2) = "78" xlWorkSheet.Cells(3, 3) = "65" xlWorkSheet.Cells(3, 4) = "55" xlWorkSheet.Cells(4, 1) = "Term3" xlWorkSheet.Cells(4, 2) = "90" xlWorkSheet.Cells(4, 3) = "80" xlWorkSheet.Cells(4, 4) = "99" xlWorkSheet.Cells(5, 1) = "Term4" xlWorkSheet.Cells(5, 2) = "90" xlWorkSheet.Cells(5, 3) = "93" xlWorkSheet.Cells(5, 4) = "50" 'create chart to spread sheet Dim chart_Page As ExcelDoc.Chart Dim xlCharts As ExcelDoc.ChartObjects Dim myDesChart As ExcelDoc.ChartObject Dim chart_Range As ExcelDoc.Range xlCharts = xlWorkSheet.ChartObjects myDesChart = xlCharts.Add(10, 80, 300, 250) chart_Page = myDesChart.Chart chart_Range = xlWorkSheet.Range("A1", "d5") chart_Page.SetSourceData(Source:=chart_Range) chart_Page.ChartType = ExcelDoc.XlChartType.xlColumnClustered 'exporting chart as picture file xlWorkSheet.ChartObjects(1).chart.Export(FileName:= _ "C:\excel_chart_export.bmp", FilterName:="BMP") 'load the pipcture into the picture box PictureBox1.Image = New System.Drawing.Bitmap _ ("C:\excel_chart_export.bmp") xlWorkSheet.SaveAs("C:\vbexcel.xlsx") xlWorkBook.Close() xlApp.Quit() releaseObject(xlApp) releaseObject(xlWorkBook) releaseObject(xlWorkSheet) 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 |

Create Excel Chart in VB.NET PictureBox
Create Excel Chart in VB.NET PictureBox






