VC调用Excel的退出问题?
程序代码:
以下是本人在编译环境为VS2010,Excel为2010环境下编写的一段代码,执行后, Excel进程无法关闭,参考了网上的许多措施,也无法关闭Excel进程,望高手指点! 谢谢!!! CString FilePathName; CFileDialog dlg(true,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, _T("EXCEL文件(*.xls;*.xlsx)|*.xls;*.xlsx||"),AfxGetMainWnd()); if(dlg.DoModal()==IDOK) { FilePathName=dlg.GetPathName(); CApplication app; CWorkbook book; CWorkbooks books; CRange range; CWorksheets sheets; CWorksheet sheet; COleException* pe = new COleException; if (!app.CreateDispatch(_T("Excel.Application"),pe)) { MessageBox(_T("Error!Creat Excel Application Server Faile!")); exit(1); } app.put_Visible(true); books = app.get_Workbooks(); book = books.Add(_variant_t(FilePathName)); int num_Sheel; sheets.AttachDispatch(book.get_Worksheets()); num_Sheel=sheets.get_Count(); sheet.AttachDispatch(sheets.get_Item(COleVariant((short)1))); range = sheet.get_Range(COleVariant(_T("A1")),COleVariant(_T("A1"))); _variant_t var; CString strResult; int i,j; i=1; j=1; var=range.get_Item(_variant_t(i),_variant_t(j)); range.ReleaseDispatch(); sheet.ReleaseDispatch(); sheets.ReleaseDispatch(); //结尾,释放 COleVariant covTrue((short)TRUE),covFalse((short)FALSE),covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR); book.Close(covFalse,covOptional,covOptional); books.Close(); pe->Delete(); book.ReleaseDispatch(); books.ReleaseDispatch(); app.Quit(); app.ReleaseDispatch(); MessageBox(_T("完成"),_T("提示"),MB_OK); }
[ 本帖最后由 laoyingyzp 于 2012-5-22 08:38 编辑 ]