使用 Interop 将 html 文本添加到 Word

发布于 2024-08-23 15:54:00 字数 190 浏览 4 评论 0原文

我正在尝试使用 Office Interop 将一些 HTML 格式的文本添加到 Word 中。我的代码如下所示:

Clipboard.SetText(notes, TextDataFormat.Html);
pgCriteria.Range.Paste();

但它抛出命令失败异常。有什么想法吗?

I'm trying to add some HTML formatted text to Word using Office Interop. My code looks like this:

Clipboard.SetText(notes, TextDataFormat.Html);
pgCriteria.Range.Paste();

but it's throwing a Command Failed exception. Any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

暖阳 2024-08-30 15:54:00

花了几个小时后,解决方案是使用这个优秀的类
http://blogs.msdn.com/jmstall/pages/示例代码-html-clipboard.aspx

After spending several hours the solutions is to use this excellent class
http://blogs.msdn.com/jmstall/pages/sample-code-html-clipboard.aspx

凉宸 2024-08-30 15:54:00

这在 Windows 7 和 Word 2007 上对我有用:

public static void pasteHTML(this Range range, string html)
{
    Clipboard.SetData(
      "HTML Format",
      string.Format("Version:0.9\nStartHTML:80\nEndHTML:{0,8}\nStart" + "Fragment:80\nEndFragment:{0,8}\n", 80 + html.Length) + html + "<");
      range.Paste();
}

示例使用: range.pasteHTML("abc");

不使用剪贴板可能更可靠的方法是将 HTML 片段保存在文件中并使用 InsertFile。像这样的东西:

public static void insertHTML(this Range range, string html) {
    string path = System.IO.Path.GetTempFileName();
    System.IO.File.WriteAllText(path, "<html>" + html); // must start with certain tag to be detected as html: <html> or <body> or <table> ...
    range.InsertFile(path, ConfirmConversions: false);
    System.IO.File.Delete(path); }

This worked for me on Windows 7 and Word 2007:

public static void pasteHTML(this Range range, string html)
{
    Clipboard.SetData(
      "HTML Format",
      string.Format("Version:0.9\nStartHTML:80\nEndHTML:{0,8}\nStart" + "Fragment:80\nEndFragment:{0,8}\n", 80 + html.Length) + html + "<");
      range.Paste();
}

Sample use: range.pasteHTML("a<b>b</b>c");

Probably a bit more reliable way without using the clipboard is to save the HTML fragment in a file and use InsertFile. Something like:

public static void insertHTML(this Range range, string html) {
    string path = System.IO.Path.GetTempFileName();
    System.IO.File.WriteAllText(path, "<html>" + html); // must start with certain tag to be detected as html: <html> or <body> or <table> ...
    range.InsertFile(path, ConfirmConversions: false);
    System.IO.File.Delete(path); }
柠檬心 2024-08-30 15:54:00

在word文档中添加html有点棘手。最好的方法是创建一个临时文件,然后将该文件插入到单词的选定范围。诀窍是利用 RangeInsertFile 函数。这允许我们插入任意 HTML 字符串,方法是首先将它们作为文件保存到磁盘上的临时位置。

唯一的技巧是< html>> 必须是根元素
文档。

我在我的一个项目中使用了类似的东西。

   private static string HtmlHeader => "<html lang='en' xmlns='http://www.w3.org/1999/xhtml'><head><meta charset='utf-8' /></ head >{0}</html>";
   public static string GenerateTemporaryHtmlFile(Range range,string htmlContent)
    {
        string path = Path.GetTempFileName();
        File.WriteAllText(path, string.Format(HtmlHeader , htmlContent));
        range.InsertFile(FileName: tmpFilePath, ConfirmConversions: false);
        return path;
    }

添加很重要

字符集='utf-8'

到 html 文件的头部,否则插入文件后,您可能会在 Word 文档中看到意外的字符。

It is kind of tricky to add the html in a word document. The best way is creating a temporary file and than insert this file to selected range of the word. The trick is to leverage the InsertFile function of the Range. This allows us to insert arbitrary HTML strings by first saving them as files to a temporary location on disk.

The only trick is that < html/> must be the root element of the
document.

I use something like this at one of my project.

   private static string HtmlHeader => "<html lang='en' xmlns='http://www.w3.org/1999/xhtml'><head><meta charset='utf-8' /></ head >{0}</html>";
   public static string GenerateTemporaryHtmlFile(Range range,string htmlContent)
    {
        string path = Path.GetTempFileName();
        File.WriteAllText(path, string.Format(HtmlHeader , htmlContent));
        range.InsertFile(FileName: tmpFilePath, ConfirmConversions: false);
        return path;
    }

it is important to adding

charset='utf-8'

to head of html file other wise you may see unexpected characters at your word document after you insert file.

ぃ双果 2024-08-30 15:54:00

只需使用您的 html 内容构建一个临时 html 文件并将其插入,如下所示。

// 1- Sample HTML Text
var Html = @"<h1>Sample Title</h1><p>Lorem ipsum dolor <b>his sonet</b> simul</p>";

// 2- Write temporary html file
var HtmlTempPath = Path.Combine(Path.GetTempPath(), $"{Path.GetRandomFileName()}.html");
File.WriteAllText(HtmlTempPath, $"<html>{Html}</html>");

// 3- Insert html file to word
ContentControl ContentCtrl = Document.ContentControls.Add(WdContentControlType.wdContentControlRichText, Missing);
ContentCtrl.Range.InsertFile(HtmlTempPath, ref Missing, ref Missing, ref Missing, ref Missing);

Just build a temporary html file with your html content and insert it like below.

// 1- Sample HTML Text
var Html = @"<h1>Sample Title</h1><p>Lorem ipsum dolor <b>his sonet</b> simul</p>";

// 2- Write temporary html file
var HtmlTempPath = Path.Combine(Path.GetTempPath(), $"{Path.GetRandomFileName()}.html");
File.WriteAllText(HtmlTempPath, $"<html>{Html}</html>");

// 3- Insert html file to word
ContentControl ContentCtrl = Document.ContentControls.Add(WdContentControlType.wdContentControlRichText, Missing);
ContentCtrl.Range.InsertFile(HtmlTempPath, ref Missing, ref Missing, ref Missing, ref Missing);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文