M
marcnikko
Hi,
I have VB .NET function below that creates an excel document. It
works fine and the Excel.exe process ends except when I try to add a
chart. When I include the code here I cannot cleanly end the Excel.exe
process:
xlChart.ChartType = Excel.XlChartType.xlXYScatterLines
Once I include that code the EXCEL.EXE process is still in the task
manager.
What can I do to close the EXCEL.EXE process?
Thanks,
Marc
Code:
Imports Excel = Microsoft.Office.Interop.Excel
Function CreateExcelPlot() As String
Dim plotFilePath As String = Nothing
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlDataSheet As Excel.Worksheet
Dim xlChartSheet As Excel.Worksheet
Dim xlChart As Excel.Chart
'Initiate Excel, hide the application, and add a workbook. In
case you were wondering, by referring to the Excel
'application, the program is initiated
xlApp = New Excel.Application
xlApp.Visible = False
xlBook = xlApp.Workbooks.Add()
xlDataSheet = CType(xlBook.Worksheets.Add(), Excel.Worksheet)
xlChartSheet = CType(xlBook.Worksheets.Add(), Excel.Worksheet)
xlChart = xlApp.Charts.Add
xlChart.ChartType = Excel.XlChartType.xlXYScatterLines 'Once
I include this, I can't cleanly close EXCEL.EXE
xlDataSheet.Cells(1, 1) = DateValue("January 1, 2003")
xlDataSheet.Cells(1, 2) = 30
xlDataSheet.Cells(2, 1) = DateValue("February 1, 2003")
xlDataSheet.Cells(2, 2) = 35
xlDataSheet.Cells(3, 1) = DateValue("March 1, 2003")
xlDataSheet.Cells(3, 2) = 31
xlDataSheet.Cells(4, 1) = DateValue("April 1, 2003")
xlDataSheet.Cells(4, 2) = 36
'xlChart.SetSourceData(xlDataSheet.Range("A1:B4")) Ignore for
now
plotFilePath = "C:\Test13.xls"
Try
My.Computer.FileSystem.DeleteFile(plotFilePath)
Catch
End Try
xlBook.SaveAs(plotFilePath)
xlBook.Close()
xlApp.Workbooks.Close()
xlApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
System.Runtime.InteropServices.Marshal.ReleaseComObject
(xlDataSheet)
System.Runtime.InteropServices.Marshal.ReleaseComObject
(xlChartSheet)
System.Runtime.InteropServices.Marshal.ReleaseComObject
(xlChart)
System.Runtime.InteropServices.Marshal.ReleaseComObject
(xlBook)
xlApp = Nothing
xlBook = Nothing
xlDataSheet = Nothing
xlChartSheet = Nothing
xlChart = Nothing
GC.Collect()
return plotFilePath
End Function
I have VB .NET function below that creates an excel document. It
works fine and the Excel.exe process ends except when I try to add a
chart. When I include the code here I cannot cleanly end the Excel.exe
process:
xlChart.ChartType = Excel.XlChartType.xlXYScatterLines
Once I include that code the EXCEL.EXE process is still in the task
manager.
What can I do to close the EXCEL.EXE process?
Thanks,
Marc
Code:
Imports Excel = Microsoft.Office.Interop.Excel
Function CreateExcelPlot() As String
Dim plotFilePath As String = Nothing
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlDataSheet As Excel.Worksheet
Dim xlChartSheet As Excel.Worksheet
Dim xlChart As Excel.Chart
'Initiate Excel, hide the application, and add a workbook. In
case you were wondering, by referring to the Excel
'application, the program is initiated
xlApp = New Excel.Application
xlApp.Visible = False
xlBook = xlApp.Workbooks.Add()
xlDataSheet = CType(xlBook.Worksheets.Add(), Excel.Worksheet)
xlChartSheet = CType(xlBook.Worksheets.Add(), Excel.Worksheet)
xlChart = xlApp.Charts.Add
xlChart.ChartType = Excel.XlChartType.xlXYScatterLines 'Once
I include this, I can't cleanly close EXCEL.EXE
xlDataSheet.Cells(1, 1) = DateValue("January 1, 2003")
xlDataSheet.Cells(1, 2) = 30
xlDataSheet.Cells(2, 1) = DateValue("February 1, 2003")
xlDataSheet.Cells(2, 2) = 35
xlDataSheet.Cells(3, 1) = DateValue("March 1, 2003")
xlDataSheet.Cells(3, 2) = 31
xlDataSheet.Cells(4, 1) = DateValue("April 1, 2003")
xlDataSheet.Cells(4, 2) = 36
'xlChart.SetSourceData(xlDataSheet.Range("A1:B4")) Ignore for
now
plotFilePath = "C:\Test13.xls"
Try
My.Computer.FileSystem.DeleteFile(plotFilePath)
Catch
End Try
xlBook.SaveAs(plotFilePath)
xlBook.Close()
xlApp.Workbooks.Close()
xlApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
System.Runtime.InteropServices.Marshal.ReleaseComObject
(xlDataSheet)
System.Runtime.InteropServices.Marshal.ReleaseComObject
(xlChartSheet)
System.Runtime.InteropServices.Marshal.ReleaseComObject
(xlChart)
System.Runtime.InteropServices.Marshal.ReleaseComObject
(xlBook)
xlApp = Nothing
xlBook = Nothing
xlDataSheet = Nothing
xlChartSheet = Nothing
xlChart = Nothing
GC.Collect()
return plotFilePath
End Function