将HTML保留格式粘到Word文档中

//HTML代码按照Windows剪贴板格进行格式化

private static string HtmlClipboardData(string html)

{

    StringBuilder sb = new StringBuilder();

    Encoding encoding = Encoding.UTF8; //Encoding.GetEncoding(936);

    string Header = @"

Version: 1.0

StartHTML: {0:000000}

EndHTML: {1:000000}

StartFragment: {2:000000}

EndFragment: {3:000000}

";

    string HtmlPrefix = @"

<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">

<html>

<head>

<meta http-equiv=Content-Type content=""text/html; charset={0}"">

</head>

<body>

<!--StartFragment-->

";

    HtmlPrefix = string.Format(HtmlPrefix, "gb2312");

 

    string HtmlSuffix = @"

<!--EndFragment-->

</body>

</html>

";

 

    // Get lengths of chunks

    int HeaderLength = encoding.GetByteCount(Header);

    HeaderLength -= 16; // extra formatting characters {0:000000}

    int PrefixLength = encoding.GetByteCount(HtmlPrefix);

    int HtmlLength = encoding.GetByteCount(html);

    int SuffixLength = encoding.GetByteCount(HtmlSuffix);

 

    // Determine locations of chunks

    int StartHtml = HeaderLength;

    int StartFragment = StartHtml + PrefixLength;

    int EndFragment = StartFragment + HtmlLength;

    int EndHtml = EndFragment + SuffixLength;

 

    // Build the data

    sb.AppendFormat(Header, StartHtml, EndHtml, StartFragment, EndFragment);

    sb.Append(HtmlPrefix);

    sb.Append(html);

    sb.Append(HtmlSuffix);

 

    //Console.WriteLine(sb.ToString());

    return sb.ToString();

}

 

// HTML代码复制到Windows剪贴板,并保证中

[DllImport("user32.dll")]

static extern bool OpenClipboard(IntPtr hWndNewOwner);

[DllImport("user32.dll")]

static extern bool EmptyClipboard();

[DllImport("user32.dll")]

static extern IntPtr SetClipboardData(uint uFormat, IntPtr hMem);

[DllImport("user32.dll")]

static extern bool CloseClipboard();

[DllImport("user32.dll", SetLastError = true)]

static extern uint RegisterClipboardFormatA(string lpszFormat);

 

[DllImport("kernel32.dll", SetLastError = true)]

static extern IntPtr GlobalLock(IntPtr hMem);

[DllImport("kernel32.dll", SetLastError = true)]

static extern uint GlobalSize(IntPtr hMem);

[DllImport("kernel32.dll", SetLastError = true)]

static extern IntPtr GlobalUnlock(IntPtr hMem);

 

static public bool CopyHTMLToClipboard(string html)

{

    uint CF_HTML = RegisterClipboardFormatA("HTML Format");

    bool bResult = false;

    if (OpenClipboard(IntPtr.Zero))

    {

        if (EmptyClipboard())

        {

            byte[] bs = System.Text.Encoding.UTF8.GetBytes(html);

 

            int size = Marshal.SizeOf(typeof(byte)) * bs.Length;

 

            IntPtr ptr = Marshal.AllocHGlobal(size);

            Marshal.Copy(bs, 0, ptr, bs.Length);

 

            IntPtr hRes = SetClipboardData(CF_HTML, ptr);

            CloseClipboard();

        }

    }

    return bResult;

}

 

//创建Word实例,然后将HTMLWindows剪贴板进行格式化,再以HTML方式粘贴到Word

[System.STAThread]

static void Main(string[] args)

{

    Microsoft.Office.Interop.Word.Application word = null;

    object missing = System.Reflection.Missing.Value;

    try

    {

 

        word = new Microsoft.Office.Interop.Word.Application();

        Microsoft.Office.Interop.Word.Document doc = word.Documents.Add(ref missing, ref missing, ref missing, ref missing);

        doc.Activate();

        word.Visible = true;

 

        string html = HtmlClipboardData("<p  style='margin-top:3.0pt;margin-right:2.85pt;margin-bottom:0cm;margin-left:35.55pt;margin-bottom:.0001pt;text-indent:-18.0pt;'>1  湿度调节器001SZ故障:相对湿度>40%;电加热器(001RS-02RS)不能正常启动</p>");

        CopyHTMLToClipboard(html);

 

        object dataType = Microsoft.Office.Interop.Word.WdPasteDataType.wdPasteHTML;

        word.Selection.PasteSpecial(ref missing, ref missing, ref missing, ref missing, ref dataType, ref missing, ref missing);

 

        Console.ReadLine();

 

    }

    finally

    {

        word.Quit(ref missing, ref missing, ref missing);

    }

}

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值