html怎么不让复制字符串,无法将html格式的字符串复制到剪贴板

我正在尝试将html格式的字符串从我的数据库复制到word文档。我需要能够像子弹一样恢复格式。使用下面的代码,我得到一个异常

“指定的数据类型不可用。”

下面是要复制到剪贴板并写入word文档的代码。

DataObject clipDO = new DataObject();

clipDO.SetData(DataFormats.Html, HtmlClipboardData(temp));

Thread thread = new Thread(() => Clipboard.SetDataObject(clipDO, true));

thread.ApartmentState = ApartmentState.STA;

thread.Start();

thread.Join();

Microsoft.Office.Interop.Word.Paragraph oPara = wordApp.Selection.Paragraphs.Add(ref oMissing);

object pasteType = WdPasteDataType.wdPasteHTML;

oPara.Range.PasteSpecial(ref oMissing, ref oMissing, ref oMissing, ref oMissing, pasteType, ref oMissing, ref oMissing);

代码现在停止工作了。当它提前工作时,它不会将html复制到剪贴板。它会写入word doc,剪贴板的先前内容。

将标题添加到html字符串的帮助方法如下所示。

private static string HtmlClipboardData(string html)

{

StringBuilder sb = new StringBuilder();

Encoding encoding = Encoding.GetEncoding("utf-8");

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//TR""

html

head

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

head

body

!--StartFragment--

";

HtmlPrefix = string.Format(HtmlPrefix, encoding.WebName);

string HtmlSuffix = @"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值