在线等待:Excel死进程问题? 顶者给分,不够再加

yanwei100 2004-04-07 05:38:31
On Error resume NEXT
Set objExcelApp = CreateObject("Excel.Application")
objExcelApp.DisplayAlerts = false
objExcelApp.Application.Visible = false
objExcelApp.WorkBooks.Open(filename)
if isEmpty(objExcelApp.ActiveWorkBook) then
set objExcelApp.WorkBooks = nothing
objExcelApp.Quit
set objExcelApp = nothing
exit sub
end if
set objExcelBook = objExcelApp.ActiveWorkBook
if (not isEmpty(objExcelBook)) then
set objExcelSheets = objExcelBook.Worksheets(1)
set objExcelSheet = objExcelBook.Sheets(3)
'-----------------------------------------
'开始从excel文件读数据
end if
...全文
50 点赞 收藏 26
写回复
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
yanwei100 2004-04-08
由于只有100分,如果还需要,可以告诉我,另外开贴
回复
yanwei100 2004-04-08
感谢大家的关注。虽然这个问题还没有解决,但通过和大家的交流,还是对原来的程序进行了一些改进。
特别感谢qxg1123(雄关漫道真如铁,而今迈步从头越)
和xzq686(瞬)
回复
yanwei100 2004-04-08
这是我的完整程序, 在异常情况下,执行到isEmpty(objExcelApp.ActiveWorkBook)会报错unknown exception。

sub ReadExcel(filename,byRef MyDataCollection,byRef Count)
' check if excel file exist
Set fs = CreateObject("Scripting.FileSystemObject")
if (not fs.FileExists(filename)) then
Response.Write "Can't find the Excel File " + filename
Count = 0
exit sub
end if
Set objExcelApp = CreateObject("Excel.Application")
objExcelApp.DisplayAlerts = false
objExcelApp.Application.Visible = false
objExcelApp.Application.WorkBooks.Open(filename)
if (not isEmpty(objExcelApp.ActiveWorkBook)) then
set objExcelSheet = objExcelApp.ActiveWorkBook.Sheets(3)
'get the count number of records
while objExcelSheet.cells(Count+Start_line,1)<>""
Count=Count+1
wend
if ( Count>0 ) Then
redim MyDataCollection(Count)

for i = 1 to Count
' read record from curent line in excel
dim MyProject
set MyProject = new Project
dim line
line = i + Start_line -1
GetRecordFromExcelLine MyProject,objExcelSheet,line
set MyDataCollection(i) = MyProject
next
end if

else
Response.Write "Excel File open fail !"
end if
objExcelApp.WorkBooks.Close
objExcelApp.Quit
set objExcelApp = Nothing

end sub
回复
jasonboy 2004-04-08
帮你up
回复
yanwei100 2004-04-08
我的程序在正常情况下是可以运行的,现在的问题是有些时候不知道出了什么异常,然后系统就开始不正常了,也就是set objWorkBook=objExcelApp.WorkBooks.Open("c:\aa.xls")时会报错,然后就会出现Excel死进程。
没有on error resume next
if isEmpty(objExcelApp.ActiveWorkBook) then 等语句不会被执行
即使加上on error resume next
程序执行了objExcelApp.Quit后Excel进程还是存在


回复
qxg1123 2004-04-08
<%'On Error resume NEXT
Set objExcelApp = CreateObject("Excel.Application")
objExcelApp.DisplayAlerts = false
objExcelApp.Application.Visible = false

dim objWorkBook
set objWorkBook=objExcelApp.WorkBooks.Open("e:\aa.xls")'确定filename是否存在!

if isEmpty(objExcelApp.ActiveWorkBook) then
set objWorkBook= nothing
'objExcelApp.Quit
'set objExcelApp = nothing'"set objExcelApp=nothing"是不行的,去掉。
else
set objExcelBook = objExcelApp.ActiveWorkBook
if (not isEmpty(objExcelBook)) then
response.write "1asdffsd12343"
'response.end
set objExcelSheets = objExcelBook.Worksheets(1)
set objExcelSheet = objExcelBook.Sheets(2)
'-----------------------------------------
'开始从excel文件读数据
'切记最后要objExcelApp.Quit
end if
end if
objExcelApp.Quit
set objExcelApp=nothing
%>没有任何问题
回复
xzq686 2004-04-08
我用下面的代码,直接打开c:\aa.xls!可以执行到response.write "1asdffsd12343"
<%
On Error resume NEXT
Set objExcelApp = CreateObject("Excel.Application")
objExcelApp.DisplayAlerts = false
objExcelApp.Application.Visible = false

dim objWorkBook
set objWorkBook=objExcelApp.WorkBooks.Open("c:\aa.xls")'确定filename是否存在!

if isEmpty(objExcelApp.ActiveWorkBook) then
set objWorkBook= nothing
objExcelApp.Quit
'set objExcelApp = nothing'"set objExcelApp=nothing"是不行的,去掉。
else
set objExcelBook = objExcelApp.ActiveWorkBook
if (not isEmpty(objExcelBook)) then
response.write "1asdffsd12343"
response.end
set objExcelSheets = objExcelBook.Worksheets(1)
set objExcelSheet = objExcelBook.Sheets(2)
'-----------------------------------------
'开始从excel文件读数据
'切记最后要objExcelApp.Quit
end if
end if
%>
回复
yanwei100 2004-04-08
当问题出现后,用debug监视objExcelApp.WorkBooks的值,会被通知:
unable to evaluate the expression. 被呼叫方拒绝接收呼叫。

回复
yanwei100 2004-04-08
to xzq686(瞬)
set objWorkBook = objExcelApp.WorkBooks.Open(filename)
和我原来的程序是一样的, 运行到这一行还是出错"unknown exception"
只要对变量对象赋值,就会出错.
不知道有什么方法可以检测打开文件的状态,我怀疑问题出在这里
回复
yanwei100 2004-04-08
下标从0开始,ActiveWorkBook.Sheets(3)是第四个表,确定存在?

ActiveWorkBook.Sheets(3)是第三个表页,肯定存在的.我的另外一台机器上运行就没有问题.
回复
xzq686 2004-04-08
下面那样试试:
On Error resume NEXT
Set objExcelApp = CreateObject("Excel.Application")
objExcelApp.DisplayAlerts = false
objExcelApp.Application.Visible = false

dim objWorkBook
set objWorkBook=objExcelApp.WorkBooks.Open(filename)'确定filename是否存在!

if isEmpty(objExcelApp.ActiveWorkBook) then
set objWorkBook= nothing
objExcelApp.Quit
'set objExcelApp = nothing'"set objExcelApp=nothing"是不行的,去掉。
exit sub
end if
set objExcelBook = objExcelApp.ActiveWorkBook
if (not isEmpty(objExcelBook)) then
set objExcelSheets = objExcelBook.Worksheets(1)
set objExcelSheet = objExcelBook.Sheets(3)
'-----------------------------------------
'开始从excel文件读数据
end if
回复
dingdi 2004-04-08
gz
回复
qxg1123 2004-04-08
下标从0开始,ActiveWorkBook.Sheets(3)是第四个表,确定存在?
回复
aspnetxp 2004-04-08
up...
up...
回复
yanwei100 2004-04-08
这是我的代码,我把业务逻辑去掉了。 现在,去掉了on error resume next 后
在运行到set objExcelSheet = objExcelApp.ActiveWorkBook.Sheets(3)时
出错"unknown exception"
我查过,文件是存在的. 我怀疑是不是大开文件时出的错

sub ReadExcel(filename,byRef MyDataCollection,byRef Count)

Set objExcelApp = CreateObject("Excel.Application")
objExcelApp.DisplayAlerts = false
objExcelApp.Application.Visible = false
objExcelApp.WorkBooks.Open(filename)
set objExcelSheet = objExcelApp.ActiveWorkBook.Sheets(3)
if (not isEmpty(objExcelSheet)) then
Response.Write "Excel File open succeesful !"
else
Response.Write "Excel File open fail !"
end if
if (not isEmpty(objExcelApp.WorkBooks)) then
objExcelApp.WorkBooks.Close
set objExcelApp.WorkBooks = Nothing
end if
objExcelApp.Quit
set objExcelApp = Nothing

end sub
回复
qxg1123 2004-04-07
去掉on error resume next

再把quit拿出来

把建立对象和关闭对象及销毁对象的代码放到相同级别的代码块中

如果if不成立,你的代码就没有释放资源了
set objExcelApp.WorkBooks = nothing会不会销毁objExcelApp,
如果会,你的objExcelApp.Quit也就无效或者出错了
回复
yanwei100 2004-04-07
楼上,你没看到我的代码?
if isEmpty(objExcelApp.ActiveWorkBook) then
set objExcelApp.WorkBooks = nothing
objExcelApp.Quit
set objExcelApp = nothing
exit sub
回复
qxg1123 2004-04-07
objExcelApp.Quit
只set =nothing是不会关掉进程的,如果你测试了几次不死才怪
回复
whghwujx 2004-04-07
up
我用DELPHI导出数据的时候,当记录数超过900的时候就会出现这种情况,在ASP中还没有用到过,
up
回复
yanwei100 2004-04-07
我现在是在从Excel读数据时出现这个问题,还没有向Excel写入那
回复
发动态
发帖子
ASP
创建于2007-09-28

2.8w+

社区成员

ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
申请成为版主
社区公告
暂无公告