我想使用SSIS導出PDF文件。要導出的報告有一個參數(type = integer)。我在互聯網上發現的代碼完全是這樣做的here。SSIS創建0 KB PDF文件
但是,當我從ssis運行此代碼時,它會導出pdf,但文件大小爲0KB。試圖打開PDF文件時出現錯誤。
下面是我使用的代碼:我試圖去URL http://Server/ReportServer/Pages/ReportViewer.aspx?%2fMapName%2fMapName%2fMapName%2Repportname&Parameter=「parametersqlresults」格式
Imports System
Imports System.Data
Imports System.Net
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime
Imports System.ComponentModel
Imports System.Diagnostics
<System.AddIn.AddIn("ScriptMain", Version:="1.0", Publisher:="", Description:="")> _
<System.CLSCompliantAttribute(False)>
Partial Public Class ScriptMain
Inherits Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
Enum ScriptResults
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
End Enum
Protected Sub SaveFile(ByVal url As String, ByVal localpath As String)
Dim loRequest As System.Net.HttpWebRequest
Dim myCredentials As New NetworkCredential("username", "password")
Dim loResponse As System.Net.HttpWebResponse
Dim loResponseStream As System.IO.Stream
Dim loFileStream As New System.IO.FileStream(localpath, System.IO.FileMode.Create, System.IO.FileAccess.Write)
Dim laBytes(256) As Byte
Dim liCount As Integer = 1
Try
loRequest = CType(System.Net.WebRequest.Create(url), System.Net.HttpWebRequest)
loRequest.Credentials = myCredentials
loRequest.Timeout = 600000
loRequest.Method = "GET"
loResponse = CType(loRequest.GetResponse, System.Net.HttpWebResponse)
loResponseStream = loResponse.GetResponseStream
Do While liCount > 0
liCount = loResponseStream.Read(laBytes, 0, 256)
loFileStream.Write(laBytes, 0, liCount)
Loop
loFileStream.Flush()
loFileStream.Close()
Catch ex As Exception
End Try
End Sub
Public Sub Main()
Dim url, destination As String
destination = Dts.Variables("varDestinationPath").Value.ToString + "\" + Dts.Variables("varRSParameter").Value.ToString + " " + Format(Now, "yyyyMMdd") + ".pdf"
url = "http://Server/ReportServer/Pages/ReportViewer.aspx?%2fMap%2fMap%2fMap%2Repportname&Parameter=" + Dts.Variables("varRSParameter").Value.ToString + "&rs:Format=PDF"
SaveFile(url, destination)
Dts.TaskResult = ScriptResults.Success
End Sub
當我這樣做,我得到一個下載PDF和一切行得通。
我認爲有可能是一些與連接到報告服務器,所以我說這代碼:
`Imports System.Net
Dim myCredentials As New NetworkCredential("username", "password")
loRequest.Credentials = myCredentials`
另外在Windows日誌報告服務器上>安全 我可以看到審覈成功用提供的用戶名和密碼。
我將位置改爲將pdf保存到多個位置。我也改變了從數據類型「字符串」變量的數據類型爲「INT32」 沒有成功
對於你說的工作,如果你手動去它並不意味着是代碼的URL格式相同的URL。這是一個錯字嗎? – DForck42
我不能看到錯字?但在我的VBScript中,當我按住CTRL時,它會要求輸入憑證,並且在填寫完參數後我可以看到該報告。 – Vico