Convert HTML to Microsoft Word.doc in .NET.

What is the best way to convert HTML to MS Word.doc in .NET?

Third party components?

+1
source share
2 answers

try using Office Interop

private void _convetHTML2Doc( string FileNameUpload) { string filePath = Server.MapPath("~/htmlfile"); object missing = Type.Missing; object FileName = @"D:\" + "\\" + FileNameUpload; object readOnly = true; m_word = new Application(); m_word.Documents.Open(ref FileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); string newfilename = filePath + "\\" + FileNameUpload.Replace(".html", ".doc"); object o_newfilename = newfilename; object o_encoding = Microsoft.Office.Core.MsoEncoding.msoEncodingUTF8; object o_format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument; object o_endings = Microsoft.Office.Interop.Word.WdLineEndingType.wdCRLF; m_word.ActiveDocument.SaveAs(ref o_newfilename, ref o_format, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref o_encoding, ref missing, ref missing, ref o_endings, ref missing); m_word.Quit(ref missing, ref missing, ref missing); System.Runtime.InteropServices.Marshal.ReleaseComObject(m_word); } 
+1
source

MS warns about Word automation when it is not viewed / controlled / controlled by the end user, for example. because a message box may appear; therefore, if you want to do this on the server, then a third-party component might be better than automating Word.

You can also learn to write the Word document format yourself (this is a documented XML format), but this (learning and writing this format) is probably more of a problem than you want.

Also note that Word can open HTML: therefore, to some (possibly small) extent, HTML is already a Word document.

+1
source

All Articles