如何在Excel中循环查看报表参数列表,并使用VBA自动生成.PDF格式的报表服务器报表?
我有一个名为“客户声明”的SSRS报告,以及一个客户ID和客户购买的相关产品的大列表。例如:如何在Excel中循环查看报表参数列表,并使用VBA自动生成.PDF格式的报表服务器报表?,excel,vba,pdf,reporting-services,ssrs-2012,Excel,Vba,Pdf,Reporting Services,Ssrs 2012,我有一个名为“客户声明”的SSRS报告,以及一个客户ID和客户购买的相关产品的大列表。例如: C1222,宽带 C1223,宽带 C1224,移动电话 C1225,宽带 C1226,移动电话 。。。(约10000条记录) 该报表生成一个客户对账单,并具有两个参数,一个用于报表类型,一个用于客户ID 我需要为每个客户保存导出报告的单个.PDF文件,并将其放入名为客户ID的文件夹中。例如“C1222.PDF” 我已经解决了一半的问题,我可以使用如下URL直接从报表服务器下载.PDF文件: 在w
- C1222,宽带
- C1223,宽带
- C1224,移动电话
- C1225,宽带
- C1226,移动电话
我有SSRS标准版,所以“数据驱动订阅”对我来说不是一个选项。经过大量的谷歌搜索和尝试,我有了一个可行的解决方案 我将我的列表放在Excel中名为“Control”的工作表的A列和B列中,并在C列中添加了文件夹名称,以便每个产品类型的语句都转到不同的文件夹中。然后在工作表上运行此VBA:
Declare PtrSafe Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" _
(ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, _
ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
Sub DownloadStatements()
Dim wb As Workbook
Set wb = ThisWorkbook
Dim Product As String
Dim CustomerID As String
'Loop down all records in Control sheet
For x = 2 To wb.Sheets("Control").Range("A1").CurrentRegion.Rows.Count
'Set CustomerID to the relevant value
CustomerID = wb.Sheets("Control").Range("A" & x).Value
'Set ClientString to the relevant value
Product = wb.Sheets("Control").Range("B" & x).Value
'Set download filename to CustomerID + ".pdf"
DownloadFile$ = CustomerID & ".pdf"
'Set download URL as the report URL, passing in the Product and CustomerID as paramaters
Url$ = "http://myreportserver/ReportServer/Pages/ReportViewer.aspx?%2fCustomer+Reports%2fCustomer+Statement&rs:Format=PDF&rs:ClearSession=true&CustomerID=" & CustomerID & "&Product=" & Product
'Set local download location based on main output folder cell and output subfolder cell for the specific row
LocalFilename$ = wb.Sheets("Control").Range("E2").Value & wb.Sheets("Control").Range("C" & x) & DownloadFile
'Download the file (in a slightly cheaty way)
On Error Resume Next
Debug.Print "Some junk text " & URLDownloadToFile(0, Url, LocalFilename, 0, 0) = 0
On Error GoTo 0
Next x
End Sub
所有的工作都很完美,Excel将坐在那里,在列表中创建.PDF文件,它非常快,并且很容易进一步自动化