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;
}