ASP.NET MVC 2 视图(ViewModel)-> MS Word 或 PDF 生成?
我想让我的 mvc 2 应用程序生成 MS Word 和 PDF 格式的报告......目前正在 Word 上工作。我发现了这个:
http:// /www.revium.com.au/articles/sandbox/aspnet-mvc-convert-view-to-word-document/
我认为基本上将视图输出从控制器操作流式传输到Word文档... 这看起来不错 - 但我的问题是我的视图是从 ViewModel 渲染的
public ActionResult DetailedReport()
{
//[...]
return View();
}
public ActionResult DetailedReportWord()
{
string url = "DetailedReport";
return new WordActionResult(url, "detailed-report.doc");
}
这是扩展 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();
}
}
,这是 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
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/
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");
}
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();
}
}
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);
}
and here is my model:
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;
}
}
I'm having trouble kinda connecting the two - to stream the view using my viewmodel to pdf
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ASP.NET MVC 中没有任何现成的东西可以让您从 POCO 类构建 PDF 或 Word 文件。您必须手动构建它或使用第三方库。完成此操作后,您可以轻松地将字节写入响应流:
GeneratePdfForModel
方法当然会特定于您用于生成文档的库/API。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:
The
GeneratePdfForModel
method will of course be specific on what library/API you are using to generate the document.诀窍是从 MS-Word 文件生成 PDF 文件。为此,您很可能需要第三方组件。
如果您不需要完美的转换保真度,只需要“足够好”,那么请尝试 Aspose.Words。如果您需要完美的转换保真度,请尝试我曾经开发过的产品,它允许使用 Web 服务界面将所有典型的 MS-Office 类型转换为 PDF 格式。
The trick is to generate the PDF file from the MS-Word file. You most likely need a 3rd party component for this.
If you don't need perfect conversion fidelity, just something that is 'good enough', then try Aspose.Words. If you need perfect conversion Fidelity then try a product I have worked on, it allows the conversion of all typical MS-Office types to PDF format using a web services interface.
我对此有点晚了,但从我所看到的来看:
您的网址指向同一个操作,它应该指向您的“StartSearch”操作,因为这是生成您想要的 HTML 的操作用Word打开。我认为它也需要是一个完整的 URL。
我也在尝试使用这个方法,我从你的问题中找到了它!然而,我在传递源视图的 WebRequest.Create(Url) 中的授权凭据时遇到问题。
I'm a bit late to this, but from what I can see:
Your url is pointing back to the same Action, it should be pointing at your "StartSearch" action as that is the one that produces the HTML you are wanting to be opened by Word. It needs to be a full URL too I think.
I am trying to use this method too, I found it from your question! I'm having issues passing over the authorised credentials in the WebRequest.Create(Url) for the source View however.