문제

ActiveReports 보고서 문서를 ASP.NET MVC 앱의 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 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:

var exportType = Request.Form["exportType"];
switch (exportType)
{
  case "RTF":
    return new RtfExportResult(reportBase.Report.Document);
  case "TIFF":
    return new TiffExportResult(reportBase.Report.Document);
  case "XLS":
    return new XlsExportResult(reportBase.Report.Document);
}

return View("Error"); // unsupported export type

Then your XlsExportResult would look like:

public class XlsExportResult : ActionResult
{
    private readonly Document document;

    public XlsExportResult(Document document)
    {
        this.document= document;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        var response = context.HttpContext.Response;
        response.ContentType = "application/octet-stream";
        response.AddHeader("Content-Disposition", "attachment;filename=report.xls");
        var xlsExport = new XlsExport();
        xlsExport.Export(this.document, response.OutputStream);
    }
}

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 응용 프로그램에서 ActiveEreports를 사용하여 보고서를 내보내는 방법에 대한 자세한 내용을 참조하십시오.

http://blogs.gcpowertools.co.in/2012/02/exporting-reports-created-using.html

이 블로그는 따라야하는 단계와 정확히 수행하는 방법을 자세히 설명합니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top