1

我有一个报告,要求用户输入他们的票号,然后他们点击查看,它会生成。很标准。我想做的是让报告在他们点击查看时自动导出为 pdf。有谁知道这是否可能以及我将如何处理此事?

感谢您的任何意见。

4

1 回答 1

1

我已经完成了这个 ASP.NET 但不是 VB.NET。它所涉及的是使用URL Access在后台向报表服务器发出请求,您可以在其中指定报表的呈现类型(在本例中为 PDF),并且我的代码隐藏将文件下载并保存到 Web 服务器,然后服务器它以 PDF 文件的形式备份给用户。用户所要做的就是填写他们的参数并点击提交按钮。

我想您可以执行与我一直遵循的几乎相同的步骤,直到您需要提供文件,在表单环境中,您不需要向他们提供下载提示,因为它已经在机器上并且所有你要做的就是打开文件(它应该在你的默认 PDF 查看器中加载)。

更新:这是一些代码,其中大部分是基于 CodeProject 教程,但我不记得如何获得它。做了一些小的修改:

Public Sub ServeReport(ByVal URL As String, _
    ByVal Directory As String, ByVal Filename As String)

    Dim f As New FileIOPermission(PermissionState.None)
    Dim fs As FileStream

    If Not System.IO.Directory.Exists(Directory) Then
        System.IO.Directory.CreateDirectory(Directory)
    End If

    DownloadWebFile(URL, Directory & Filename)    

    fs = File.Open(Directory & Filename, FileMode.Open)

    Dim bytBytes(fs.Length) As Byte
    fs.Read(bytBytes, 0, fs.Length)
    fs.Close()

    Response.AddHeader("Content-disposition", _
               "attachment; filename=" & Filename)
    Response.ContentType = "application/octet-stream"
    Response.BinaryWrite(bytBytes)

    f.AllLocalFiles = FileIOPermissionAccess.AllAccess
    File.Delete(Directory & Filename)
End Sub

下面是 DownloadWebFile 子程序,它将实际发出报表服务器请求并下载文件并将其保存到本地磁盘。

Public Shared Sub DownloadWebFile(ByVal URL As String, _
    ByVal DestFilename As String)

    'Create a web request to attach to the remote file 
    Dim WebFile As System.Net.WebRequest

    'Create a file stream for writing the file to the local filename 
    Dim LocalFile As System.IO.FileStream

    'Create some working variables for the copy process 
    Dim Buffer(16384) As Byte
    Dim BytesRead As Long

    'Open a WebRequest stream to the source file 
    WebFile = System.Net.WebRequest.Create(URL)

    'Credentials are required, pass the defaults 
    WebFile.Credentials = System.Net.CredentialCache.DefaultCredentials

    'Create the local file 
    LocalFile = New IO.FileStream(DestFilename, IO.FileMode.Create)

    'Download the file in 16k chunks 
    With WebFile.GetResponse.GetResponseStream
        Do
            BytesRead = .Read(Buffer, 0, 16384)
            LocalFile.Write(Buffer, 0, BytesRead)
        Loop Until BytesRead = 0
        .Close()
    End With

    WebFile = Nothing

    'Force all data out of memory and into the file before closing 
    LocalFile.Flush()
    LocalFile.Close()
    LocalFile = Nothing
End Sub

最后,这里是一个用法示例:

Dim URL As String
URL = reporturl & "&rs%3aCommand=Render&rs%3AFormat=PDF"

ServeReport(URL, "C:\Temp\", "MyReport.PDF")
于 2009-06-10T13:12:10.550 回答