在VC中如何能够读取.DOC文件中的内容? 哪怕是要先安装一个OFFICE2000系统! 有没有相关的控件?

解决方案 »

  1.   

    一,使用CFile
    void CReadWriteIniDlg::OnButton1() 
    {
    CFile file; //写文件
    file.Open("f:\\test.ini",CFile::modeWrite|CFile::modeCreate|CFile::modeNoTruncate);
    file.Write("0123456789",10);
    file.Close();    //读文件
    file.Open("f:\\test.ini",CFile::modeRead|CFile::modeCreate|CFile::modeNoTruncate);
    int nLength  = file.GetLength();
    char * pszBuff = new char[nLength + 1 ];
    file.Read(pszBuff,nLength);
    pszBuff[nLength] = '\0' ;
    MessageBox(pszBuff);
    delete [] pszBuff;
    file.Close();
    }CFile::modeCreate|CFile::modeNoTruncate风格的作用:文件存在就打开,不存在就创建新文件.
    |是位或,这儿也可以用+.
    Read,Write存取的数据大小限制于64k以内,如果数据大于64K,则用ReadHuge,WriteHuge.二.用 CStdioFilevoid CReadWriteIniDlg::OnButton2() 
    {
    CStdioFile file ; file.Open("f:\\test.ini",CFile::modeReadWrite|CFile::modeCreate|CFile::modeNoTruncate);

    //写文件
    file.WriteString("ABCDEFG\n");
    file.WriteString("abcdefg\n"); //读文件
    file.SeekToBegin();//将文件指针移动到最后
    char szText[1000];
    while(NULL != file.ReadString(szText,999))
    MessageBox(szText); file.Close();
    }三,以ini的形式存取.
    UINT GetProfileInt( LPCTSTR lpszSection, LPCTSTR lpszEntry, int nDefault );
    BOOL WriteProfileInt( LPCTSTR lpszSection, LPCTSTR lpszEntry, int nValue );CString GetProfileString( LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCTSTR lpszDefault = NULL );
    BOOL WriteProfileString( LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCTSTR lpszValue );
    void CReadWriteIniDlg::OnButton3() 
    {
    //写注册表
    AfxGetApp()->WriteProfileString("Section","key","value");
    //ReadWriteIni.ini(与可执行程序同名)的内容如下
    //[Section]
        //key=value //如果文件或Section或key不存在,则返回第三个参数的值
    CString strVaule = AfxGetApp()->GetProfileString("Section","key","Default");
    }
    如果不想写在ini中,可添加如下代码,因为SetRegistryKey是保护级的,所以内能在加在C…App中.
    BOOL CReadWriteIniApp::InitInstance()
    {
    //不写ini,直接写在注册表的HKEY_CURRENT_USER\Software\csdn\ReadWriteIni
    SetRegistryKey("csdn");
    ….
    }
    void CReadWriteIniDlg::OnButton4() 
    {
    CString strFileName = "f:\\test.ini";
    //strFileName文件名
    WritePrivateProfileString("Section","key","value",strFileName);
    char szValue[1000];
    GetPrivateProfileString("Section","key","Default",szValue,999,strFileName);
    }
    ---------------------------
    技术上可行
    符合你的要求吗?
    如果不明白
    请给我发短消息
    请附: 帖子的地址
    或在
    http://www.338888.com/VCShare/Default.asp
    上提问
      

  2.   

    调用word application COM接口
      

  3.   

    好象有不对题目,我要的是读word文件中的内容,不是普通文本文件!
      

  4.   

    word按普通文件读,会读出来一大堆的格式信息,所以我觉得还是调用COM接口吧
      

  5.   

    此方面的COM接口资料,在那里可以找到?
      

  6.   

    以下是创建这个MFC应用程序的步骤:  
    (1)使用AppWizard创建一个新的MFC AppWizard(EXE)工程,命名为"Embed_Word"  
    (2)选择单文档视图(SDI)结构,在第3步中需要选中Container,以提供容器支持。 其它都为默认。在ClassView中将产生如下类: 
    应用类: CEmbed_WordApp in Embed_Word.h and Embed_Word.cpp  
    框架类: CMainFrame in MainFrm.h and MainFrm.cpp  
    文档类: CEmbed_WordDoc in Embed_WordDoc.h and Embed_WordDoc.cpp  
    视图类: CEmbed_WordView in Embed_WordView.h and Embed_WordView.cpp  
    容器类: CEmbed_WordCntrItem in CntrItem.h and CntrItem.cpp  
    (3)在View菜单中,选ClassWizard,选Automation选项卡,选Add Class,选择From a TypeLibrary, 在Office目录中选中Microsoft Word 97/2000 类型库Word8.olb或Word9.olb,会将把类型库中的所有类添加到你的工程中。这时,ClassView中会多出几十个类,可以通过这些类提供的接口来实现必要的功能。 
    (4)在CCntrItem.h中添加获取标准COM接口IDispach的函数:  
    LPDISPATCH GetIDispatch(); 其函数实现如下: 
    LPDISPATCH CEmbed_WordCntrItem::GetIDispatch() 

    ASSERT_VALID(this); 
    ASSERT(m_lpObject != NULL); 
    LPUNKNOWN lpUnk = m_lpObject; 
    Run(); 
    LPOLELINK lpOleLink = NULL; 
    if(m_lpObject->QueryInterface(IID_IOleLink,(LPVOID FAR*)&lpOleLink)== NOERROR) 

    ASSERT(lpOleLink != NULL); 
    lpUnk = NULL; 
    if(lpOleLink->GetBoundSource(&lpUnk) != NOERROR) 

    TRACE0("Warning: Link is not connected!\n"); 
    lpOleLink->Release(); 

    ASSERT(lpUnk != NULL); 

    LPDISPATCH lpDispatch = NULL; 
    if(lpUnk->QueryInterface(IID_IDispatch,(LPVOID FAR*)&lpDispatch) != NOERROR) 

    TRACE0("Waring: does not support IDispatch!\n"); 
    return NULL; 

    ASSERT(lpDispatch != NULL); 
    return lpDispatch;  

    通过此函数来返回标准COM接口IDispatch。 
    (5)在Embed_WordView.cpp中添加对"MSWord8.h"的引用:#include "MSWord8.h",如使用Word2000,则包含对"MSWord9.h"的引用。 然后在视类CEmbed_WordView中添加函数EmbedAutomateExcel(): 
    void CEmbed_WordView::EmbedAutomateWord() 

    BeginWaitCursor(); 
    CEmbed_WordCntrItem* pItem = NULL; 
    TRY 

    CEmbed_WordDoc* pDoc = GetDocument(); 
    ASSERT_VALID(pDoc); 
    pItem = new CEmbed_WordCntrItem(pDoc); 
    ASSERT_VALID(pItem); 
    GetClientRect(&pItem->rect); 
    CLSID clsid; 
    if(FAILED(::CLSIDFromProgID(L"Word.document",&clsid))) 
    AfxThrowMemoryException(); 
    if(!pItem->CreateNewItem(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(); 

    如果仔细研究过这段代码,会发现它同AppWizard自动生成的OnInsertObject()函数有着惊人的相似程度,看一下View类中的 OnInsertObject() 方法,对其中的注释引起了我们的兴趣,因为它和我们刚写的方法有惊人的相似。事实上,我们刚才写的只不过是OnInsertObject()的一个特例:OnInsertObject()允许用户从可用的OLE对象列表中选择其一插入到应用程序中。因为在此我们只需对Word进行自动化,所以派生了这一行为。  
    (6)为了在程序刚启动时便将Word嵌入到程序中来,还需在视类的OnInitialUpdate()函数中添加代码: 
    void CEmbed_WordView::OnInitialUpdate() 

    CView::OnInitialUpdate(); 
    EmbedAutomateWord(); //将Word嵌入 
    m_pSelection = NULL; 

    (7)为了使嵌入的工作区占满整个客户区可以通过修改OnDraw函数来实现: 
    void CEmbed_WordView::OnDraw(CDC* pDC) 

    CEmbed_WordDoc* pDoc = GetDocument(); 
    ASSERT_VALID(pDoc); 
    if (m_pSelection == NULL) 

    POSITION pos = pDoc->GetStartPosition(); 
    m_pSelection = (CEmbed_WordCntrItem*)pDoc->GetNextClientItem(pos); 

    if (m_pSelection != NULL) 

    CRect rect; 
    GetClientRect(&m_pSelection->rect); 
    m_pSelection->OnGetItemPosition(rect); 
    m_pSelection->Draw(pDC,rect);  


    我试过了有几个小问题.
    1,在View菜单中,选ClassWizard,选Automation选项卡,选Add Class,选择From a TypeLibrary, 在Office目录中选中Microsoft Word 97/2000 类型库Word8.olb或
    Word9.olb-->MSWord9.olb2,EmbedAutomateExcel(): 
    void CEmbed_WordView::EmbedAutomateWord() 
    EmbedAutomateExcel--->EmbedAutomateWord3,几个未定义的and存取权限的错误
    注释掉就行了
    你用不着---------------------------
    技术上可行
    符合你的要求吗?
    如果不明白
    请给我发短消息
    请附: 帖子的地址
    或在
    http://www.338888.com/VCShare/Default.asp
    上提问