C#/VB.NET: Convert HTML to Word

HTML is the standard file format of webpage files that are designed to be displayed in web browsers. Due to the lack of full support for HTML elements in Word, most HTML files cannot be rendered properly in Word. If you do want to maintain the HTML layout while exporting it to Word, you need to change the HTML code and avoid using the elements, attributes, and cascading style sheet properties that are not supported. This article will show you how to convert simple HTML files to Word using Spire.Doc for .NET.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Convert HTML to Word

The detailed steps are as follows:

  • Create a Document instance.
  • Load an HTML file from disk using Document.LoadFromFile().
  • Convert the HTML file to Word and save it using Document.SaveToFile().
  • C#
  • VB.NET
using System;
using Spire.Doc;
using Spire.Doc.Documents;

namespace ConvertHTMLtoWord
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Create an instance of Document
            Document document = new Document();

            //Load an HTML file form disk
            document.LoadFromFile(@"D:\testp\test.html");
            

            //Save the HTML file as Word
            String result = "HtmltoWord.docx";
            document.SaveToFile(result, FileFormat.Docx2013);
        }
   }
}

C#/VB.NET: Convert HTML to Word

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.