Creating a Word document from HTML

12,916

Solution 1

I have used essentially the same code as you to download html as word documents, and it works fine. I modified my code so that it was the same as yours to test, and it still worked OK, so I wonder if the issue is actually with your HTML.

Have a look at doc.DocumentBody in your debugger, and see if it is valid html.

Is it wrapped in <html><body></body></html>?

I had a test - I think if you leave out the body tags, you'll end up seeing raw html.

Solution 2

Microsoft.Office.Interop is not designed to be used for server automation since this is just a wrapper around the application and would require Office to be installed on the web server. Is this correct?

Yes.

What am I missing here to get my HTML to appear rendered in the MS Word output file?

Well, you need to create a Word document, of course! Word's file format and the HTML file format are different.

There are some very good commercial libraries out there that provide a nice API for generating Office documents programmatically. With Office XML, this is not quite as necessary - it's now much more feasible to generate the XML that Word knows how to read.

Solution 3

  1. yes, and running Office applications on server without UI is not supported. (Note: "not supported" does not mean it will not work, but simply no guarantees of any kind made).

  2. use File method to return file - http://msdn.microsoft.com/en-us/library/dd505200.aspx, Check out this popular answer - How can I present a file for download from an MVC controller?.

Share:
12,916
MaseBase
Author by

MaseBase

I've been the lead software engineer and product manager for multiple companies. I am primarily a .NET C# guy with a pretty good grasp, always more to learn. Also experienced in: HTML, CSS, Javascript, jQuery, Java, Android, Windows Server administration, IIS...

Updated on June 05, 2022

Comments

  • MaseBase
    MaseBase almost 2 years

    I know there are a lot of other questions on SO about this topic, but I need some more information. It's a two-part question to my requirement: dynamically generate an MS Word document from HTML and prompt for download.

    Q1) From what I'm reading it seems that Microsoft.Office.Interop is not designed to be used for server automation since this is just a wrapper around the application and would require Office to be installed on the web server. Is this correct?

    I have gotten some of this to work, I get prompted to download, the Word doc saves properly, but the doc shows my markup as the content of the document, not the rendered HTML as the content. From what I've read, it's supposedly possible to export HTML to MS Word simply like this without the need for 3rd party tools or components. I'd also like to avoid the Open XML format as I can't guarantee which version of Word my users have.

    Q2) What am I missing here to get my HTML to appear rendered in the MS Word output file? doc.DocumentBody is a string type that contains the entire HTML document.

        public FileStreamResult DownloadDocument(string id)
        {
            /* pseudo-code here to fetch my custom "Document" object from DB */
            Document doc = DocumentService.FindById(id);
    
            var fileName = string.Format("{0}.doc", doc.Title);
            Response.AddHeader("Content-Disposition", "inline;filename=" + fileName);
            return new FileStreamResult(WordStream(doc.DocumentBody), "application/msword");
        }
    
        private static Stream WordStream(string body)
        {
            var ms = new MemoryStream();
    
            byte[] byteInfo = Encoding.ASCII.GetBytes(body);
            ms.Write(byteInfo, 0, byteInfo.Length);
            ms.Position = 0;
    
            return ms;
        }