C# Convert HTML to Word Doc/Docx
An HTML document as all you know is a webpage. If someone wants to convert HTML to Word Doc/Docx, he/she can simply open it up using a web browser such as IE, Firefox, etc. and press Ctrl + A to select all HTML content and copy it into Word Doc/Docx file. But, please do not expect the Word Doc/Docx document looks exactly like the webpage no matter how you paste. You will find this solution is only available on text-only or with very basic HTML pages. Most of the time the formatting is not rendered properly, images may be placed incorrectly or even missed, furthermore, CSS layout could be ignored completely.
Here I will share another solution for converting HTML to Word Doc/Docx. This solution might also not make totally same on format after conversion, but it’s really worth all of you to give a shot. This solution which used C# code to convert HTML to Word Doc/Docx is very easy and fast. We will use .NET Word component Spire.Doc to do this conversion. Download Spire.Doc, and make sure Spire.Doc and Visual Studio are correctly installed on system. Follow the simple steps below:
Step 1 Create a Project
Create a C# project and add Spire.Doc.dll as reference. The default setting of Spire.Doc.dll is placed under "C:\Program Files\e-iceblue\Spire.Doc\Bin”. Select assembly Spire.Doc.dll and click OK to add it to the project.
using System;
using Spire.Doc;
namespace Html2Doc
{
class Program
{
static void Main(string[] args)
{
}
}
}
Step 2 Load HTML File
Put the HTML file which we need convert to Word Doc/Docx format into the project folder. Use the code below to load it.
Document doc = new Document();
doc.LoadFromFile("Sample.html", FileFormat.Html);
Step 3 C# HTML to Word Doc/Docx
The following C# code will help us easily convert HTML to Word Doc/Docx.
doc.SaveToFile("test.doc", FileFormat.Doc);
Full C# Code:
using System;
using Spire.Doc;
namespace Html2Doc
{
class Program
{
static void Main(string[] args)
{
Document doc = new Document();
doc.LoadFromFile("Sample.html", FileFormat.Html);
doc.SaveToFile("test.doc", FileFormat.Doc);
}
}
}
By using this solution, developers even don’t have to open the original HTML and output Word Doc/Docx file. What’s more, Spire.Doc can not only help convert HTML to Doc/Docx, it also supports converting XML to Word Doc/Docx, RTF to Word Doc/Docx, Text to Word Doc/Docx, Dot to Word Doc/Docx. Clicl to learn more…
Here I will share another solution for converting HTML to Word Doc/Docx. This solution might also not make totally same on format after conversion, but it’s really worth all of you to give a shot. This solution which used C# code to convert HTML to Word Doc/Docx is very easy and fast. We will use .NET Word component Spire.Doc to do this conversion. Download Spire.Doc, and make sure Spire.Doc and Visual Studio are correctly installed on system. Follow the simple steps below:
Step 1 Create a Project
Create a C# project and add Spire.Doc.dll as reference. The default setting of Spire.Doc.dll is placed under "C:\Program Files\e-iceblue\Spire.Doc\Bin”. Select assembly Spire.Doc.dll and click OK to add it to the project.
using System;
using Spire.Doc;
namespace Html2Doc
{
class Program
{
static void Main(string[] args)
{
}
}
}
Step 2 Load HTML File
Put the HTML file which we need convert to Word Doc/Docx format into the project folder. Use the code below to load it.
Document doc = new Document();
doc.LoadFromFile("Sample.html", FileFormat.Html);
Step 3 C# HTML to Word Doc/Docx
The following C# code will help us easily convert HTML to Word Doc/Docx.
doc.SaveToFile("test.doc", FileFormat.Doc);
Full C# Code:
using System;
using Spire.Doc;
namespace Html2Doc
{
class Program
{
static void Main(string[] args)
{
Document doc = new Document();
doc.LoadFromFile("Sample.html", FileFormat.Html);
doc.SaveToFile("test.doc", FileFormat.Doc);
}
}
}
By using this solution, developers even don’t have to open the original HTML and output Word Doc/Docx file. What’s more, Spire.Doc can not only help convert HTML to Doc/Docx, it also supports converting XML to Word Doc/Docx, RTF to Word Doc/Docx, Text to Word Doc/Docx, Dot to Word Doc/Docx. Clicl to learn more…