这里是一个非常简单的例子(它可以改善 - 参见后码块注释)通过使用脚本任务动态地改变连接字符串。您还可以使用表达式和连接管理器的表达式属性动态更改连接字符串。但是,由于您正在使用脚本任务来处理下载,因此我已经使用其中一个进行了演示。
由于您没有标记您正在使用的脚本语言(VB或C#),我已经在VB中编写了一个粗略的草稿。
我已添加评论,但stackoverflow语法突出显示解释奇怪;道歉。
Public Sub Main()
' Get the HTTP Connection
Dim connObj As Object = Dts.Connections("HTTP Connection Manager").AcquireConnection(Nothing)
Dim connection As New HttpClientConnection(connObj)
' Static list of month names for dynamic connection string (obviously add as many as needed)
Dim monthNames As String() = New String() {"Januar", "February", "March"}
' Nested loop - for each year and month, try to download the Excel file
For Y As Integer = 2007 To 2016 Step 1
For M As Integer = 0 To monthNames.Length - 1 Step 1
' Set the assumed name of the remote file
Dim remoteFileName As String = monthNames(M) + "-" + Y.ToString() + ".xls"
' Change the connection string a.k.a dynamic connection string
connection.ServerURL = "http://www.ads-slo.org/media/xls/" + Y.ToString() + "/" + remoteFileName
' Set where to download the file to
Dim localFileName As String = "C:\Temp\" + remoteFileName
Try
connection.DownloadFile(localFileName, True)
Dim buffer As Byte() = connection.DownloadData()
Dim data As String = Encoding.ASCII.GetString(buffer)
Catch E As Exception
' File may not exist on remote server, delete the blank copy it attempted to create
File.Delete(localFileName)
End Try
Next
Next
Dts.TaskResult = DTSExecResult.Success
End Sub
这怎么能改进?
一个潜在的改进是解析的文件夹和目录内容的远程服务器(保存为月份名称,硬编码的开始和结束多年的建设和文件名的静态列表)使用HttpWebRequest。
但是,远程服务器权限可能会存在问题,因此您需要进行进一步调查并与服务器管理员进行交流。
测试上述代码,它成功下载了网站上的Januar-2015和Januar-2016 Excel文件。
哇谢谢你太多了。 – user3729625
但是,如果我正试图在此页面上下载xls文件,我该怎么办:https://nio.gov.si/nio/data/prvic+registrirana+vozila+v+letu+2014+po+mesecih。由于他们的URL不是可以预测的。有一种简单的方法来解压缩文件并将它们导入到sql表中吗? 再次提前谢谢您。 亲切的问候,Domen – user3729625