关于excel的文章,大多都是在外部操作application这种方式实现的,能不能在VC的form上嵌入一个excel的表格,让用户来操作呢?
   使用VB完成了这个功能,但是界面很不友好,不知VC下面如何?

解决方案 »

  1.   

    自己解决了。
    以下是创建这个MFC应用程序的步骤: 1.使用AppWizard创建一个新的MFC AppWizard(EXE)工程,命名为"Embed_Excel" 2.选择单文档视图(SDI)结构,在第3步中需要选中Container,以提供容器支持。 其它都为默认。 
    产生以下类: 
    应用类: CEmbed_ExcelApp in Embed_Excel.h and Embed_Excel.cpp 框架类: CMainFrame in MainFrm.h and MainFrm.cpp 文档类: CEmbed_ExcelDoc in Embed_ExcelDoc.h and Embed_ExcelDoc.cpp 视图类: CEmbed_ExcelView in Embed_ExcelView.h and Embed_ExcelView.cpp 容器类: CEmbed_ExcelCntrItem in CntrItem.h and CntrItem.cpp 
    3.在VIEW菜单中,选ClassWizard,选Automation选项卡,选Add Class,选择From a TypeLibrary, 选中Microsoft Excel 97/2000 类型库:Excel8.olb或Excel9.olb(在Microsoft Office\Office目录下) 会将类型库中的所有类添加到你的工程中。 4.在CntrItem.h中为CEmbed_ExcelCntrItem类添加如下函数定义: 
    LPDISPATCH GetIDispatch(); 5.然后在CntrItem.cpp中添加GetIDispatch方法 
    示例代码 ----------- /******************************************************************* * This method returns the IDispatch* for the application linked to * this container. ********************************************************************/ LPDISPATCH CEmbed_ExcelCntrItem::GetIDispatch() { //The this and m_lpObject pointers must be valid for this function //to work correctly. The m_lpObject is the IUnknown pointer to // this object. ASSERT_VALID(this); ASSERT(m_lpObject != NULL); 
    LPUNKNOWN lpUnk = m_lpObject; 
    //The embedded application must be running in order for the rest //of the function to work. Run(); 
    //QI for the IOleLink interface of m_lpObject. LPOLELINK lpOleLink = NULL; if (m_lpObject->QueryInterface(IID_IOleLink, (LPVOID FAR*)&lpOleLink) == NOERROR) { ASSERT(lpOleLink != NULL); lpUnk = NULL; 
    //Retrieve the IUnknown interface to the linked application. if (lpOleLink->GetBoundSource(&lpUnk) != NOERROR) { TRACE0("Warning: Link is not connected!\n"); lpOleLink->Release(); return NULL; } ASSERT(lpUnk != NULL); } 
    //QI for the IDispatch interface of the linked application. LPDISPATCH lpDispatch = NULL; if (lpUnk->QueryInterface(IID_IDispatch, (LPVOID FAR*)&lpDispatch) !=NOERROR) { TRACE0("Warning: does not support IDispatch!\n"); return NULL; } 
    //After assuring ourselves it is valid, return the IDispatch //interface to the caller. ASSERT(lpDispatch != NULL); return lpDispatch; } 6.在Embed_ExcelView.h中为CEmbed_ExcelView类添加如下函数定义: void EmbedAutomateExcel(); 7.然后在Embed_ExcelView.cpp中添加EmbedAutomateExcel方法: 示例代码 ----------- 
    /******************************************************************** * This method encapsulates the process of embedding an Excel * Worksheet in a View object and automating that worksheet to add * some text to cell A1. ********************************************************************/ void CEmbed_ExcelView::EmbedAutomateExcel() { //Change the cursor so the user knows something exciting is going 

    //on. 
    CRect rect(0,0,200,400);
    CString m_FileName = "d:\\yy\\Research2.xls";
    BeginWaitCursor(); 
    COleExcelCntrItem* pItem = NULL; 
    TRY 

    COleExcelDoc* pDoc = GetDocument(); 
    ASSERT_VALID(pDoc); 
    pItem = new COleExcelCntrItem(pDoc); 
    ASSERT_VALID(pItem); 
    GetClientRect(&rect); 
    CLSID clsid; 
    if(FAILED(::CLSIDFromProgID(L"Excel.sheet",&clsid))) 
    AfxThrowMemoryException(); 
    // if(!pItem->CreateNewItem(clsid)) 
    // AfxThrowMemoryException(); 
    //open file ?
    if(!pItem->CreateFromFile(m_FileName,clsid))
    {
    AfxThrowMemoryException();
    } ASSERT_VALID(pItem); 
    pItem->DoVerb(OLEIVERB_SHOW, this); 
    m_pSelection = pItem; 
    pDoc->UpdateAllViews(NULL); 
    LPDISPATCH lpDisp; 
    lpDisp = pItem->GetIDispatch();  }
    CATCH(CException, e) 

    if (pItem != NULL) 

    ASSERT_VALID(pItem); 
    pItem->Delete(); 

    AfxMessageBox(IDP_FAILED_TO_CREATE);  

    END_CATCH 
    EndWaitCursor(); 
    } 将下面一行添加到 Embed_ExcelView.h: #include "excel8.h" 注意:如果使用Excel 2000, 头文件是 "excel9.h." 
    看一下View类中的 OnInsertObject() 方法,对其中的注释引起了我们的兴趣,因为它和我们刚写的方法有惊人的相似。事实上,我们刚才写的是OnInsertObject()的一个特例:允许用户从可用的OLE对象列表中选择其一插入到应用程序中。因为我们只想对Excel工作表进行自动化,所以派生这一行为。在我们的程序中,我们移去了InsertObject()内部的所有代码,用如上EmbedAutomateExcel()中的代码代替,或者你可以在InsertObject()函数中直接调用EmbedAutomateExcel()。
      

  2.   

    我的office 目录里没有这两个文件(Excel8.olb,Excel9.olb)
      

  3.   

    Word 2000 msword9.olb 
    Excel 2000 excel9.olb 
    PowerPoint 2000 msppt9.olb 
    54gym(不可能) :
    与office的版本有关Access 2000 msacc9.olb 
    Outlook 2000 msoutl9.olb 
    Word 2002 msword.olb 
    Excel 2002 excel.olb 
    PowerPoint 2002 msppt.olb 
    Access 2002 msacc.olb 
    Outlook 2002 msoutl.olb 
    Word 2003 msword.olb 
    Excel 2003 excel.exe 
    PowerPoint 2003 msppt.olb 
    Access 2003 msacc.olb 
    Outlook 2003 msoutl.olb 
      

  4.   

    用这种接口怎么打开excel文件呢?试了好多方法都没成功。