Why Use C#/VB.NET to Convert HTML to Word?
Save HTML file content into Word document can be very easy only through copy and paste. Or users can right click on the html document and then choose edit. It automatically opens in Word. Then you can save it as a Word .doc file. But first, “Copy and paste” method can only used on text-only pages or some very basic HTML pages. For complex HTML, it may happen to formatting is not rendered properly, images are placed incorrectly, sometimes even missing, and CSS layout seems to be ignored completely. Second, edit on HTML document also will lead CSS ignored. But with C#/VB.NET, save files from HTML to Word could be much better. Furthermore, it doesn’t need Microsoft Office installed on your system.
How to Use C#/VB.NET to Convert HTML to Word?
With a .NET library, we can use C#/VB.NET convert HTML to Word effortlessly. Spire.Doc is a MS Word component which enables user to perform a wide range of Word document processing tasks directly for .NET and Silverlight. Like Microsoft Word can help you save Word files to different format, Spire.Doc also support converting files from Word Doc to HTML, PDF, XML, RTF, Text, Docx, Dot, etc. Furthermore, besides converting files from Word to other popular file format, Spire.Doc can also transfer files from HTML to Doc, XML to Doc, RTF to Doc, Text to Doc, Docx to Doc, Dot to Doc, etc.
Download Spire.Doc (or Spire.Office) with .NET framework together and follow the simple steps below to convert HTML to Word.
Step 1: Create a project in Visual Studio and add Spire.Doc as reference.
Step 2: Load the HTML file which will be converted to Word doc file by using the follow code:
Document document = new Document();
document.LoadFromFile(@"D:\Work\Stephen\2011.12.06\test.html",FileFormat.Html,XHTMLValidationType.None);
Step 3: The following code below can help us convert the HTML file to Word doc. Furthermore, Spire.Doc also enables convert HTML to PDF, XML, ePub, Text, Dot, etc.
document.SaveToFile("test.doc", FileFormat.Doc);
Step 4: Write the whole simple code into the project and press F5 to start the conversion.
Full code:
[C#]
using System;
using Spire.Doc;
using Spire.Doc.Documents;
namespace Html2Doc
{
class Program
{
static void Main(string[] args)
{
Document document = new Document();
document.LoadFromFile(@"D:\test.html",FileFormat.Html,XHTMLValidationType.None);
document.SaveToFile("test.doc", FileFormat.Doc);
}
}
}
[VB.NET]
Imports System
Imports Spire.Doc
Imports Spire.Doc.Documents
Namespace Html2Doc
Friend Class Program
Shared Sub Main(ByVal args() As String)
Dim document As New Document()
document.LoadFromFile("D:\test.html",FileFormat.Html,XHTMLValidationType.None)
document.SaveToFile("test.doc", FileFormat.Doc)
End Sub
End Class
End Namespace
The output Word doc file can be find in Bin folder
Effective Screeshot: