Excel进程未终止 [英] Excel Process not terminating

查看:81
本文介绍了Excel进程未终止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我有一个VB.NET应用程序,除其他外,它将数据写入Excel。我退出Excel后,无法让Excel进程终止。
我发现了一篇与此问题相关的文章:

http://support.microsoft.com/default...b;en-us;317109


以下是我根据上述文章编写的代码示例。当Excel

退出时,excel

工作簿,工作表和所有单元格都已正确格式化,但该过程不会终止。


我已经尝试了上面文章中显示的代码,并且这个过程确实会终止,但似乎只要你做了一些修改
$ b $的事情b工作簿,Excel进程不会终止。


任何想法?


Private Sub Button2_Click(ByVal sender As System.Object ,ByVal e As

System.EventArgs)处理Button2.Click

Dim c,i,j,r,s,lastcol As Integer

Dim Sname()As String = {" Hello"," World"," Bye"}

Dim T_head As New ArrayList

Dim Lee()As Integer = {0,1,2}

Dim Jeanie()As Integer = {3,4,5}

Dim we()As Integer = {6,7, 8}

Dim HIndex()= {Lee,Jeanie,我们}

Dim head()As String = {" This"," Is"," A,&qu ot;测试,OF,工作,

" With",Excel"," Code}

Dim Rarray As New ArrayList

Dim T_Rarray As New ArrayList

Dim oApp As New Excel.Application

oApp.SheetsInNewWorkbook = 1

Dim oBooks作为Excel.Workbooks = oApp.Workbooks

Dim oBook As Excel.Workbook = oBooks.Add

Dim oSheet As Excel.Worksheet

Dim rng As Excel.Range


oApp.Visible = True

oSheet = oBook.ActiveSheet

oSheet.Name =" Lee"

r = 1

for s = 0 To 2

oBook.Sheets.Add()

oSheet = oBook.ActiveSheet

oSheet.Name = Sname(s)

For i = 0 To HIndex(s).Length - 1

T_head .Add(head(HIndex(s)(i)))

下一页

对于c = 0到T_head.Count - 1

oSheet 。选择(s + 1)

CType(oSheet.Cells.Item(r,c + 1),Excel.Range).Value2 =

T_head(c)

下一页

T_head.Clear()

下一页

对于j = 1到3

r = r + 1

对于i = 1到9

Rarray.Add(i * j)

下一页

对于s = 0到2

对于i = 0到HIndex(s).Length - 1

T_Rarray.Add(Rarray(HIndex(s)(i)))

下一页

对于c = 0到T_Rarray.Count - 1

oSheet.Select(s + 1)

CType(oSheet.Cells.Item(r,c + 1),Excel.Range).Value2 =

T_Rarray(c)

下一页

T_Rarray.Clear()

下一页

Rarray.Clear()

下一页

s = 0到2

oSheet.Select(s + 1)

lastcol = oSheet.UsedRange.Find(" *",,,,

Excel.XlSearchOrder.xlByColumns,Excel.XlSearchDirection.xlPrevious).Column

oSheet.UsedRange.Horizo​​ntalAlignment = 3

如果(s = 0)那么

oSheet.UsedRange.NumberFormat = 0

结束如果

oSheet.UsedRange.Columns.AutoFit()


For我= 1到lastcol

rng = oSheet.Cells(1,i)

rng.Font.Bold = True

rng.Interior.ColorIndex = 15

下一页

下一页

NAR(rng)

NAR(oSheet)

oBook .Close(False)

NAR(oBook)

NAR(oBooks)

Debug.WriteLine(Sleeping ...)

System.Threading.Thread.Sleep(5000)

oApp.Quit()

NAR(oApp)

GC.Collect()

Debug.WriteLine(结束Excel)


结束子


Private Sub NAR(ByVal o As Object)

尝试

System.Runtime.InteropServices.Marshal.ReleaseComO bject(o)

Catch

最后

o =没什么

结束尝试

结束子

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 problem:

http://support.microsoft.com/default...b;en-us;317109

Below is a sample of code that I wrote based on the above article. The excel
workbook, worksheets, and all of the cells are properly formatted when Excel
quits, but the process will not terminate.

I''ve tried the code as shown in the above article and the process does
terminate, but it seems that as soon as you do something to modify the
workbook, the Excel process won''t terminate.

any ideas??

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Dim c, i, j, r, s, lastcol As Integer
Dim Sname() As String = {"Hello", "World", "Bye"}
Dim T_head As New ArrayList
Dim Lee() As Integer = {0, 1, 2}
Dim Jeanie() As Integer = {3, 4, 5}
Dim we() As Integer = {6, 7, 8}
Dim HIndex() = {Lee, Jeanie, we}
Dim head() As String = {"This", "Is", "A", "Test", "OF", "Working",
"With", "Excel", "Code"}
Dim Rarray As New ArrayList
Dim T_Rarray As New ArrayList
Dim oApp As New Excel.Application
oApp.SheetsInNewWorkbook = 1
Dim oBooks As Excel.Workbooks = oApp.Workbooks
Dim oBook As Excel.Workbook = oBooks.Add
Dim oSheet As Excel.Worksheet
Dim rng As Excel.Range

oApp.Visible = True
oSheet = oBook.ActiveSheet
oSheet.Name = "Lee"
r = 1
For s = 0 To 2
oBook.Sheets.Add()
oSheet = oBook.ActiveSheet
oSheet.Name = Sname(s)
For i = 0 To HIndex(s).Length - 1
T_head.Add(head(HIndex(s)(i)))
Next
For c = 0 To T_head.Count - 1
oSheet.Select(s + 1)
CType(oSheet.Cells.Item(r, c + 1), Excel.Range).Value2 =
T_head(c)
Next
T_head.Clear()
Next
For j = 1 To 3
r = r + 1
For i = 1 To 9
Rarray.Add(i * j)
Next
For s = 0 To 2
For i = 0 To HIndex(s).Length - 1
T_Rarray.Add(Rarray(HIndex(s)(i)))
Next
For c = 0 To T_Rarray.Count - 1
oSheet.Select(s + 1)
CType(oSheet.Cells.Item(r, c + 1), Excel.Range).Value2 =
T_Rarray(c)
Next
T_Rarray.Clear()
Next
Rarray.Clear()
Next
For s = 0 To 2
oSheet.Select(s + 1)
lastcol = oSheet.UsedRange.Find("*", , , ,
Excel.XlSearchOrder.xlByColumns, Excel.XlSearchDirection.xlPrevious).Column
oSheet.UsedRange.HorizontalAlignment = 3
If (s = 0) Then
oSheet.UsedRange.NumberFormat = 0
End If
oSheet.UsedRange.Columns.AutoFit()

For i = 1 To lastcol
rng = oSheet.Cells(1, i)
rng.Font.Bold = True
rng.Interior.ColorIndex = 15
Next
Next
NAR(rng)
NAR(oSheet)
oBook.Close(False)
NAR(oBook)
NAR(oBooks)
Debug.WriteLine("Sleeping...")
System.Threading.Thread.Sleep(5000)
oApp.Quit()
NAR(oApp)
GC.Collect()
Debug.WriteLine("End Excel")

End Sub

Private Sub NAR(ByVal o As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComO bject(o)
Catch
Finally
o = Nothing
End Try
End Sub

推荐答案

大家好,


好​​的,这里有一些关于问题出在哪里的更具体的信息

发生。


下面的代码有3个基本部分。第一个在每张纸上添加3个工作表和一组3个标题(第1行)的
。第二部分将这些列添加到

。第三部分添加了一些格式。


如果我摆脱了第二和第三部分,Excel进程仍然没有终止,因为它应该终止。但是,如果我也没有将第一部分中创建的工作表命名为

,则Excel流程将终止,因为它应该是




所以,第一个主要障碍是找出原因


oSheet = oBook.ActiveSheet

oSheet.Name = Sname(s)


导致Excel进程无法终止。


问候,

Lee

" lgbjr" < LG *** @ online.nospam>在消息中写道

新闻:Oo **************** @ tk2msftngp13.phx.gbl ...
Hi All,

OK, here is some more specific information about where the problem is
occuring.

the code below has 3 basic sections. the first adds 3 worksheets and a set
of 3 headers (row 1) on each sheet. The second section adds some data to
these columns. The third section adds some formatting.

If I get rid of the second and third sections, the Excel process still
doesn''t terminate as it should. however, if I also don''t name the sheets
that are created in the first section, the Excel process terminates as it
should.

So, the first major hurddle is finding out why

oSheet = oBook.ActiveSheet
oSheet.Name = Sname(s)

causes the Excel process not to terminate.

regards,
Lee
"lgbjr" <lg***@online.nospam> wrote in message
news:Oo****************@tk2msftngp13.phx.gbl...
大家好,

我有一个VB.NET应用程序,除其他外,它将数据写入Excel。退出Excel后,我无法让Excel流程终止。
我发现了一篇与此问题相关的文章:

http://support.microsoft.com/default...b;en-us; 317109

以下是我根据上述文章编写的代码示例。当Excel退出时,
excel工作簿,工作表和所有单元格都已正确格式化,但该过程不会终止。

我已经尝试了如下所示的代码在上面的文章和过程确实终止,但似乎只要你做一些修改
工作簿的事情,Excel过程就不会终止。

任何想法??

Private Sub Button2_Click(ByVal sender As System.Object,ByVal e As
System.EventArgs)处理Button2.Click
Dim c,i,j,r ,s,lastcol As Integer
Dim Sname()As String = {" Hello"," World"," Bye"}
Dim T_head As New ArrayList
Dim Lee() As Integer = {0,1,2}
Dim Jeanie()As Integer = {3,4,5}
Dim we()As Integer = {6,7,8}
Dim HIndex()= {Lee,Jeanie,we}
Dim head()As String = {" This",Is,A,Test,OF&q uot;,Working,
" With"," Excel"," Code"}
Dim Rarray As New ArrayList
Dim T_Rarray As New ArrayList
Dim oApp As New Excel.Application
oApp.SheetsInNewWorkbook = 1
Dim oBooks As Excel.Workbooks = oApp.Workbooks
Dim oBook as Excel.Workbook = oBooks.Add
Dim oSheet As Excel.Worksheet
Dim rng As Excel.Range

oApp.Visible = True
oSheet = oBook.ActiveSheet
oSheet.Name =" Lee"
r = 1
对于s = 0到2
oBook.Sheets.Add()
oSheet = oBook.ActiveSheet
oSheet.Name = Sname(s)
对于i = 0到HIndex(s).Length - 1
T_head.Add(head(HIndex(s)(i)))
对于c = 0到T_head.Count - 1
oSheet.Select(s + 1)
CType(oSheet.Cells.Ite m(r,c + 1),Excel.Range).Value2 =
T_head(c)
下一页
T_head.Clear()
下一页
对于j = 1至3
r = r + 1
对于i = 1至9
Rarray.Add(i * j)
下一页
对于s = 0至2
对于i = 0到HIndex(s).Length - 1
T_Rarray.Add(Rarray(HIndex(s)(i)))
下一页
对于c = 0到T_Rarray .Count - 1
oSheet.Select(s + 1)
CType(oSheet.Cells.Item(r,c + 1),Excel.Range).Value2
= T_Rarray(c)
下一页
T_Rarray.Clear()
下一页
Rarray.Clear()
下一页
对于s = 0到2
oSheet.Select (s + 1)
lastcol = oSheet.UsedRange.Find(" *",,,,,, Excel.XlSearchOrder.xlByColumns,
Excel.XlSearchDirection.xlPrevious).Colu mS
oSheet.UsedRange.Horizo​​ntalAlignment = 3
if(s = 0)然后
oSheet.UsedRange.NumberFormat = 0
结束如果是/> oSheet.UsedRange.Columns。 AutoFit()

对于i = 1到lastcol
rng = oSheet.Cells(1,i)
rng.Font.Bold = True
rng.Interior。 ColorIndex = 15
下一页
NAR(rng)
NAR(oSheet)
oBook.Close(False)
NAR(oBook) NAR(oBooks)
Debug.WriteLine(睡觉......)
System.Threading.Thread.Sleep(5000)
oApp.Quit()
NAR (oApp)
GC.Collect()
Debug.WriteLine(结束Excel)

End Sub

私人子NAR(ByVal o作为对象)
尝试
System.Runtime.InteropServices.Marshal.ReleaseComO bject(o)
Catch
最后
o =什么都没有
结束尝试
End Sub
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 problem:

http://support.microsoft.com/default...b;en-us;317109

Below is a sample of code that I wrote based on the above article. The
excel workbook, worksheets, and all of the cells are properly formatted
when Excel quits, but the process will not terminate.

I''ve tried the code as shown in the above article and the process does
terminate, but it seems that as soon as you do something to modify the
workbook, the Excel process won''t terminate.

any ideas??

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Dim c, i, j, r, s, lastcol As Integer
Dim Sname() As String = {"Hello", "World", "Bye"}
Dim T_head As New ArrayList
Dim Lee() As Integer = {0, 1, 2}
Dim Jeanie() As Integer = {3, 4, 5}
Dim we() As Integer = {6, 7, 8}
Dim HIndex() = {Lee, Jeanie, we}
Dim head() As String = {"This", "Is", "A", "Test", "OF", "Working",
"With", "Excel", "Code"}
Dim Rarray As New ArrayList
Dim T_Rarray As New ArrayList
Dim oApp As New Excel.Application
oApp.SheetsInNewWorkbook = 1
Dim oBooks As Excel.Workbooks = oApp.Workbooks
Dim oBook As Excel.Workbook = oBooks.Add
Dim oSheet As Excel.Worksheet
Dim rng As Excel.Range

oApp.Visible = True
oSheet = oBook.ActiveSheet
oSheet.Name = "Lee"
r = 1
For s = 0 To 2
oBook.Sheets.Add()
oSheet = oBook.ActiveSheet
oSheet.Name = Sname(s)
For i = 0 To HIndex(s).Length - 1
T_head.Add(head(HIndex(s)(i)))
Next
For c = 0 To T_head.Count - 1
oSheet.Select(s + 1)
CType(oSheet.Cells.Item(r, c + 1), Excel.Range).Value2 =
T_head(c)
Next
T_head.Clear()
Next
For j = 1 To 3
r = r + 1
For i = 1 To 9
Rarray.Add(i * j)
Next
For s = 0 To 2
For i = 0 To HIndex(s).Length - 1
T_Rarray.Add(Rarray(HIndex(s)(i)))
Next
For c = 0 To T_Rarray.Count - 1
oSheet.Select(s + 1)
CType(oSheet.Cells.Item(r, c + 1), Excel.Range).Value2
= T_Rarray(c)
Next
T_Rarray.Clear()
Next
Rarray.Clear()
Next
For s = 0 To 2
oSheet.Select(s + 1)
lastcol = oSheet.UsedRange.Find("*", , , ,
Excel.XlSearchOrder.xlByColumns,
Excel.XlSearchDirection.xlPrevious).Column
oSheet.UsedRange.HorizontalAlignment = 3
If (s = 0) Then
oSheet.UsedRange.NumberFormat = 0
End If
oSheet.UsedRange.Columns.AutoFit()

For i = 1 To lastcol
rng = oSheet.Cells(1, i)
rng.Font.Bold = True
rng.Interior.ColorIndex = 15
Next
Next
NAR(rng)
NAR(oSheet)
oBook.Close(False)
NAR(oBook)
NAR(oBooks)
Debug.WriteLine("Sleeping...")
System.Threading.Thread.Sleep(5000)
oApp.Quit()
NAR(oApp)
GC.Collect()
Debug.WriteLine("End Excel")

End Sub

Private Sub NAR(ByVal o As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComO bject(o)
Catch
Finally
o = Nothing
End Try
End Sub



大家好,


好​​的,这里有一些更具体的信息,关于问题出在哪里。


下面的代码有3个基本部分。第一个在每张纸上添加3个工作表和一组3个标题(第1行)的
。第二部分将这些列添加到

。第三部分添加了一些格式。


如果我摆脱了第二和第三部分,Excel进程仍然没有终止,因为它应该终止。但是,如果我也没有将第一部分中创建的工作表命名为

,则Excel流程将终止,因为它应该是




所以,第一个主要障碍是找出原因


oSheet = oBook.ActiveSheet

oSheet.Name = Sname(s)


导致Excel进程无法终止。


问候,

Lee

" lgbjr" < LG *** @ online.nospam>在消息中写道

新闻:Oo **************** @ tk2msftngp13.phx.gbl ...
Hi All,

OK, here is some more specific information about where the problem is
occuring.

the code below has 3 basic sections. the first adds 3 worksheets and a set
of 3 headers (row 1) on each sheet. The second section adds some data to
these columns. The third section adds some formatting.

If I get rid of the second and third sections, the Excel process still
doesn''t terminate as it should. however, if I also don''t name the sheets
that are created in the first section, the Excel process terminates as it
should.

So, the first major hurddle is finding out why

oSheet = oBook.ActiveSheet
oSheet.Name = Sname(s)

causes the Excel process not to terminate.

regards,
Lee
"lgbjr" <lg***@online.nospam> wrote in message
news:Oo****************@tk2msftngp13.phx.gbl...
大家好,

我有一个VB.NET应用程序,除其他外,它将数据写入Excel。退出Excel后,我无法让Excel流程终止。
我发现了一篇与此问题相关的文章:

http://support.microsoft.com/default...b;en-us; 317109

以下是我根据上述文章编写的代码示例。当Excel退出时,
excel工作簿,工作表和所有单元格都已正确格式化,但该过程不会终止。

我已经尝试了如下所示的代码在上面的文章和过程确实终止,但似乎只要你做一些修改
工作簿的事情,Excel过程就不会终止。

任何想法??

Private Sub Button2_Click(ByVal sender As System.Object,ByVal e As
System.EventArgs)处理Button2.Click
Dim c,i,j,r ,s,lastcol As Integer
Dim Sname()As String = {" Hello"," World"," Bye"}
Dim T_head As New ArrayList
Dim Lee() As Integer = {0,1,2}
Dim Jeanie()As Integer = {3,4,5}
Dim we()As Integer = {6,7,8}
Dim HIndex()= {Lee,Jeanie,we}
Dim head()As String = {" This",Is,A,Test,OF&q uot;,Working,
" With"," Excel"," Code"}
Dim Rarray As New ArrayList
Dim T_Rarray As New ArrayList
Dim oApp As New Excel.Application
oApp.SheetsInNewWorkbook = 1
Dim oBooks As Excel.Workbooks = oApp.Workbooks
Dim oBook as Excel.Workbook = oBooks.Add
Dim oSheet As Excel.Worksheet
Dim rng As Excel.Range

oApp.Visible = True
oSheet = oBook.ActiveSheet
oSheet.Name =" Lee"
r = 1
对于s = 0到2
oBook.Sheets.Add()
oSheet = oBook.ActiveSheet
oSheet.Name = Sname(s)
对于i = 0到HIndex(s).Length - 1
T_head.Add(head(HIndex(s)(i)))
对于c = 0到T_head.Count - 1
oSheet.Select(s + 1)
CType(oSheet.Cells.Ite m(r,c + 1),Excel.Range).Value2 =
T_head(c)
下一页
T_head.Clear()
下一页
对于j = 1至3
r = r + 1
对于i = 1至9
Rarray.Add(i * j)
下一页
对于s = 0至2
对于i = 0到HIndex(s).Length - 1
T_Rarray.Add(Rarray(HIndex(s)(i)))
下一页
对于c = 0到T_Rarray .Count - 1
oSheet.Select(s + 1)
CType(oSheet.Cells.Item(r,c + 1),Excel.Range).Value2
= T_Rarray(c)
下一页
T_Rarray.Clear()
下一页
Rarray.Clear()
下一页
对于s = 0到2
oSheet.Select (s + 1)
lastcol = oSheet.UsedRange.Find(" *",,,,,, Excel.XlSearchOrder.xlByColumns,
Excel.XlSearchDirection.xlPrevious).Colu mS
oSheet.UsedRange.Horizo​​ntalAlignment = 3
if(s = 0)然后
oSheet.UsedRange.NumberFormat = 0
结束如果是/> oSheet.UsedRange.Columns。 AutoFit()

对于i = 1到lastcol
rng = oSheet.Cells(1,i)
rng.Font.Bold = True
rng.Interior。 ColorIndex = 15
下一页
NAR(rng)
NAR(oSheet)
oBook.Close(False)
NAR(oBook) NAR(oBooks)
Debug.WriteLine(睡觉......)
System.Threading.Thread.Sleep(5000)
oApp.Quit()
NAR (oApp)
GC.Collect()
Debug.WriteLine(结束Excel)

End Sub

私人子NAR(ByVal o作为对象)
尝试
System.Runtime.InteropServices.Marshal.ReleaseComO bject(o)
Catch
最后
o =什么都没有
结束尝试
End Sub
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 problem:

http://support.microsoft.com/default...b;en-us;317109

Below is a sample of code that I wrote based on the above article. The
excel workbook, worksheets, and all of the cells are properly formatted
when Excel quits, but the process will not terminate.

I''ve tried the code as shown in the above article and the process does
terminate, but it seems that as soon as you do something to modify the
workbook, the Excel process won''t terminate.

any ideas??

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Dim c, i, j, r, s, lastcol As Integer
Dim Sname() As String = {"Hello", "World", "Bye"}
Dim T_head As New ArrayList
Dim Lee() As Integer = {0, 1, 2}
Dim Jeanie() As Integer = {3, 4, 5}
Dim we() As Integer = {6, 7, 8}
Dim HIndex() = {Lee, Jeanie, we}
Dim head() As String = {"This", "Is", "A", "Test", "OF", "Working",
"With", "Excel", "Code"}
Dim Rarray As New ArrayList
Dim T_Rarray As New ArrayList
Dim oApp As New Excel.Application
oApp.SheetsInNewWorkbook = 1
Dim oBooks As Excel.Workbooks = oApp.Workbooks
Dim oBook As Excel.Workbook = oBooks.Add
Dim oSheet As Excel.Worksheet
Dim rng As Excel.Range

oApp.Visible = True
oSheet = oBook.ActiveSheet
oSheet.Name = "Lee"
r = 1
For s = 0 To 2
oBook.Sheets.Add()
oSheet = oBook.ActiveSheet
oSheet.Name = Sname(s)
For i = 0 To HIndex(s).Length - 1
T_head.Add(head(HIndex(s)(i)))
Next
For c = 0 To T_head.Count - 1
oSheet.Select(s + 1)
CType(oSheet.Cells.Item(r, c + 1), Excel.Range).Value2 =
T_head(c)
Next
T_head.Clear()
Next
For j = 1 To 3
r = r + 1
For i = 1 To 9
Rarray.Add(i * j)
Next
For s = 0 To 2
For i = 0 To HIndex(s).Length - 1
T_Rarray.Add(Rarray(HIndex(s)(i)))
Next
For c = 0 To T_Rarray.Count - 1
oSheet.Select(s + 1)
CType(oSheet.Cells.Item(r, c + 1), Excel.Range).Value2
= T_Rarray(c)
Next
T_Rarray.Clear()
Next
Rarray.Clear()
Next
For s = 0 To 2
oSheet.Select(s + 1)
lastcol = oSheet.UsedRange.Find("*", , , ,
Excel.XlSearchOrder.xlByColumns,
Excel.XlSearchDirection.xlPrevious).Column
oSheet.UsedRange.HorizontalAlignment = 3
If (s = 0) Then
oSheet.UsedRange.NumberFormat = 0
End If
oSheet.UsedRange.Columns.AutoFit()

For i = 1 To lastcol
rng = oSheet.Cells(1, i)
rng.Font.Bold = True
rng.Interior.ColorIndex = 15
Next
Next
NAR(rng)
NAR(oSheet)
oBook.Close(False)
NAR(oBook)
NAR(oBooks)
Debug.WriteLine("Sleeping...")
System.Threading.Thread.Sleep(5000)
oApp.Quit()
NAR(oApp)
GC.Collect()
Debug.WriteLine("End Excel")

End Sub

Private Sub NAR(ByVal o As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComO bject(o)
Catch
Finally
o = Nothing
End Try
End Sub



大家好,


好​​的,我一直在做一些实验。如果我添加额外的


NAR(oSheet)


那么我可以使用

$ b来命名表格$ b oSheet = oBook.ActiveSheet

oSheet.Name = Sname(s)


和Excel流程终止。但是,代码

的第2和第3部分仍然保持Excel进程运行。因为我显然不明白需要发布什么COM对象才能让Excel进程退出,也许

这是解决这个问题的更好方法。


有谁知道如何检索活动的COM对象列表?这样,我就可以看到我需要发布的东西了。


谢谢

Lee

" lgbjr" < LG *** @ online.nospam>写在消息

新闻:uQ ************** @ TK2MSFTNGP11.phx.gbl ...
Hi All,

OK, I''ve been doing some experimenting. If I add an additional

NAR(oSheet)

then I can name the sheets using

oSheet = oBook.ActiveSheet
oSheet.Name = Sname(s)

and the Excel Process terminates. However, sections 2 and 3 of the code
still leave the Excel process running. Since I obviously don''t understand
what COMobjects need to be released to get the Excel process to exit, maybe
there''s a better way to approach this problem.

Does anyone know how to retrieve a list of active COMobjects? This way, I
could see what I need to release.

thanks
Lee
"lgbjr" <lg***@online.nospam> wrote in message
news:uQ**************@TK2MSFTNGP11.phx.gbl...
大家好,

好的,这里有一些关于问题出现的具体信息。

下面的代码有3个基本部分。第一个在每张纸上添加3个工作表和一组3个标题(第1行)。第二部分为这些列添加了一些数据。第三部分添加了一些格式。

如果我摆脱第二和第三部分,Excel进程仍然没有终止它应该。但是,如果我也没有将第一部分中创建的工作表命名为
,那么Excel工艺将终止,因为它应该是。

所以,第一个主要障碍找出原因

oSheet = oBook.ActiveSheet
oSheet.Name = Sname(s)

导致Excel进程不终止。

问候,
$

" lgbjr" < LG *** @ online.nospam>在消息中写道
新闻:Oo **************** @ tk2msftngp13.phx.gbl ...
Hi All,

OK, here is some more specific information about where the problem is
occuring.

the code below has 3 basic sections. the first adds 3 worksheets and a set
of 3 headers (row 1) on each sheet. The second section adds some data to
these columns. The third section adds some formatting.

If I get rid of the second and third sections, the Excel process still
doesn''t terminate as it should. however, if I also don''t name the sheets
that are created in the first section, the Excel process terminates as it
should.

So, the first major hurddle is finding out why

oSheet = oBook.ActiveSheet
oSheet.Name = Sname(s)

causes the Excel process not to terminate.

regards,
Lee
"lgbjr" <lg***@online.nospam> wrote in message
news:Oo****************@tk2msftngp13.phx.gbl...
大家好,

我有一个VB.NET应用程序,除其他外,它将数据写入Excel。退出Excel后,我无法让Excel流程终止。
我发现了一篇与此问题相关的文章:

http://support.microsoft.com/default...b;en-us; 317109

以下是我根据上述文章编写的代码示例。当Excel退出时,
excel工作簿,工作表和所有单元格都已正确格式化,但该过程不会终止。

我已经尝试了如下所示的代码在上面的文章和过程确实终止,但似乎只要你做一些修改
工作簿的事情,Excel过程就不会终止。

任何想法??

Private Sub Button2_Click(ByVal sender As System.Object,ByVal e As
System.EventArgs)处理Button2.Click
Dim c,i,j,r ,s,lastcol As Integer
Dim Sname()As String = {" Hello"," World"," Bye"}
Dim T_head As New ArrayList
Dim Lee() As Integer = {0,1,2}
Dim Jeanie()As Integer = {3,4,5}
Dim we()As Integer = {6,7,8}
Dim HIndex()= {Lee,Jeanie,we}
Dim head()As String = {" This",Is,A,Test,OF&q uot;,
" Working"," With"," Excel"," Code"}
Dim Rarray As New ArrayList
Dim T_Rarray As New ArrayList
Dim oApp As New Excel.Application
oApp.SheetsInNewWorkbook = 1
Dim oBooks As Excel.Workbooks = oApp.Workbooks
Dim oBook as Excel.Workbook = oBooks.Add
Dim oSheet As Excel.Worksheet
Dim rng As Excel.Range

oApp.Visible = True
oSheet = oBook.ActiveSheet
oSheet.Name =" Lee"
r = 1
对于s = 0到2
oBook.Sheets.Add()
oSheet = oBook.ActiveSheet
oSheet.Name = Sname(s)
对于i = 0到HIndex(s).Length - 1
T_head.Add(head(HIndex(s)(i)))
对于c = 0到T_head.Count - 1
oSheet.Select(s + 1)
CType(oSheet.Cells.Ite m(r,c + 1),Excel.Range).Value2 =
T_head(c)
下一页
T_head.Clear()
下一页
对于j = 1至3
r = r + 1
对于i = 1至9
Rarray.Add(i * j)
下一页
对于s = 0至2
对于i = 0到HIndex(s).Length - 1
T_Rarray.Add(Rarray(HIndex(s)(i)))
下一页
对于c = 0到T_Rarray .Count - 1
oSheet.Select(s + 1)
CType(oSheet.Cells.Item(r,c + 1),Excel.Range).Value2
= T_Rarray(c)
下一页
T_Rarray.Clear()
下一页
Rarray.Clear()
下一页
对于s = 0到2
oSheet.Select (s + 1)
lastcol = oSheet.UsedRange.Find(" *",,,,,, Excel.XlSearchOrder.xlByColumns,
Excel.XlSearchDirection.xlPrevious).Colu mS
oSheet.UsedRange.Horizo​​ntalAlignment = 3
if(s = 0)然后
oSheet.UsedRange.NumberFormat = 0
结束如果是/> oSheet.UsedRange.Columns。 AutoFit()

对于i = 1到lastcol
rng = oSheet.Cells(1,i)
rng.Font.Bold = True
rng.Interior。 ColorIndex = 15
下一页
NAR(rng)
NAR(oSheet)
oBook.Close(False)
NAR(oBook) NAR(oBooks)
Debug.WriteLine(睡觉......)
System.Threading.Thread.Sleep(5000)
oApp.Quit()
NAR (oApp)
GC.Collect()
Debug.WriteLine(结束Excel)

End Sub

私人子NAR(ByVal o作为对象)
尝试
System.Runtime.InteropServices.Marshal.ReleaseComO bject(o)
Catch
最后
o = Nothing
结束尝试
End Sub
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 problem:

http://support.microsoft.com/default...b;en-us;317109

Below is a sample of code that I wrote based on the above article. The
excel workbook, worksheets, and all of the cells are properly formatted
when Excel quits, but the process will not terminate.

I''ve tried the code as shown in the above article and the process does
terminate, but it seems that as soon as you do something to modify the
workbook, the Excel process won''t terminate.

any ideas??

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Dim c, i, j, r, s, lastcol As Integer
Dim Sname() As String = {"Hello", "World", "Bye"}
Dim T_head As New ArrayList
Dim Lee() As Integer = {0, 1, 2}
Dim Jeanie() As Integer = {3, 4, 5}
Dim we() As Integer = {6, 7, 8}
Dim HIndex() = {Lee, Jeanie, we}
Dim head() As String = {"This", "Is", "A", "Test", "OF",
"Working", "With", "Excel", "Code"}
Dim Rarray As New ArrayList
Dim T_Rarray As New ArrayList
Dim oApp As New Excel.Application
oApp.SheetsInNewWorkbook = 1
Dim oBooks As Excel.Workbooks = oApp.Workbooks
Dim oBook As Excel.Workbook = oBooks.Add
Dim oSheet As Excel.Worksheet
Dim rng As Excel.Range

oApp.Visible = True
oSheet = oBook.ActiveSheet
oSheet.Name = "Lee"
r = 1
For s = 0 To 2
oBook.Sheets.Add()
oSheet = oBook.ActiveSheet
oSheet.Name = Sname(s)
For i = 0 To HIndex(s).Length - 1
T_head.Add(head(HIndex(s)(i)))
Next
For c = 0 To T_head.Count - 1
oSheet.Select(s + 1)
CType(oSheet.Cells.Item(r, c + 1), Excel.Range).Value2 =
T_head(c)
Next
T_head.Clear()
Next
For j = 1 To 3
r = r + 1
For i = 1 To 9
Rarray.Add(i * j)
Next
For s = 0 To 2
For i = 0 To HIndex(s).Length - 1
T_Rarray.Add(Rarray(HIndex(s)(i)))
Next
For c = 0 To T_Rarray.Count - 1
oSheet.Select(s + 1)
CType(oSheet.Cells.Item(r, c + 1), Excel.Range).Value2
= T_Rarray(c)
Next
T_Rarray.Clear()
Next
Rarray.Clear()
Next
For s = 0 To 2
oSheet.Select(s + 1)
lastcol = oSheet.UsedRange.Find("*", , , ,
Excel.XlSearchOrder.xlByColumns,
Excel.XlSearchDirection.xlPrevious).Column
oSheet.UsedRange.HorizontalAlignment = 3
If (s = 0) Then
oSheet.UsedRange.NumberFormat = 0
End If
oSheet.UsedRange.Columns.AutoFit()

For i = 1 To lastcol
rng = oSheet.Cells(1, i)
rng.Font.Bold = True
rng.Interior.ColorIndex = 15
Next
Next
NAR(rng)
NAR(oSheet)
oBook.Close(False)
NAR(oBook)
NAR(oBooks)
Debug.WriteLine("Sleeping...")
System.Threading.Thread.Sleep(5000)
oApp.Quit()
NAR(oApp)
GC.Collect()
Debug.WriteLine("End Excel")

End Sub

Private Sub NAR(ByVal o As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComO bject(o)
Catch
Finally
o = Nothing
End Try
End Sub




这篇关于Excel进程未终止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆