Imports Excel = Microsoft.Office.Interop.Excel
Public Class PupilMainResultsGraphForm
' Create Excel objects
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
' Create Image object
Dim img As Image
Private Sub PupilMainResultsGraphForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Instantiate excel objects
xlApp = New Excel.ApplicationClass
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")
' Create first cell blank
xlWorkSheet.Cells(1, 1) = ""
For i = 0 To ArrayListDates.Count - 1
' Add date headings
xlWorkSheet.Cells(1, i + 2) = "" + ArrayListDates(i).ToString ' "Date" + i.ToString
Next
' Add Results heading
xlWorkSheet.Cells(2, 1) = "Results"
For k = 0 To ArrayListResults.Count - 1
' Add results
xlWorkSheet.Cells(2, k + 2) = "" + ArrayListResults(k).ToString
Next
' Create chart
Dim chartPage As Excel.Chart
Dim xlCharts As Excel.ChartObjects
Dim myChart As Excel.ChartObject
Dim chartRange As Excel.Range
' Apply chart settings
xlCharts = xlWorkSheet.ChartObjects
myChart = xlCharts.Add(10, 80, 300, 250)
chartPage = myChart.Chart
chartRange = xlWorkSheet.Range("A1", "K2")
chartPage.SetSourceData(Source:=chartRange)
chartPage.ChartType = Excel.XlChartType.xlColumnStacked
Try
' Save Excel Worlbook
xlWorkSheet.SaveAs("C:\Users\andrew\Desktop\Software engineering project\SchoolTestProgram\images\" + pupilUser.ToString + "ResultsGraph.xlsx")
' Export chart as picture file
xlWorkSheet.ChartObjects(1).chart.Export(FileName:="C:\Users\andrew\Desktop\Software engineering project\SchoolTestProgram\images\" + pupilUser.ToString + "ResultsGraph.jpg", FilterName:="JPG")
' Set PBoxGraph
PBoxGraph.ImageLocation = "C:\Users\andrew\Desktop\Software engineering project\SchoolTestProgram\images\" + pupilUser.ToString + "ResultsGraph.jpg"
' Close Excel objects
xlWorkBook.Close()
xlApp.Quit()
Catch ex As Exception
MsgBox("Exception: " + ex.ToString)
End Try
' Realese the excel objects
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
Private Sub PupilMainResultsGraphForm_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
' Delete the files to avoid any problems that occur when the file already exists. This will also ensure that
' you are getting the most up-to-date files every time you open the graph
Dim FileToDelete As String
Dim PictureFileToDelete As String
FileToDelete = "C:\Users\andrew\Desktop\Software engineering project\SchoolTestProgram\images\" + pupilUser.ToString + "ResultsGraph.xlsx"
PictureFileToDelete = "C:\Users\andrew\Desktop\Software engineering project\SchoolTestProgram\images\" + pupilUser.ToString + "ResultsGraph.jpg"
If System.IO.File.Exists(FileToDelete) = True Then
System.IO.File.Delete(FileToDelete)
End If
' Kill EXCEL processes to try and avoid exceptions when closing
Dim myProcesses() As Process
Dim myProcess As Process
' Return array containing all instances of "EXCEL".
myProcesses = Process.GetProcessesByName("EXCEL")
For Each myProcess In myProcesses
myProcess.Kill()
Next
' Kill ONENOTE processes to try and avoid exceptions when closing
Dim myProcesses2() As Process
Dim myProcess2 As Process
' Return array containing all instances of "ONENOTEM".
myProcesses2 = Process.GetProcessesByName("ONENOTEM")
For Each myProcess2 In myProcesses
myProcess2.Kill()
Next
Try
If System.IO.File.Exists(PictureFileToDelete) = True Then
System.IO.File.Delete(PictureFileToDelete)
End If
Catch ex As Exception
MsgBox(ex.ToString)
Exit Sub
End Try
End Sub
Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
' Set PrintDialog1 print settings
PrintDialog1.Document = PrintDocument1
PrintDialog1.PrinterSettings.Copies = 1
PrintDialog1.PrinterSettings.DefaultPageSettings.Landscape = True
' Set Image
img = Image.FromFile("C:\Users\andrew\Desktop\Software engineering project\SchoolTestProgram\images\" + pupilUser.ToString + "ResultsGraph.jpg")
'OpenAccess PrintDialog1
If PrintDialog1.ShowDialog = DialogResult.OK Then
PrintDocument1.Print()
End If
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
' Inform the printer of the image that is to be prited
e.Graphics.DrawImage(img, e.MarginBounds.Location)
img = Nothing
End Sub
Private Sub btnReturnToMain_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReturnToMain.Click
' Close graph form and allow access to main pupil form
Me.Close()
End Sub
End Class