2009-10-30 77 views
4

我有一份報告,需要多次運行並保存爲PDF。我目前以編程方式生成PDF格式的報告,但希望保存報告,而用戶​​不必每次都手動選擇保存選項。以編程方式將RDLC報告另存爲PDF

我用它來呈現一個報表爲PDF的代碼是:

Dim warnings As Microsoft.Reporting.WebForms.Warning() = Nothing 

    Dim streamids As String() = Nothing 

    Dim mimeType As String = Nothing 

    Dim encoding As String = Nothing 

    Dim extension As String = Nothing 

    Dim deviceInfo As String 

    Dim bytes As Byte() 

    Dim lr As New Microsoft.Reporting.WebForms.LocalReport 

    deviceInfo = "<DeviceInfo><SimplePageHeaders>True</SimplePageHeaders></DeviceInfo>" 

    bytes = ReportViewer1.LocalReport.Render("PDF", deviceInfo, mimeType, encoding, extension, streamids, warnings) 

    Response.ClearContent() 

    Response.ClearHeaders() 

    Response.ContentType = "application/pdf" 

    Response.BinaryWrite(bytes) 

    Response.Flush() 

    Response.Close() 

我盤算,我可以在一個循環中運行它,並在每次保存PDF。

+0

做你試着用** **的FileStream並獲得解決方案? – Kiquenet 2015-10-15 13:49:10

回答

6

你在這裏有什麼問題?它不起作用嗎?

這裏是我們2005年做的一個例子。我們定義了一個名爲rptViewer1的控件,根據您的需要,它可以是可見的或不可見的。 strFormat應該包含「PDF」和strNomFicher的完整路徑。

BTW變量名稱和功能都在法國,但無論如何都會正常工作:)

 

    Public Sub CreerFichierRapport(ByVal strNomFichier As String, ByVal strFormat As String) 
     Dim bytes() As Byte 
     Dim strDeviceInfo As String = "" 
     Dim strMimeType As String = "" 
     Dim strEncoding As String = "" 
     Dim strExtension As String = "" 
     Dim strStreams() As String 
     Dim warnings() As Warning 
     Dim oFileStream As FileStream 
     _stream = New List(Of Stream) 
     Try 
      bytes = rptViewer1.LocalReport.Render(strFormat, strDeviceInfo, strMimeType, strEncoding, strExtension, strStreams, warnings) 

      oFileStream = New FileStream(strNomFichier, FileMode.Create) 
      oFileStream.Write(bytes, 0, bytes.Length) 
      _stream.Add(oFileStream) 
     Finally 
      If Not IsNothing(oFileStream) Then 
       oFileStream.Close() 
       oFileStream.Dispose() 
      End If 
     End Try 
    End Sub 

+0

我需要發送到客戶端'Response'報告像***應用程序/ pdf和應用程序/ vnd.ms-excel ***使用***字節數組***與'HttpContext.Current.Response.BinaryWrite(renderedBytes )'。是否可能或_我需要先將文件保存到磁盤,然後發送到客戶端?_ – Kiquenet 2015-10-15 13:54:32

+0

什麼是*** _ stream ***變量?靜態在課堂上? – Kiquenet 2015-10-15 13:59:16

5

大衛的回答是對我很有幫助。我以爲我會發表我的簡化,(略)翻譯這段代碼的版本,因爲原來的包含了一些法國人,而不是相關的也有一些參考:

Imports Microsoft.Reporting.WebForms 
Imports System.IO 

Public Class RenderToPDF 
    Public Sub Save(ByVal viewer As ReportViewer, ByVal savePath As String) 
     Dim Bytes() As Byte = viewer.LocalReport.Render("PDF", "", Nothing, Nothing, Nothing, Nothing, Nothing) 

     Using Stream As New FileStream(savePath, FileMode.Create) 
      Stream.Write(Bytes, 0, Bytes.Length) 
     End Using 
    End Sub 
End Class 
+0

我使用***字節數組***與'HttpContext.Current.Response.BinaryWrite(renderedBytes)',但有時我得到**異常** _OutOfMemory_也許首先將文件保存到ASP.NET服務器中的文件系統? – Kiquenet 2015-10-15 13:58:31