如何在Word文档中编写HTML?

发布于 2024-12-07 07:02:43 字数 2600 浏览 1 评论 0原文

如何使用 C# 在 Word 文档中编写 HTML?

我创建了一个类来帮助编写文档

using System;
using System.IO;
using Microsoft.Office.Interop.Word;

namespace WordExporter
{
    public class WordApplication : IDisposable
    {
        private Application application;
        private Document document;

        private string path;
        private bool editing;

        public WordApplication(string path)
        {
            this.path = path;
            this.editing = File.Exists(path);

            application = new Application();

            if (editing)
            {
                document = application.Documents.Open(path, ReadOnly: false, Visible: false);
            }
            else
            {
                document = application.Documents.Add(Visible: false);
            }
            document.Activate();
        }

        public void WriteHeader(string text)
        {
            foreach (Section wordSection in document.Sections)
            {
                var header = wordSection.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;

                header.Font.ColorIndex = WdColorIndex.wdDarkRed;
                header.Font.Size = 20;
                header.Text = text;
            }
        }

        public void WriteFooter(string text)
        {
            foreach (Section wordSection in document.Sections)
            {
                var footer = wordSection.Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;

                footer.Font.ColorIndex = WdColorIndex.wdDarkRed;
                footer.Font.Size = 20;
                footer.Text = text;
            }
        }

        public void Save()
        {
            if (editing)
            {
                application.Documents.Save(true);
            }
            else
            {
                document.SaveAs(path);
            }
        }

        #region IDisposable Members

        public void Dispose()
        {
            ((_Document)document).Close(SaveChanges: true);
            ((_Application)application).Quit(SaveChanges: true);
        }

        #endregion
    }

    class Program
    {
        static void Main(string[] args)
        {
            using (var doc = new WordApplication(Directory.GetCurrentDirectory() + "\\test.docx"))
            {
                doc.WriteHeader("<h1>Header text</h1>");
                doc.WriteFooter("<h1>Footer text</h1>");
                doc.Save();
            }
        }
    }
}

WriteHeader 中,我在文档标题上写了一些文本,但我需要使用 HTML。我怎么能说内容是HTML呢?我还需要在文档内容中插入 HTML...

How can I write HTML in a Word document using C#?

I made a class to help writing a document

using System;
using System.IO;
using Microsoft.Office.Interop.Word;

namespace WordExporter
{
    public class WordApplication : IDisposable
    {
        private Application application;
        private Document document;

        private string path;
        private bool editing;

        public WordApplication(string path)
        {
            this.path = path;
            this.editing = File.Exists(path);

            application = new Application();

            if (editing)
            {
                document = application.Documents.Open(path, ReadOnly: false, Visible: false);
            }
            else
            {
                document = application.Documents.Add(Visible: false);
            }
            document.Activate();
        }

        public void WriteHeader(string text)
        {
            foreach (Section wordSection in document.Sections)
            {
                var header = wordSection.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;

                header.Font.ColorIndex = WdColorIndex.wdDarkRed;
                header.Font.Size = 20;
                header.Text = text;
            }
        }

        public void WriteFooter(string text)
        {
            foreach (Section wordSection in document.Sections)
            {
                var footer = wordSection.Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;

                footer.Font.ColorIndex = WdColorIndex.wdDarkRed;
                footer.Font.Size = 20;
                footer.Text = text;
            }
        }

        public void Save()
        {
            if (editing)
            {
                application.Documents.Save(true);
            }
            else
            {
                document.SaveAs(path);
            }
        }

        #region IDisposable Members

        public void Dispose()
        {
            ((_Document)document).Close(SaveChanges: true);
            ((_Application)application).Quit(SaveChanges: true);
        }

        #endregion
    }

    class Program
    {
        static void Main(string[] args)
        {
            using (var doc = new WordApplication(Directory.GetCurrentDirectory() + "\\test.docx"))
            {
                doc.WriteHeader("<h1>Header text</h1>");
                doc.WriteFooter("<h1>Footer text</h1>");
                doc.Save();
            }
        }
    }
}

In the WriteHeader I write some text on the document header, but I need to use HTML. How can I say the contents are HTML? I will also need to insert HTML in the document content...

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

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

发布评论

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

评论(1

原谅过去的我 2024-12-14 07:02:43

我可以在我想要使用的部分插入 html 文件:

range.InsertFile("file.html");

I can just insert the html file on the section I want using:

range.InsertFile("file.html");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文