Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Excel中循环查看报表参数列表,并使用VBA自动生成.PDF格式的报表服务器报表?_Excel_Vba_Pdf_Reporting Services_Ssrs 2012 - Fatal编程技术网

如何在Excel中循环查看报表参数列表,并使用VBA自动生成.PDF格式的报表服务器报表?

如何在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

我有一个名为“客户声明”的SSRS报告,以及一个客户ID和客户购买的相关产品的大列表。例如:

  • C1222,宽带
  • C1223,宽带
  • C1224,移动电话
  • C1225,宽带
  • C1226,移动电话
。。。(约10000条记录)

该报表生成一个客户对账单,并具有两个参数,一个用于报表类型,一个用于客户ID

我需要为每个客户保存导出报告的单个.PDF文件,并将其放入名为客户ID的文件夹中。例如“C1222.PDF”

我已经解决了一半的问题,我可以使用如下URL直接从报表服务器下载.PDF文件:

在web浏览器中打开该链接会自动将报告下载为名为“Customer Statement.PDF”的.PDF文件,并传入正确的参数

我需要的是遍历整个列表,为每一行生成一个文件,并将相关参数传递到URL中。.PDF的文件名必须是列表中的客户ID

理想情况下,我希望使用VBA宏在Excel中执行此操作,但我愿意使用Powershell、SQL Server甚至Windows批处理文件,如果这样做更简单的话

编辑:


我有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文件,它非常快,并且很容易进一步自动化