使用FastReport.net和asp.net导出pdf

时间:2013-08-07 10:19:11

标签: c# asp.net-mvc fastreport

如何使用pdfFastReport.net导出asp.net? 我想在控制器中导出文件。我试过这种方式在FastReport网站上支持:

public FileResult GetFile()
        {
            WebReport webReport = new WebReport();

            // bind data
            System.Data.DataSet dataSet = new System.Data.DataSet();
            dataSet.ReadXml(report_path + "nwind.xml");
            webReport.Report.RegisterData(dataSet, "NorthWind");

            // load report
            webReport.ReportFile = this.Server.MapPath("~/App_Data/report.frx");

            // prepare report
            webReport.Report.Prepare();

            // save file in stream
            Stream stream = new MemoryStream();
            webReport.Report.Export(new PDFExport(), stream);
            stream.Position = 0;

            // return stream in browser
            return File(stream, "application/zip", "report.pdf");
        }

但是pdf的大小总是0字节。

有人知道我的问题的解决方案吗?

2 个答案:

答案 0 :(得分:8)

好的,现在我找到了解决方案。只需使用普通Report(不是WebReport)并将WebMode设置为true即可。 pdf-Export上的其他设置只是为了好玩。

所以,这将解决问题:

public FileResult GetFile(Dataset dataset1)
{
    FastReport.Utils.Config.WebMode = true;
    Report rep = new Report();
    rep.Load(Request.PhysicalApplicationPath + "App_Data/report.frx");    

    rep.RegisterData(dataset1);

    if (rep.Report.Prepare())
    {
        // Set PDF export props
        FastReport.Export.Pdf.PDFExport pdfExport = new FastReport.Export.Pdf.PDFExport();
        pdfExport.ShowProgress = false;
        pdfExport.Subject = "Subject";
        pdfExport.Title = "xxxxxxx";
        pdfExport.Compressed = true;
        pdfExport.AllowPrint = true;
        pdfExport.EmbeddingFonts = true;

        MemoryStream strm = new MemoryStream();
        rep.Report.Export(pdfExport, strm);
        rep.Dispose();
        pdfExport.Dispose();
        strm.Position = 0;

        // return stream in browser
        return File(strm, "application/pdf", "report.pdf");
    }
    else
    {
        return null;
    } 
}

遗憾的是,这些代码模板在开发者的官方网站上是错误的。

答案 1 :(得分:0)

2017.1为我工作

    public void GetFile()
    {
        SetReport();
        webReport.ExportPdf();
    }

    public void GetPrint()
    {
        SetReport();
        webReport.Prepare();
        webReport.PrintPdf();
    }
相关问题