Monday, May 7, 2007

Excel & Graph Formatting in VB .NET

'**********************************
' Author : Chen Fock Siong
' Date Created : 05-May-2007
'**********************************
Imports Excel

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button()
Me.Button2 = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(16, 16)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(176, 23)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Excel Formatting"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(16, 51)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(176, 23)
Me.Button2.TabIndex = 1
Me.Button2.Text = "Graph Formatting"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(208, 125)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button2, Me.Button1})
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Excel_Exec()
End Sub

Private Function Excel_Exec()
Dim oExcel As New Excel.Application()
Dim oBooks As Excel.Workbooks, oBook As Excel.Workbook
Dim oSheets As Excel.Sheets, oSheet As Excel.Worksheet
Dim oCells As Excel.Range
Dim sFile As String, sTemplate As String

Try
sFile = "D:\ExcelFormatting.xls"
oExcel.Visible = True : oExcel.DisplayAlerts = False
oBooks = oExcel.Workbooks
oBooks.Add()
oBook = oBooks.Item(1)
oSheets = oBook.Worksheets
oSheet = CType(oSheets.Item(1), Excel.Worksheet)
oSheet.Name = "First Sheet"
oCells = oSheet.Cells
FillData(oCells)
oSheet.SaveAs(sFile)
Catch ex As Exception
MessageBox.Show(ex.Message, "Excel Error")
End Try
End Function

Private Function FillData(ByVal oCells As Excel.Range)
Dim sAmt As String
Dim row, col, rowStart, rowEnd As Integer

row = 3 : col = 3 : oCells(row, col) = "Total"
row = 3 : col = 4 : oCells(row, col) = 100
row = 4 : col = 4 : oCells(row, col) = 200

rowStart = 3 : rowEnd = 4
sAmt = "=Sum(D" & rowStart.ToString & ":" & _
"D" & rowEnd.ToString & ")"

row = 6 : col = 4 : oCells(row, col).Formula = sAmt

With oCells(row, col)
With .Borders(XlBordersIndex.xlEdgeTop)
.LineStyle = Excel.XlLineStyle.xlContinuous
.Weight = Excel.XlBorderWeight.xlThin
.ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic
End With

With .Borders(XlBordersIndex.xlEdgeBottom)
.LineStyle = Excel.XlLineStyle.xlDouble
.Weight = Excel.XlBorderWeight.xlThick
.ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic
End With
End With
oCells(row, col).Font.Bold = True
End Function

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim oExcel As New Excel.Application()
Dim oBook As Excel.Workbook
Dim oSheets As Excel.Sheets, oSheet As Excel.Worksheet
Dim oChart As Excel.Chart
Dim sFile As String

Try
sFile = "D:\graph.xls"
oExcel.Visible = True : oExcel.DisplayAlerts = False

oBook = oExcel.Workbooks.Open(sFile)
oSheets = oBook.Worksheets
oSheet = CType(oSheets.Item(1), Excel.Worksheet)
oSheets.Item(2).select()
oChart = oBook.Charts.Add
oChart.ChartType = XlChartType.xlLine
oChart.SetSourceData(oSheets.Item(1).Range("C4:G33"), Excel.XlRowCol.xlColumns)

oChart.ChartArea.Select()
With oChart
.HasTitle = True
.ChartTitle.Characters.Text = "Traffic Volume Vs Time"
.Axes(Excel.XlAxisType.xlValue, Excel.XlAxisGroup.xlPrimary).HasTitle = True
.Axes(Excel.XlAxisType.xlValue, Excel.XlAxisGroup.xlPrimary).AxisTitle.Characters.Text = "Traffic Volume (veh/day)"
.Axes(Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlPrimary).HasTitle = True
.Axes(Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlPrimary).AxisTitle.Characters.Text = "Day"
End With

oChart.SeriesCollection(1).Name = "=""Class 1"""
oChart.SeriesCollection(2).Name = "=""Class 2"""
oChart.SeriesCollection(3).Name = "=""Class 3"""
oChart.SeriesCollection(4).Name = "=""Class 4"""

Catch ex As Exception
MessageBox.Show(ex.Message, "Excel Error")
End Try
End Sub
End Class