我用VC作EXCEL的二次开发,
没有import ...“excel.exe”,而是使用了import ...“excel.olb”结果是用这样的定义报错:
Excel::_Application spApp(Application); 改用
使用 CComQIPtr <Excel::_Application> spApp(Application); 
就不错,不知道为什么,两者有什么差别,后者使用了ATL的模板?

解决方案 »

  1.   

    当然用ATL的模板好了,又现成的不用自己关心CreateInstance/QueryInterfce...又得到了指针。
      

  2.   

    “excel.exe和excel.olb”在Import之后得到的类不一样:import ...“excel.exe”主要用于直接的类定义:如_Application spApp(Application); 而import ...“excel.olb”大多用于ATL模板:CComQIPtr <Excel::_Application> spApp(Application); ////////////////////
    你碰到的问题并不是错误!
      

  3.   

    谢谢二位,让我彻底明白了,另有一点疑问,两种方法好像能够使用的函数不一样,请看下面的代码:
    CComQIPtr< Excel::Workbooks> books;
    CComQIPtr< Excel::_Workbook> book;       // More object declarations.

    CComQIPtr< Excel::Worksheets> sheets;
    CComQIPtr< Excel::_Worksheet> sheet; CComQIPtr< Excel::Range> range;          // Used for Microsoft Excel 97( & 2000) components. COleVariant
    covTrue((short)TRUE),
    covFalse((short)FALSE),
    covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR); books = app->GetWorkbooks();     // Get an IDispatch pointer.
    // to the books object.
    book = books->Open("C:\\Test.xls");  
    sheets = book->GetWorksheets();
    sheet = sheets->GetItem( COleVariant((short)(1)) );
    range = sheet->GetRange(COleVariant("B3"), COleVariant("b3"));
    range->PutItem(COleVariant((long)(1)),COleVariant((long)(1)),COleVariant(LPCTSTR("000666")));
    CComQIPtr< IFont> newfont;
    newfont = range->GetFont();
    newfont->SetName(COleVariant("宋体"));
    newfont->SetSize(COleVariant((long)24));
    其中sheets = book->GetWorksheets();总是获得一个NULL指针,真是着急呀,不知道为什么?