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. - ''' <summary>
-
''' Exports data from a datatable to excel.
-
''' </summary>
-
Friend Shared Sub ExportToExcel(ByVal dtbl As DataTable)
-
Dim exa As Excel.Application = Nothing
-
Dim wkb As Excel.Workbook = Nothing
-
Dim wks As Excel.Worksheet = Nothing
-
Dim intColIndex, intRowIndex As Integer
-
intColIndex = 0 : intRowIndex = 2
-
-
Try
-
exa = New Excel.Application
-
exa.SheetsInNewWorkbook = 1
-
wkb = exa.Workbooks.Add
-
wks = wkb.ActiveSheet
-
-
For intColIndex = 1 To dtbl.Columns.Count
-
wks.Cells(1, intColIndex) = dtbl.Columns(intColIndex - 1).ColumnName
-
Next
-
-
For Each row As DataRow In dtbl.Rows
-
For intColIndex = 1 To dtbl.Columns.Count
-
wks.Cells(intRowIndex, intColIndex) = row(intColIndex - 1)
-
Next
-
-
intRowIndex += 1
-
Next
-
-
For intColIndex = 1 To dtbl.Columns.Count
-
wks.Range(wks.Cells(1, intColIndex), wks.Cells(1, intColIndex)).Font.Bold = True
-
wks.Range(wks.Cells(1, intColIndex), wks.Cells(1, intColIndex)).Font.Underline = True
-
Next
-
-
'***** The problem doesn't occur if the following two lines are not used *****
-
wks.Range(wks.Cells(1, 1), wks.Cells(dtbl.Rows.Count + 1, dtbl.Columns.Count)).Columns.WrapText = False
-
wks.Range(wks.Cells(1, 1), wks.Cells(dtbl.Rows.Count + 1, dtbl.Columns.Count)).Columns.AutoFit()
-
'*****************************************************************************
-
-
exa.Visible = True
-
exa.UserControl = True
-
-
If Not exa Is Nothing Then exa.Quit()
-
System.Runtime.InteropServices.Marshal.ReleaseComObject(wks)
-
wks = Nothing
-
System.Runtime.InteropServices.Marshal.ReleaseComObject(wkb)
-
wkb = Nothing
-
System.Runtime.InteropServices.Marshal.ReleaseComObject(exa)
-
exa = Nothing
-
Catch ex As Exception
-
wks = Nothing
-
wkb = Nothing
-
exa = Nothing
-
MsgBox("The following error has occurred:" & vbCrLf & ex.Message, MsgBoxStyle.Critical, "Error")
-
Finally
-
GC.Collect()
-
End Try
-
End Sub
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: - '***** The problem doesn't occur if the following two lines are not used *****
-
Dim ColumnCollection As Excel.Range = wks.Range(wks.Cells(1, 1), wks.Cells(dtbl.Rows.Count + 1, dtbl.Columns.Count)).Columns
-
ColumnCollection.WrapText = False
-
ColumnCollection.AutoFit()
-
''wks.Range(wks.Cells(1, 1), wks.Cells(dtbl.Rows.Count + 1, dtbl.Columns.Count)).Columns.WrapText = False
-
''wks.Range(wks.Cells(1, 1), wks.Cells(dtbl.Rows.Count + 1, dtbl.Columns.Count)).Columns.AutoFit()
-
'*****************************************************************************
-
Cheers,
Randy
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
|
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...
|
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...
|
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...
| | |