Word、RichEdit文本转RTF文件,RTF文件转HTML

Word、RichEdit文本转RTF文件,RTF文件转HTML
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(); //释放对象指针。切记,必须调用

你可能感兴趣的:(Word、RichEdit文本转RTF文件,RTF文件转HTML)