开发者

How can I write HTML in a Word document?

开发者 https://www.devze.com 2023-04-09 15:42 出处:网络
How can I write HTML in a Word document using C#? I made a class to help writing a document using System;

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);
            }
        }
开发者_JS百科
        #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...


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

range.InsertFile("file.html");
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号