ユーザーがチケット番号を入力する必要があるレポートがあり、表示をクリックするとレポートが生成されます。かなり標準的。私がしたいのは、ビューをクリックすると、レポートが自動的に PDF にエクスポートされるようにすることです。これが可能かどうか、そして私がどのように問題にアプローチするかを誰かが知っていますか?
ご意見ありがとうございます。
ユーザーがチケット番号を入力する必要があるレポートがあり、表示をクリックするとレポートが生成されます。かなり標準的。私がしたいのは、ビューをクリックすると、レポートが自動的に PDF にエクスポートされるようにすることです。これが可能かどうか、そして私がどのように問題にアプローチするかを誰かが知っていますか?
ご意見ありがとうございます。
私はこの ASP.NET を実行しましたが、VB.NET は実行しませんでした。これには、レポートのレンダリング タイプ (この場合は PDF) を指定できるURL アクセスを使用して、舞台裏でレポート サーバーに要求を行うことが含まれていました。 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")