1 现象描述
if (!appExcel.CreateDispatch("Excel.Application")) { return FALSE; } wbsBooks.AttachDispatch(appExcel.get_Workbooks()); lpDisp = wbsBooks.Open(m_csXlsPath,covOption, covOption, covOption, covOption, covOption, covOption, covOption, covOption, covOption, covOption, covOption, covOption, covOption, covOption); wbBook.AttachDispatch(lpDisp); lpDisp = wbBook.get_ActiveSheet(); wsSheet.AttachDispatch(lpDisp); rangeBasicCells.AttachDispatch(wsSheet.get_Cells()); rangeUsedCells.AttachDispatch(wsSheet.get_UsedRange()); rangeTempCells.AttachDispatch(rangeUsedCells.get_Columns()); lMaxColNum = rangeTempCells.get_Count(); rangeTempCells.AttachDispatch(rangeUsedCells.get_Rows()); lMaxRowNum = rangeTempCells.get_Count(); lStartCol = rangeUsedCells.get_Column(); lStartRow = rangeUsedCells.get_Row(); rangeUsedCells.ReleaseDispatch(); rangeTempCells.ReleaseDispatch(); GetInfoFromExc(rangeBasicCells, lMaxRowNum); rangeBasicCells.ReleaseDispatch(); wsSheet.ReleaseDispatch(); wbBook.ReleaseDispatch(); wbsBooks.Close(); wbsBooks.ReleaseDispatch();//*****标记1****** appExcel.Quit(); appExcel.ReleaseDispatch();
以上代码中,最初,注释为“//*****标记1******”这行代码并不存在,执行代码后Excel进程并不会从后台结束。当加上这行代码后Excel进程才会从后台结束。原因是,wbsBooks没有释放,appExcel不能退出。
2 该类问题解决方法
1)查看与Execl相关的变量是否全部都ReleaseDispatch了。
2)查看与Execl相关的变量在关闭和释放的时候顺序是否正确。比如,要先使用Close(),后使用ReleaseDispatch。
该解决方法总结于[vc excel Quit 无法关闭excel进程,急啊!!!!!](http://bbs.csdn.net/topics/390039312?page=1#post-395362721)
时间: 2024-10-19 02:17:52