面对现实,超越自己
逆水行舟,不进则退
posts - 269,comments - 32,trackbacks - 0
1、RichEdit文本转RTF文件

static DWORD CALLBACK MyStreamOutCallback(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
{
    CFile* pFile = (CFile*) dwCookie;
    pFile->Write(pbBuff, cb);
    *pcb = cb;
    return 0;
}

void CKTLXFunction::OnBnClickedIssuePaper()
{
    // TODO: Add your control notification handler code here
    CString sText;
    m_richEdit.GetWindowText(sText);
    if (sText.IsEmpty() && m_sImportPaperPath.IsEmpty())
    {
        AfxMessageBox("内容为空");
        return;
    }

    CString sImportRtfPath = CCommonFun::GetExecutablePath() + "temp.rtf";
    if (CFileFind().FindFile(sImportRtfPath))
    {
        ::DeleteFile(sImportRtfPath);
    }

    CFile cFile(TEXT(sImportRtfPath), CFile::modeCreate|CFile::modeWrite);
    EDITSTREAM es;
    es.dwCookie = (DWORD) &cFile; //设置用例参数,以便回调函数调用
    es.pfnCallback = MyStreamOutCallback;
    m_richEdit.StreamOut(SF_RTF, es);
}

2、word文本转RTF文件

    CString  m_sImportRtfPath = CCommonFun::GetExecutablePath() + "temp.rtf";
    if (CFileFind().FindFile(m_sImportRtfPath))
    {
        ::DeleteFile(m_sImportRtfPath);
    }

    _Application WordApp;
    CoInitialize(NULL);
    if(!WordApp.CreateDispatch("Word.Application",NULL))
    {
        LOG("创建Word服务失败!");
        exit(1);
        return FALSE;
    }
    WordApp.SetVisible(FALSE);

    COleVariant vTrue((short)TRUE),vFalse((short)FALSE),vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR);

    Documents docs=WordApp.GetDocuments();
    docs.Open(COleVariant("word文件路径"), vFalse, vFalse, vFalse, COleVariant(""), 
              COleVariant(""), vFalse, COleVariant(""), COleVariant(""),
              COleVariant((short)0),COleVariant("UTF-8") ,vTrue, vFalse, 
              COleVariant((short)0), vFalse, COleVariant(""));

    _Document active_doc; 
    active_doc = WordApp.GetActiveDocument();

    active_doc.SaveAs(COleVariant(m_sImportRtfPath), COleVariant((short)wdFormatRTF), vFalse, 
              COleVariant(""),vFalse, COleVariant(""), vFalse, vTrue, vFalse, vFalse,
              vFalse,COleVariant("936"), vFalse, vFalse, COleVariant((short)0), vFalse);

    WordApp.Quit(vOpt, vOpt, vOpt);
    active_doc.ReleaseDispatch();
    docs.ReleaseDispatch();
  WordApp.ReleaseDispatch(); //释放对象指针。切记,必须调用

3、RTF文件转html文件

    // 保存html文件 [8/12/2013 dell]
    _Application WordApp;
    CoInitialize(NULL);
    if(!WordApp.CreateDispatch("Word.Application",NULL))
    {
        //AfxMessageBox("创建Word服务失败!");
        exit(1);
        return FALSE;
    }
    WordApp.SetVisible(FALSE);

    COleVariant vTrue((short)TRUE),vFalse((short)FALSE),vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
    CString  m_sImportRtfPath = CCommonFun::GetExecutablePath() + "temp.rtf";
    if (!CFileFind().FindFile(m_sImportRtfPath))
    {
        LOG("转换html文件失败,没有找到rtf文件 : %s", m_sImportRtfPath);
        return FALSE;
    }
    Documents docs=WordApp.GetDocuments();
    docs.Open(COleVariant(m_sImportRtfPath),
        vFalse,vFalse,vFalse,COleVariant(""),COleVariant(""),
        vFalse,COleVariant(""),COleVariant(""),
        COleVariant((short)0),COleVariant("UTF-8")
        ,vTrue,vFalse,COleVariant((short)0),vFalse,
        COleVariant(""));

    _Document active_doc; 
    active_doc = WordApp.GetActiveDocument();

    active_doc.SaveAs(COleVariant("保存html文件路径"), 
        COleVariant((short)wdFormatHTML),
        vFalse, COleVariant(""),vFalse, COleVariant(""),
        vFalse,vTrue,vFalse,vFalse,vFalse,COleVariant("936"),//COleVariant(L"UTF-8")
        vFalse,vFalse,COleVariant((short)0),vFalse);

    WordApp.Quit(vOpt, vOpt, vOpt);
    active_doc.ReleaseDispatch();
    docs.ReleaseDispatch();
    WordApp.ReleaseDispatch(); //释放对象指针。切记,必须调用
posted on 2013-08-21 08:53 王海光 阅读(2371) 评论(1)  编辑 收藏 引用 所属分类: MFC

FeedBack:
# re: Word、RichEdit文本转RTF文件,RTF文件转HTML[未登录]
2014-08-18 14:09 | xy
要是没有装office呢?  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理