473,396 Members | 1,884 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Excel process continues running (.Net Framework 3.5, VS.Net 2008, VB.Net 2008).

88 64KB
Hi, I'm calling the following method from a button click event to export a datatable to excel. After the export is completed, the excel application object is quit,
released and assigned to nothing. But in reality it's not getting released and stays active unless the entire application is closed. So every time the button is
clicked for export, a new excel application object keeps on running. How can I solve this? Please help. Regards.

The problem doesn't occur if two of the lines from the method below are not used. But I can't omit them as they are really needed. Check the * marked lines.

Expand|Select|Wrap|Line Numbers
  1. ''' <summary>
  2.     ''' Exports data from a datatable to excel.
  3.     ''' </summary>
  4.     Friend Shared Sub ExportToExcel(ByVal dtbl As DataTable)
  5.         Dim exa As Excel.Application = Nothing
  6.         Dim wkb As Excel.Workbook = Nothing
  7.         Dim wks As Excel.Worksheet = Nothing
  8.         Dim intColIndex, intRowIndex As Integer
  9.         intColIndex = 0 : intRowIndex = 2
  10.  
  11.         Try
  12.             exa = New Excel.Application
  13.             exa.SheetsInNewWorkbook = 1
  14.             wkb = exa.Workbooks.Add
  15.             wks = wkb.ActiveSheet
  16.  
  17.             For intColIndex = 1 To dtbl.Columns.Count
  18.                 wks.Cells(1, intColIndex) = dtbl.Columns(intColIndex - 1).ColumnName
  19.             Next
  20.  
  21.             For Each row As DataRow In dtbl.Rows
  22.                 For intColIndex = 1 To dtbl.Columns.Count
  23.                     wks.Cells(intRowIndex, intColIndex) = row(intColIndex - 1)
  24.                 Next
  25.  
  26.                 intRowIndex += 1
  27.             Next
  28.  
  29.             For intColIndex = 1 To dtbl.Columns.Count
  30.                 wks.Range(wks.Cells(1, intColIndex), wks.Cells(1, intColIndex)).Font.Bold = True
  31.                 wks.Range(wks.Cells(1, intColIndex), wks.Cells(1, intColIndex)).Font.Underline = True
  32.             Next
  33.  
  34.         '***** The problem doesn't occur if the following two lines are not used *****
  35.             wks.Range(wks.Cells(1, 1), wks.Cells(dtbl.Rows.Count + 1, dtbl.Columns.Count)).Columns.WrapText = False
  36.             wks.Range(wks.Cells(1, 1), wks.Cells(dtbl.Rows.Count + 1, dtbl.Columns.Count)).Columns.AutoFit()
  37.         '*****************************************************************************
  38.  
  39.             exa.Visible = True
  40.             exa.UserControl = True
  41.  
  42.             If Not exa Is Nothing Then exa.Quit()
  43.             System.Runtime.InteropServices.Marshal.ReleaseComObject(wks)
  44.             wks = Nothing
  45.             System.Runtime.InteropServices.Marshal.ReleaseComObject(wkb)
  46.             wkb = Nothing
  47.             System.Runtime.InteropServices.Marshal.ReleaseComObject(exa)
  48.             exa = Nothing
  49.         Catch ex As Exception
  50.             wks = Nothing
  51.             wkb = Nothing
  52.             exa = Nothing
  53.             MsgBox("The following error has occurred:" & vbCrLf & ex.Message, MsgBoxStyle.Critical, "Error")
  54.         Finally
  55.             GC.Collect()
  56.         End Try
  57.     End Sub
Apr 15 '11 #1
1 1303
Hello, priyamtheone,

This is a problem that occurs because of an implicit reference to Excel. The application holds on to this reference until it closes. In your case, I believe that you can fix the problem by using an explicit reference within the "offending" lines. Here is an example:

Expand|Select|Wrap|Line Numbers
  1.             '***** The problem doesn't occur if the following two lines are not used ***** 
  2.             Dim ColumnCollection As Excel.Range = wks.Range(wks.Cells(1, 1), wks.Cells(dtbl.Rows.Count + 1, dtbl.Columns.Count)).Columns
  3.             ColumnCollection.WrapText = False
  4.             ColumnCollection.AutoFit()
  5.             ''wks.Range(wks.Cells(1, 1), wks.Cells(dtbl.Rows.Count + 1, dtbl.Columns.Count)).Columns.WrapText = False
  6.             ''wks.Range(wks.Cells(1, 1), wks.Cells(dtbl.Rows.Count + 1, dtbl.Columns.Count)).Columns.AutoFit()
  7.             '***************************************************************************** 
  8.  
Cheers,
Randy
Apr 21 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Tim Marsden | last post by:
Hi, I have a routine which is call from a ASP.NET web form. This routine creates an excel application, opens a workbook , runs some code to update the workbook, saves it as HTML on the sever and...
3
by: Mike Eaton | last post by:
Hi there, I'm working on an application that opens an excel workbook, does some processing on the data contained in it, and then saves the data into the workbook. What I'm noticing is that once...
2
by: Powerguy | last post by:
Hi all, I am looking for a way to get the Process id (or a handle) of an EXCEL process created from within my code. For example when the following code is executed: Dim EXL As...
18
by: lgbjr | last post by:
Hi All, I have a VB.NET app that, among other things, writes data to Excel. I am having trouble getting the Excel process to terminate after I quit Excel. I found an article related to this...
16
by: LP | last post by:
Hello, I am trying to use .NET with Excel. I installed Office 2003 and selected ..NET programming suport option, so it installed all those PIA, as MS sugests. But I can not find a way to destroy...
5
by: mabond | last post by:
Hi recently read a posting and reply about Excel processs still running after the Appliction.Quit was called. Thought I might be able to use the same...
0
by: ravindarjobs | last post by:
dear friends, i am using ms access 2003. i wanted to export data from access table to excel table. so i have followed this Dim db As Database Dim ea As Excel.Application Dim rs As...
3
by: Chris | last post by:
I have a python script that is driving Excel and using the win32com module. However, upon program completion there's still an Excel.exe process running in the background that I must terminate...
4
by: pavi14 | last post by:
Hi, I have a method in C# which checks if any Excel process is running on my system. If it finds any process is there a way to get the directory path of the file running in that excel process...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.