关于WORD文本转换成带HTML格式的文本

NiCeKssssssssss 2014-09-28 02:39:01
我是通过粘贴板 先读取粘贴板内容 然后转换 再调用 SetClipboardData 可是这个转换要怎么转 表格形式什么的都读不出来,求指点
char * buffer = NULL;
CString fromClipboard;
if ( OpenClipboard() )
{
HANDLE hData = GetClipboardData(CF_TEXT);
char * buffer = (char*)GlobalLock(hData);
fromClipboard = buffer;
GlobalUnlock(hData);
CloseClipboard();
}
CString str = fromClipboard;
CopyHtmlToClip(str);

--------------------------------------------
BOOL CHtmlEdit::CopyHtmlToClip( const CString &strHtml )
{
// 1)转换成UTF-8
CW2A pszU8(CT2W(strHtml), CP_UTF8);
int nHtmlSrcLen = strlen(pszU8);

// 2)组成剪贴板片段
CStringA strHtmlClip;
strHtmlClip.Format("Version:0.9\r\n"
"StartHTML:%08u\r\n"
"EndHTML:%08u\r\n"
"StartFragment:%08u\r\n"
"EndFragment:%08u\r\n"
"<html><body>\r\n"
"<!--StartFragment -->\r\n"
"%s\r\n"
"<!--EndFragment-->\r\n"
"</body></html>",
97, 172 + nHtmlSrcLen, 111, 136 + nHtmlSrcLen, pszU8);

// 3)剪贴板操作
static int cfid = 0;
if(!cfid)
{
cfid = RegisterClipboardFormat(_T("HTML Format"));
}

if (!OpenClipboard())
{
return false;
}

if (!EmptyClipboard())
{
CloseClipboard();
return false;
}

HGLOBAL hClipBuffer = GlobalAlloc(GMEM_DDESHARE, strHtmlClip.GetLength() + 1);
if (hClipBuffer == NULL)
{
CloseClipboard();
return false;
}

char *lpExpBuffer = (char *)GlobalLock(hClipBuffer);
if (lpExpBuffer == NULL)
{
GlobalFree(hClipBuffer);
CloseClipboard();
return false;
}

memcpy(lpExpBuffer, strHtmlClip, strHtmlClip.GetLength());
lpExpBuffer[strHtmlClip.GetLength()] = '\0';

GlobalUnlock(hClipBuffer);
SetClipboardData(cfid, hClipBuffer);
CloseClipboard();

return TRUE;
}
...全文
813 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
baidu_36416422 2016-10-22
  • 打赏
  • 举报
回复
public ActionResult File(HttpPostedFileBase file) { string FileUrl = string.Empty; string path = System.IO.Path.Combine(Server.MapPath("~/upload"), System.IO.Path.GetFileName(file.FileName)); //路径 file.SaveAs(path);//保存 FileUrl = path; //HttpFileCollectionBase files = Request.Files; string extension = Path.GetExtension(FileUrl); //.docx string htmlUrl = PreviewWord(FileUrl, "/upload/test.docx"); string htmcontent = Url.Content(htmlUrl); string name = Path.GetFileNameWithoutExtension(FileUrl) + ".html"; WebRequest myWebRequest = WebRequest.Create(System.IO.Path.Combine(Server.MapPath("~/upload"), name)); WebResponse myWebResponse = myWebRequest.GetResponse(); Stream myStream = myWebResponse.GetResponseStream(); Encoding encode = System.Text.Encoding.GetEncoding("gb2312"); StreamReader myStreamReader = new StreamReader(myStream, encode); string strhtml = myStreamReader.ReadToEnd(); myWebResponse.Close(); string stroutput = strhtml;//html内容 Regex reg = new Regex("<p .+/p>"); MatchCollection mas = reg.Matches(stroutput); return Redirect(Url.Content(htmlUrl)); //return Json(Redirect(Url.Content(htmlUrl)),JsonRequestBehavior.AllowGet); } #region 预览Word /// <summary> /// 预览Word /// </summary> public string PreviewWord(string physicalPath, string url) { Microsoft.Office.Interop.Word._Application application = null; Microsoft.Office.Interop.Word._Document doc = null; application = new Microsoft.Office.Interop.Word.Application(); object missing = Type.Missing; object trueObject = true; application.Visible = false; application.DisplayAlerts = WdAlertLevel.wdAlertsNone; doc = application.Documents.Open(physicalPath, missing, trueObject, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); //Save Excelto Html object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML; string htmlName = Path.GetFileNameWithoutExtension(physicalPath) + ".html"; String outputFile = Path.GetDirectoryName(physicalPath) + "\\" + htmlName; doc.SaveAs(outputFile, format, missing, missing, missing, missing, XlSaveAsAccessMode.xlNoChange, missing, missing, missing, missing, missing); doc.Close(); application.Quit(); return Path.GetDirectoryName(Server.UrlDecode(url)) + "\\" + htmlName; }
vcf_reader 2014-10-02
  • 打赏
  • 举报
回复
最好不要这么麻烦,遇到不同的版本不一定能执行
赵4老师 2014-09-29
  • 打赏
  • 举报
回复
搜索msword.h在哪个目录,将其拷贝到和你.cpp文件相同目录下。
NiCeKssssssssss 2014-09-29
  • 打赏
  • 举报
回复
引用 5 楼 zhao4zhong1 的回复:
//1.在VC中新建一控制台程序,选支持MFC(当然,你也可以不选择支持MFC的,不过会很麻烦)
//2.按CTRL+W调出MFC ClassWizard,Add Class->From a type library,选择你的word的类型库
//  (例如我的是word2003,安装在e盘,我的路径是"e:\edittools\microsoft office\office11\msword.olb"),
//  选择完毕后,在弹出的窗口中选择要让classwizard生成的包装类,在本例中要用到
//  _Application,
//  Documents,
//  _Document,
//  Range
//  这四个类,选中他们后按OK
//3.进入你的main函数所在的cpp文件,加入头文件引用
//  #include  "msword.h"    //引用刚才classwizard生成的idispatch包装类
谢谢大哥 我导入_Application,Documents,_Document, Range生成了四个类而不是一个 然后调用#include "msword.h" 无法打开
赵4老师 2014-09-29
  • 打赏
  • 举报
回复
//1.在VC中新建一控制台程序,选支持MFC(当然,你也可以不选择支持MFC的,不过会很麻烦)
//2.按CTRL+W调出MFC ClassWizard,Add Class->From a type library,选择你的word的类型库
//  (例如我的是word2003,安装在e盘,我的路径是"e:\edittools\microsoft office\office11\msword.olb"),
//  选择完毕后,在弹出的窗口中选择要让classwizard生成的包装类,在本例中要用到
//  _Application,
//  Documents,
//  _Document,
//  Range
//  这四个类,选中他们后按OK
//3.进入你的main函数所在的cpp文件,加入头文件引用
//  #include  "msword.h"    //引用刚才classwizard生成的idispatch包装类
//4.加入代码
// console_word.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "console_word.h"
#include "msword.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// The one and only application object

CWinApp theApp;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
    int nRetCode = 0;

    // initialize MFC and print and error on failure
    if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
    {
        // TODO: change error code to suit your needs
        printf(_T("Fatal Error: MFC initialization failed!\n"));
        nRetCode = 1;
    }
    else
    {
        // TODO: code your application's behavior here.
        if  (CoInitialize(NULL)  !=  S_OK)
        {
            AfxMessageBox("初始化COM支持库失败!");
            return  -1;
        }

        _Application  wordApp;
        Documents     docs;
        _Document     doc;
        Range         aRange;
        COleVariant   vTrue((short)TRUE), vFalse((short)FALSE), vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
        CString       txt;

        wordApp.CreateDispatch("Word.Application",NULL);
        wordApp.SetVisible(FALSE);
            docs=wordApp.GetDocuments();
                doc=docs.Open(COleVariant("c:\\new\\测试.doc"),vFalse,vTrue,vFalse,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt);
                    aRange=doc.Range(vOpt,vOpt);
                        txt=aRange.GetText();
                        AfxMessageBox(txt);//这里GetText得到的就是word文件的纯文本了,你可以将其写到txt文件中
                        printf("[%s]\n",txt.GetBuffer(txt.GetLength()));//里面的换行不是\r\n而是\r,所以需要输出重定向到文本文件看结果。
                    aRange.ReleaseDispatch();
                doc.Close(vOpt,vOpt,vOpt);
                doc.ReleaseDispatch();
            docs.ReleaseDispatch();
        wordApp.Quit(vOpt,vOpt,vOpt);
        wordApp.ReleaseDispatch();

        CoUninitialize();
    }

    return nRetCode;
}


NiCeKssssssssss 2014-09-29
  • 打赏
  • 举报
回复
引用 3 楼 zhao4zhong1 的回复:
用文件不会死人的。
这位大哥 WORD的COM是如何调用的 这方面没接触过 有没有什么实例 用文件方式的话首先要实现WORD的读写 可是我写入WORD的内容都没有格式了 应该如何修改
赵4老师 2014-09-29
  • 打赏
  • 举报
回复
VC6+Office 2003
NiCeKssssssssss 2014-09-29
  • 打赏
  • 举报
回复

这个是添加类

这个是打开其中的一个 感觉导入不正确
NiCeKssssssssss 2014-09-29
  • 打赏
  • 举报
回复
引用 7 楼 zhao4zhong1 的回复:
搜索msword.h在哪个目录,将其拷贝到和你.cpp文件相同目录下。
没有这个头文件。。。
赵4老师 2014-09-28
  • 打赏
  • 举报
回复
用文件不会死人的。
NiCeKssssssssss 2014-09-28
  • 打赏
  • 举报
回复
引用 1 楼 zhao4zhong1 的回复:
Word可以另存为HTML 在Word2003中开始记录宏,手动完成所需功能,结束记录宏,按Alt+F11键,查看刚才记录的宏对应的VBA代码。
我想请问下 这个是对文件的操作吗?我想在剪贴板中直接完成转换能行吗?
赵4老师 2014-09-28
  • 打赏
  • 举报
回复
Word可以另存为HTML 在Word2003中开始记录宏,手动完成所需功能,结束记录宏,按Alt+F11键,查看刚才记录的宏对应的VBA代码。

16,473

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧