Sunday 5 August 2012

Three Steps to Save Html File as Word Document with C#, VB.NET

Introduction
Html format has its specified character which cannot be replaced by other formats. So we often need to convert one format to multiple formats. Thus, the conversion software or applications with the conversion function is necessary. For word application Spire.Doc users, it not only can save word as different formats but also can be converted by other formats, such as html and xml. Today, I am glad to share with the method how to save  html as word with C#,VB.NET.
You may say that you do not need to save html as word with C#/VB.NET since you can use the copy and paste function, or you can right click the html file to edit it and then it automatically opened as word document. Both ways can realize the conversion. In some sense, you are right. However, for some basic and simple html files without image, you can easily do that through the methods above. But for those have images and a little complex, that measure is not available. When you edit the html, CSS is very likely to be ignored. So, using the method of save html as word with C#,VB.NET becomes a best and most reliable mean. I use Spire.Doc to help me finish this task in a fast way.


Procedure

Step1. Load a html file from the system.

Step2. Convert HTML to Word.

Step3. Preview the effect.


Main Code:
C# Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spire.Doc;
using Spire.Doc.Documents;
namespace html_to_word
{
    class Program
    {
        static void Main(string[] args)
        {
            Document document = new Document();
            document.LoadFromFile(@"E:\TestScript\Sample.html", FileFormat.Html, XHTMLValidationType.None);
            document.SaveToFile("test.doc", FileFormat.Doc);
        }
    }
}
 
VB.NET Code:

Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
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("E:\TestScript\Sample.html",FileFormat.Html,XHTMLValidationType.None)
                       document.SaveToFile("test.doc", FileFormat.Doc)
               End Sub
        End Class
End Namespace

Preview

                   
From the above procedure, you can obviously see that using Spire.Doc to save html as word with C#,VB.NET is not more complex than copy and paste function and edit methods. It is the most effective way to help people realize the conversion function. Not only html to word and xml to word conversion, but also word to epub, html, xml, tiff, rtf, tiff image, image and so on.


No comments:

Post a Comment

Popular posts