在水晶报表中循环导出为PDF

时间:2018-10-06 15:18:40

标签: c# crystal-reports

在循环生成超过1个PDF的Crystal报表i-e时,我遇到了困难。基本上我创建的是PDF文件,然后压缩这些文件并生成HTTP响应。为了演示代码,我在这里运行了两次代码。

Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/zip";
Response.AppendHeader("content-disposition", "attachment; filename=Report.zip");
using (ZipFile zip = new ZipFile())
{                    
        for (int i = 0; i < 2; i++)
        {
            var re = rpt.ExportToStream(ExportFormatType.PortableDocFormat);
            string Name="PDF"+i+".pdf";
            zip.AddEntry(Name, re);
        }
        zip.Save(Response.OutputStream);                          
}

Response.Clear();

它成功生成了zip文件,但是当我尝试提取该zip文件时,我给出了一个错误No archive found (The archive is either in unknown formate or damaged).。顺便说一句,我点击了此链接Export Crystal Report to PDF in a Loop only works with first

1 个答案:

答案 0 :(得分:0)

如果zip文件未附加到本地路径,则使用memorystreem。

  

Response.ClearContent(); Response.ClearHeaders();
  Response.ContentType =“应用程序/邮政编码”;
  Response.AppendHeader(“ content-disposition”,“ attachment;
  filename = Report.zip“);

     

using(var memoryStream = new MemoryStream()){使用(var   存档=新的ZipArchive(memoryStream,ZipArchiveMode.Create,true))
  {
         for(int i = 0; i <2; i ++)
          {
              var re = rpt.ExportToStream(ExportFormatType.PortableDocFormat);
              字符串名称=“ PDF” + i +“。pdf”;
              zip.AddEntry(Name,re);
            zip.Save(memoryStream);
          }}

     

memoryStream.Seek(0,SeekOrigin.Begin);   memoryStream.WriteTo(Response.OutputStream);