//将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实例,然后将HTML按Windows剪贴板进行格式化,再以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);
}
}