循环生成Crystal Reports并保存为zip文件并下载

时间:2019-04-18 11:37:15

标签: c# asp.net-mvc crystal-reports

我想通过记录ID生成多个报告,并保存到一个zip文件中并下载。目前,我无法为单个报告生成报告,现在我试图循环生成报告并添加到zip文件中。任何帮助都是可取的。

DataSet ds = new DataSet("ReportDs");
DataTable dtSMD = GetSurveyMaster(smdId, "SMD");
ds.Tables.Add(dtSMD);
smdReport report = new smdReport();
report.FileName = Server.MapPath("~/Views/Report/smdReport.rpt");
report.Load();
string fileName = $"smdReport{smdId}_{DateTime.Now.ToString()}";
report.SetDataSource(ds);
Stream stream = report.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);

1 个答案:

答案 0 :(得分:1)

将上面的逻辑移至名为CreateReportPdf()的方法以返回流,然后使用诸如DotnetZip之类的库,您可以执行以下操作:

using (ZipFile zip = new ZipFile())
{
    foreach (var smdId in reports)
    {
        string fileName = $"smdReport{smdId}_{DateTime.Now.ToString()}";
        using (var report = CreateReportPDF(smdId))
        {
            // convert stream to archive
            zip.AddEntry($"{fileName}", report.ToArray());
        }
    }
    zip.Save("YourZip.zip");
}

您的CreateReportPDF应该返回一个MemoryStream

var ms = new MemoryStream();
stream.CopyTo(ms);
ms.Position = 0;
return ms;

请注意,我使用您的filename逻辑在zip归档文件中生成文件的名称