One request we always seem to come across as developers is “Can we have a version of the page avaliable as a Word Document?”. With traditional ASP.Net this is easy however doing the same in Asp .Net MVC is not that simple. 

One request we always seem to come across as developers is “Can we have a version of the page avaliable as a Word Document?”. With traditional ASP.Net this is easy. Simply place something similar to the following code block in your Page_Load.

Response.Buffer = true;
Response.ContentType = "application/ms-word";
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);

And that was it. Now using Asp.Net MVC it isn’t that simple. We somehow need to return this data in the form of an ActionResult. I did this by creating: 2 Controllers 1 View and 1 new ActionResult Class.

First the 2 Views: DetailedReport and DetailedReportWord.

/// <summary>
/// This view is used to display the web version of the report as a web page in the users browser
/// This Controller has a View named DetailedReport
/// <summary>
public ActionResult DetailedReport()
    return View();

/// <summary>
/// This view is used convert the DetailedReport view into a Word Document and send it to the user
/// This Controller does not have an associated View
/// <summary>
public ActionResult DetailedReportWord()
    RouteValueDictionary rvd = new RouteValueDictionary();

    string url = Url.Action("DetailedReport", "Reports", rvd, "http");
    return new WordActionResult(url, "detailed-report.doc");

Now for the new ActionResult Class: WordActionResult. This class will take a URL and execute a HttpRequest. The resulting content is then streamed to the users as a Word Document

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.UI;

namespace Revium.Utilities.MVC
    public class WordActionResult : ActionResult
        private string _url;
        public string url
            get { return _url; }

        private string _filename;
        public string fileName
            get { return _filename; }

        public WordActionResult(string pUrl, string pFileName)
            _url = pUrl;
            _filename = pFileName;

        public override void ExecuteResult(ControllerContext context)
            HttpContext curContext = HttpContext.Current;
            curContext.Response.AddHeader("content-disposition", "attachment;filename=" + _filename);
            curContext.Response.Charset = "";
            curContext.Response.ContentType = "application/ms-word";

            HttpWebRequest wreq = (HttpWebRequest)HttpWebRequest.Create(_url);

            // If we are using forms authentication we need to get the .ASPXAUTH cookie and add it to our request
            var httpCookie = context.HttpContext.Request.Cookies[".ASPXAUTH"];
            wreq.CookieContainer = new CookieContainer();
            wreq.CookieContainer.Add(new Cookie(httpCookie.Name, httpCookie.Value, httpCookie.Path, context.HttpContext.Request.Url.Host));

            HttpWebResponse wres = (HttpWebResponse)wreq.GetResponse();

            using (Stream s = wres.GetResponseStream())
                using (StreamReader sr = new StreamReader(s, Encoding.ASCII))

Now you may notice that ExecuteResult is a little different to the example i gave right at the start. This is because we are not converting the current view to a Word document. We are using the DetailedResultWord Controller to convert DetaileResult to a Word document.

This is just a very simple implementation but it has certainly given us the stepping stones required to convert MVC Views to Word and Excel documents.

You may also like

Newsletter sign up

Every couple of months we send out an update on what's been happening around our office and the web. Sign up and see what you think. And of course, we never spam.