循环生成 Crystal 份报告并保存在 zip 文件中并下载
Generate Crystal Reports in loop and save in a zip file and download
我想通过记录 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);
将你上面的逻辑移动到一个名为 CreateReportPdf()
的方法到 return 流,然后使用像 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
应该return一个MemoryStream
var ms = new MemoryStream();
stream.CopyTo(ms);
ms.Position = 0;
return ms;
请注意,我使用了您的 filename
逻辑来生成 zip 存档中的文件名
我想通过记录 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);
将你上面的逻辑移动到一个名为 CreateReportPdf()
的方法到 return 流,然后使用像 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
应该return一个MemoryStream
var ms = new MemoryStream();
stream.CopyTo(ms);
ms.Position = 0;
return ms;
请注意,我使用了您的 filename
逻辑来生成 zip 存档中的文件名