Created
January 30, 2014 03:52
-
-
Save bjcull/8702230 to your computer and use it in GitHub Desktop.
A Filter Attribute that lets you download an ASP.NET MVC View as a Word Document
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class WordDocumentAttribute : ActionFilterAttribute | |
{ | |
public string DefaultFilename { get; set; } | |
public override void OnActionExecuted(ActionExecutedContext filterContext) | |
{ | |
var result = filterContext.Result as ViewResult; | |
if (result != null) | |
result.MasterName = "~/Views/Shared/_LayoutWord.cshtml"; | |
filterContext.Controller.ViewBag.WordDocumentMode = true; | |
base.OnActionExecuted(filterContext); | |
} | |
public override void OnResultExecuted(ResultExecutedContext filterContext) | |
{ | |
var filename = filterContext.Controller.ViewBag.WordDocumentFilename; | |
filename = filename ?? DefaultFilename ?? "Document"; | |
filterContext.HttpContext.Response.AppendHeader("Content-Disposition", string.Format("filename={0}.doc", filename)); | |
filterContext.HttpContext.Response.ContentType = "application/msword"; | |
base.OnResultExecuted(filterContext); | |
} | |
} |
After generating word document, if there is a apostrophe in a word then it render as ’ . Can you help me to fix this issue. Thanks in advance.
Is there a possibility to give it a custom CSS? He can't find the css files from my project.
thank useful code 💃
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great article,how can be modified this for pdf document and how can i choose download folder?