Tuesday, 31 July 2012

HTML To Word Document using OpenXMl



Download OpenXMLSDKTool.msi  and run it, you will  get DocumentFormat.OpenXml.dll 

From this link you will get HTMLtoOpenXML.dll

Add these two reference.

using System.IO;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using NotesFor.HtmlToOpenXml;
...

static void Main(string[] args)
{
     const string filename = "D:\test.docx";
     string html = "<font size='10' face='arial'>Hi this is html to word convert</font>" ;

     if (File.Exists(filename)) File.Delete(filename);

     using (MemoryStream generatedDocument = new MemoryStream())
     {
          using (WordprocessingDocument package = WordprocessingDocument.Create(generatedDocument, WordprocessingDocumentType.Document))
          {
               MainDocumentPart mainPart = package.MainDocumentPart;
               if (mainPart == null)
               {
                    mainPart = package.AddMainDocumentPart();
                    new Document(new Body()).Save(mainPart);
               }

               HtmlConverter converter = new HtmlConverter(mainPart);
               Body body = mainPart.Document.Body;

               var paragraphs = converter.Parse(html);
               for (int i = 0; i < paragraphs.Count; i++)
               {
                    body.Append(paragraphs[i]);
               }

               mainPart.Document.Save();
          }

          File.WriteAllBytes(filename, generatedDocument.ToArray());
     }

     System.Diagnostics.Process.Start(filename);
}
                                                                 





No comments:

Post a Comment