|
| 1 | +### HTML转出到Word中 ### |
| 2 | +通过系统剪切板把HTML复制到Word中,并且保留原来的格式 |
| 3 | + |
| 4 | +```C# |
| 5 | + |
| 6 | + class HTMLUtils |
| 7 | + { |
| 8 | + // 将HTML代码复制到Windows剪贴板,并保证中 |
| 9 | + [DllImport("user32.dll")] |
| 10 | + static extern bool OpenClipboard(IntPtr hWndNewOwner); |
| 11 | + [DllImport("user32.dll")] |
| 12 | + static extern bool EmptyClipboard(); |
| 13 | + [DllImport("user32.dll")] |
| 14 | + static extern IntPtr SetClipboardData(uint uFormat, IntPtr hMem); |
| 15 | + [DllImport("user32.dll")] |
| 16 | + static extern bool CloseClipboard(); |
| 17 | + [DllImport("user32.dll", SetLastError = true)] |
| 18 | + static extern uint RegisterClipboardFormatA(string lpszFormat); |
| 19 | + |
| 20 | + [DllImport("kernel32.dll", SetLastError = true)] |
| 21 | + static extern IntPtr GlobalLock(IntPtr hMem); |
| 22 | + [DllImport("kernel32.dll", SetLastError = true)] |
| 23 | + static extern uint GlobalSize(IntPtr hMem); |
| 24 | + [DllImport("kernel32.dll", SetLastError = true)] |
| 25 | + static extern IntPtr GlobalUnlock(IntPtr hMem); |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// copy the html into clipboard |
| 29 | + /// </summary> |
| 30 | + /// <param name="html"></param> |
| 31 | + /// <returns></returns> |
| 32 | + static public bool CopyHTMLToClipboard(string html) |
| 33 | + { |
| 34 | + uint CF_HTML = RegisterClipboardFormatA("HTML Format"); |
| 35 | + bool bResult = false; |
| 36 | + if (OpenClipboard(IntPtr.Zero)) |
| 37 | + { |
| 38 | + if (EmptyClipboard()) |
| 39 | + { |
| 40 | + byte[] bs = System.Text.Encoding.UTF8.GetBytes(html); |
| 41 | + |
| 42 | + int size = Marshal.SizeOf(typeof(byte)) * bs.Length; |
| 43 | + |
| 44 | + IntPtr ptr = Marshal.AllocHGlobal(size); |
| 45 | + Marshal.Copy(bs, 0, ptr, bs.Length); |
| 46 | + |
| 47 | + IntPtr hRes = SetClipboardData(CF_HTML, ptr); |
| 48 | + CloseClipboard(); |
| 49 | + } |
| 50 | + } |
| 51 | + return bResult; |
| 52 | + } |
| 53 | + |
| 54 | + //将HTML代码按照Windows剪贴板格进行格式化 |
| 55 | + public static string HtmlClipboardData(string html) |
| 56 | + { |
| 57 | + StringBuilder sb = new StringBuilder(); |
| 58 | + Encoding encoding = Encoding.UTF8; //Encoding.GetEncoding(936); |
| 59 | + string Header = @"Version: 1.0 |
| 60 | + StartHTML: {0:000000} |
| 61 | + EndHTML: {1:000000} |
| 62 | + StartFragment: {2:000000} |
| 63 | + EndFragment: {3:000000} |
| 64 | + "; |
| 65 | + string HtmlPrefix = @"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN""> |
| 66 | + <html> |
| 67 | + <head> |
| 68 | + <meta http-equiv=Content-Type content=""text/html; charset={0}""> |
| 69 | + </head> |
| 70 | + <body> |
| 71 | + <!--StartFragment--> |
| 72 | + "; |
| 73 | + HtmlPrefix = string.Format(HtmlPrefix, "gb2312"); |
| 74 | + |
| 75 | + string HtmlSuffix = @"<!--EndFragment--></body></html>"; |
| 76 | + |
| 77 | + // Get lengths of chunks |
| 78 | + int HeaderLength = encoding.GetByteCount(Header); |
| 79 | + HeaderLength -= 16; // extra formatting characters {0:000000} |
| 80 | + int PrefixLength = encoding.GetByteCount(HtmlPrefix); |
| 81 | + int HtmlLength = encoding.GetByteCount(html); |
| 82 | + int SuffixLength = encoding.GetByteCount(HtmlSuffix); |
| 83 | + |
| 84 | + // Determine locations of chunks |
| 85 | + int StartHtml = HeaderLength; |
| 86 | + int StartFragment = StartHtml + PrefixLength; |
| 87 | + int EndFragment = StartFragment + HtmlLength; |
| 88 | + int EndHtml = EndFragment + SuffixLength; |
| 89 | + |
| 90 | + // Build the data |
| 91 | + sb.AppendFormat(Header, StartHtml, EndHtml, StartFragment, EndFragment); |
| 92 | + sb.Append(HtmlPrefix); |
| 93 | + sb.Append(html); |
| 94 | + sb.Append(HtmlSuffix); |
| 95 | + |
| 96 | + //Console.WriteLine(sb.ToString()); |
| 97 | + return sb.ToString(); |
| 98 | + } |
| 99 | + } |
| 100 | +``` |
| 101 | + |
| 102 | +调用: |
| 103 | + |
| 104 | +```C# |
| 105 | + string html = "<p><input type='text' name='test'>abc</p>"; |
| 106 | + html = HTMLUtils.HtmlClipboardData(html); |
| 107 | + HTMLUtils.CopyHTMLToClipboard(html); |
| 108 | +``` |
0 commit comments