如何在 ASP.Net MVC 应用程序中将 ActiveReport 导出到 XLS?
我不确定如何在我的 asp.net mvc 应用程序中将 ActiveReports 报告文档导出到 XLS。
到目前为止,我的想法是有一个导出类型的下拉列表和一个将该值提交到我的控制器的提交按钮。 当我在控制器上时,我重新生成报告并将其传递给我的导出方法。 我不确定此导出方法会返回什么。 我还在实际的 xlsExport.Export 方法上遇到了超出范围的错误。 以下是我的导出方法。 另请注意,reportBase.Report 是一个 ActiveReport3 对象。
private ActionResult Export(ReportBase reportBase)
{
Response.ClearContent();
Response.ClearHeaders();
var exportType = Request.Form["exportType"];
switch (exportType)
{
case "RTF":
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;filename=report.rtf");
var rtfExport = new RtfExport();
rtfExport.Export(reportBase.Report.Document, Response.OutputStream);
break;
case "TIFF":
Response.ContentType = "image/tiff";
Response.AddHeader("Content-Disposition", "attachment;filename=report.tif");
var tiffExport = new TiffExport();
var filePath = System.IO.Path.GetTempFileName();
tiffExport.Export(reportBase.Report.Document, filePath);
var fileStream = System.IO.File.Open(filePath, System.IO.FileMode.Open);
var bufferLength = (int)fileStream.Length;
var output = new byte[bufferLength];
var bytesRead = fileStream.Read(output, 0, bufferLength);
Response.OutputStream.Write(output, 0, bytesRead);
System.IO.File.Delete(filePath);
break;
case "XLS":
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;filename=report.xls");
var xlsExport = new XlsExport();
xlsExport.Export(reportBase.Report.Document, Response.OutputStream);
break;
}
Response.Flush();
Response.End();
return View("Display", reportBase);
}
I am unsure how to go about exporting my ActiveReports report document to XLS in my asp.net mvc app.
My concept so far is to have a dropdown of export types and a submit button that submits that value to my controller. When I'm on the controller, I regenerate the report and pass it to my Export method. I'm not sure what to have this Export method return. I'm also getting an out of range error on the actual xlsExport.Export method. Below is my Export method. Also to note, reportBase.Report is an ActiveReport3 object.
private ActionResult Export(ReportBase reportBase)
{
Response.ClearContent();
Response.ClearHeaders();
var exportType = Request.Form["exportType"];
switch (exportType)
{
case "RTF":
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;filename=report.rtf");
var rtfExport = new RtfExport();
rtfExport.Export(reportBase.Report.Document, Response.OutputStream);
break;
case "TIFF":
Response.ContentType = "image/tiff";
Response.AddHeader("Content-Disposition", "attachment;filename=report.tif");
var tiffExport = new TiffExport();
var filePath = System.IO.Path.GetTempFileName();
tiffExport.Export(reportBase.Report.Document, filePath);
var fileStream = System.IO.File.Open(filePath, System.IO.FileMode.Open);
var bufferLength = (int)fileStream.Length;
var output = new byte[bufferLength];
var bytesRead = fileStream.Read(output, 0, bufferLength);
Response.OutputStream.Write(output, 0, bytesRead);
System.IO.File.Delete(filePath);
break;
case "XLS":
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;filename=report.xls");
var xlsExport = new XlsExport();
xlsExport.Export(reportBase.Report.Document, Response.OutputStream);
break;
}
Response.Flush();
Response.End();
return View("Display", reportBase);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我对你的问题没有答案。 包含完整的异常消息会很有帮助。 我没有足够的信息来帮助您,但我会检查以确保 reportBase.Report.Document 不为空。
不过,我确实想对您的代码进行一般性评论。 您的控制器操作不遵循 ASP.NET MVC 的约定。 它不应该直接写入响应流。 首先,单元测试很难。 其次,它往往会使你的操作责任爆炸(它已经比我喜欢的最大控制器大大约 4 倍)。 Response.End 缩短了操作,而“return View()”什么也不做。 我会做类似的事情:
然后您的 XlsExportResult 看起来像:
然后您可以编写测试来更轻松地仅执行 XlsExport 部分。 (我还找到了一种将 XlsExport 隐藏在界面后面的方法。)通过一些创造力(为文件名等添加附加属性),您将能够在项目中重用 *Result 类。
I don't have an answer for your issue. Including the full exception message would be helpful. There's not enough information for me to help you, but I'd check to make sure reportBase.Report.Document isn't null.
However, I do want to comment on your code in general. Your controller action isn't following the conventions of ASP.NET MVC. It shouldn't be writing directly to the reponse stream. Firstly, it's hard to unit test. Second, it tends to make your action explode in responsibility (it's already about 4 times larger than I prefer my largest controllers to be) The Response.End is cutting the action short and the "return View()" does nothing. I would do something like:
Then your XlsExportResult would look like:
You could then write tests to exercise only the XlsExport part more easily. (I'd also find a way to hide XlsExport behind an interface.) With some creativity (adding additional prperties for things like file name, etc) you'll be able to reuse the *Result classes across your project.
您可以参考本博客文章中给出的示例应用程序,其中详细介绍了如何在 MVC 应用程序中使用 ActiveReports 导出报表。
http://blogs.gcpowertools.co.in/2012 /02/exporting-reports-created-using.html
此博客详细说明了您需要遵循的步骤以及具体的操作方法。
You can refer to the sample application given in this blog post which details about how to export reports using ActiveReports in a MVC application.
http://blogs.gcpowertools.co.in/2012/02/exporting-reports-created-using.html
This blog explains in detail that steps that you need to follow and exactly how to do it.