在循环中将Crystal Report导出为PDF仅适用于第一个 [英] Export Crystal Report to PDF in a Loop only works with first
问题描述
我正在尝试生成报告并将其循环导出为pdf,该报告将在每个循环中接收一个新参数,并提示客户端下载PDF,换句话说,客户端可能需要下载2或同时有3个(或更多)PDF,问题是仅对第一个PDF出现接受下载的提示,不知道为什么.我可以毫无问题地导出到磁盘(服务器端).
i'm trying to generate a report and export it to pdf in a loop, the report will receive a new parameter in each loop and prompt the client to download a PDF, in other words, the client may need to download 2 or 3 (or more) PDFs at the same time, the problem is that the prompt to accept the download only appears for the first pdf, dont know why. I can export to disk (server side) without any problems.
代码:
Sub PrintReport(ByVal Cod As Integer)
Dim CTableLogInfo As TableLogOnInfo
Dim ConnInfo As CrystalDecisions.Shared.ConnectionInfo = New ConnectionInfo()
ConnInfo.Type = ConnectionInfoType.SQL
ConnInfo.ServerName = ConfigurationManager.AppSettings("SQLSERVERNAME")
ConnInfo.DatabaseName = ConfigurationManager.AppSettings("SQLDBNAME")
ConnInfo.UserID = ConfigurationManager.AppSettings("SQLSERVERUSER")
ConnInfo.Password = ConfigurationManager.AppSettings("SQLSERVERPASSWORD")
ConnInfo.AllowCustomConnection = False
ConnInfo.IntegratedSecurity = False
For Each CTable As Table In CrystalReportSource1.ReportDocument.Database.Tables
CTable.LogOnInfo.ConnectionInfo = ConnInfo
CTableLogInfo = CTable.LogOnInfo
CTableLogInfo.ReportName = CrystalReportSource1.ReportDocument.Name
CTableLogInfo.TableName = CTable.Name
CTable.ApplyLogOnInfo(CTableLogInfo)
Next
Dim pField As ParameterField = CrystalReportSource1.ReportDocument.ParameterFields(0)
Dim val1 As ParameterDiscreteValue = New ParameterDiscreteValue
val1.Value = Cod
pField.CurrentValues.Clear()
pField.CurrentValues.Add(val1)
Dim PDFName As String = "PDF Nº " & Cod
CrystalReportSource1.ReportDocument.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Page.Response, True, PDFName)
End Sub
尝试使用DotNetZip压缩报告,但我的zip损坏了.
Tried to zip the reports with DotNetZip but i get an broken zip.
你能告诉我怎么了吗? (已解决:现在已更正下面的代码)
Can you tell me whats wrong? (Solved: code bellow is corrected now)
Response.ClearContent()
Response.ClearHeaders()
Response.ContentType = "application/zip"
Response.AppendHeader("content-disposition", "attachment; filename=AllPDFs.zip")
Using zipFile As New ZipFile()
For i = 0 To Cod.Length - 1
If Cod(i) > 0 Then
val1.Value = Cod(i)
pField.CurrentValues.Clear()
pField.CurrentValues.Add(val1)
val2.Value = Cod(i)
pField2.CurrentValues.Clear()
pField2.CurrentValues.Add(val2)
Dim PDFNameAs String = "PDF Nº " & Cod(i) & ".pdf"
Dim s As New System.IO.MemoryStream
s =CrystalReportSource1.ReportDocument.ExportToStream(ExportFormatType.PortableDocFormat)
zipFile.AddEntry(PDFName, s)
End If
Next
zipFile.Save(Response.OutputStream)
End Using
Response.Clear()
推荐答案
可能响应在第一个响应之后结束,因此对于第二次和第三次尝试都没有响应可写.
Probably the response ends after the first one, therefore there's no response to write to for the 2nd and 3rd attempts.
相反,您可以让客户端通过AJAX请求下载报告(将报告生成移至.ashx
通用处理程序中),或让用户单击按钮3次以发起新请求.
Instead, you can have the client download the reports via AJAX Request (move your report generation into an .ashx
generic handler), or have the user click the button 3 times to initiate new requests.
或将PDF压缩至单个文件,然后允许客户端下载.
Or zip the PDF's up until a single file and allow the client to download that.
这篇关于在循环中将Crystal Report导出为PDF仅适用于第一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!