ASP.NET MVC 2视图(视图模型) - GT; MS Word或PDF生成? [英] ASP.NET MVC 2 View (ViewModel) -> MS Word or PDF Generation?

查看:140
本文介绍了ASP.NET MVC 2视图(视图模型) - GT; MS Word或PDF生成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还想在MS Word和PDF两种格式我的MVC 2应用程序生成报告....目前正在字。我发现这一点:

Id like to have my mvc 2 app generating reports in both MS Word and PDF formats....currently working on Word. I found this:

http://www.revium.com.au/articles/sandbox/aspnet-mvc-convert-view-to-word-document/

我觉得基本上流从一个控制器动作输出视图到一个Word文档....

Which i think basically streams the view output from a controller action to a word document....

public ActionResult DetailedReport()  
    {            
        //[...]    
        return View();  
    }  


public ActionResult DetailedReportWord()  
    {  
        string url = "DetailedReport";  
        return new WordActionResult(url, "detailed-report.doc");  
    }  

和继承人扩展的ActionResult类

And heres the class that extends ActionResult

public class WordActionResult : ActionResult   
{   private string Url { get; set;}          
private string FileName { get; set;}  


public WordActionResult(string url, string fileName)  
{  
    Url = url;  
    FileName = fileName;  
}  

public override void ExecuteResult(ControllerContext context)  
{  
    var curContext = HttpContext.Current;  
    curContext.Response.Clear();  
    curContext.Response.AddHeader("content-disposition", "attachment;filename=" + FileName);  
    curContext.Response.Charset = "";  
    curContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);  
    curContext.Response.ContentType = "application/ms-word";  

    var wreq = (HttpWebRequest) WebRequest.Create(Url);  
    var wres = (HttpWebResponse) wreq.GetResponse();  
    var s = wres.GetResponseStream();  
    var sr = new StreamReader(s, Encoding.ASCII);  

    curContext.Response.Write(sr.ReadToEnd());  
    curContext.Response.End();  
}  

}

它看起来pretty不错 - 但我的问题是,我的观点从一个视图模型渲染,这里是行动

Which looks pretty good - but my problem is that my view renders from a ViewModel, here is the Action

[HttpPost]  
    public ActionResult StartSearch(SearchResultsViewModel model)  
    {  
        SearchResultsViewModel resultsViewModel = _searchService.Search(model.Search, 1, PAGE_SIZE);  
        return View("Index", resultsViewModel);  
    }  

和这里是我的模型:

public class SearchResultsViewModel  
{  
[SearchWordLimit]  
public string Search { get; set; }  

public IEnumerable<Employee> Employees { get; private set; }  
public IEnumerable<Organization> Organizations { get; private set; }  
public PageInfo PageInfo { get; private set;}  

public SearchResultsViewModel()  
{  
}  

public SearchResultsViewModel(string searchString, IEnumerable<Employee> employees, IEnumerable<Organization> organizations, PageInfo pageInfo)  
{  
    Search = searchString;  
    Employees = employees;  
    Organizations = organizations;  
    PageInfo = pageInfo;  
}  
}  

我有麻烦还挺连接两个 - 用我的视图模型为PDF流视图

I'm having trouble kinda connecting the two - to stream the view using my viewmodel to pdf

推荐答案

有什么出ASP.NET MVC的框,让你建立从POCO类PDF或WORD文件。您必须手动或使用第三方库的构建。一旦你做到了这一点,你可以很容易地写字节响应流:

There's nothing out of the box in ASP.NET MVC that allows you to build a PDF or Word file from a POCO class. You have to build it manually or using a third party library. Once you have done this you could easily write the bytes to the response stream:

public ActionResult SomeAction(SearchResultsViewModel model)
{
    byte[] pdf = GeneratePdfForModel(model);
    return File(pdf, "application/pdf");
}

GeneratePdfForModel 方法当然会具体您使用生成文档库什么/ API。

The GeneratePdfForModel method will of course be specific on what library/API you are using to generate the document.

这篇关于ASP.NET MVC 2视图(视图模型) - GT; MS Word或PDF生成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆