简体   繁体   English

生成脚本以为 SSRS 中的每条记录创建报告

[英]Generate script to create reports for each record in SSRS

In SSRS you can use parameters to export a report to a specific format.在 SSRS 中,您可以使用参数将报告导出为特定格式。 For instance I can add to a url rs:format=PDF to get the report to export to a pdf file.例如,我可以添加到 url rs:format=PDF以将报告导出到 pdf 文件。 When this exports the report comes in pdf file with the name of the report the title of the actual report.导出时,报告会出现在 pdf 文件中,报告的名称是实际报告的标题。 I realize there is no way in SSRS to actually give a specific name to the report when it is downloaded.我意识到在 SSRS 中没有办法在下载报告时实际为报告指定特定名称。

However, we have 10k records that a user wants automatically exported to say pdf export.但是,我们有 10k 条记录,用户希望自动导出到 pdf 导出。 I don't want to go in there and create 10k different data driven subscriptions and give the parameter the ID value of the record.我不想在那里 go 并创建 10k 个不同的数据驱动订阅并为参数提供记录的 ID 值。 I'd have to do that 10k times!我必须这样做 10k 次!

Is there an easier way to do this such that I can export all my records to a specific format with the name of the report the ID of the record??有没有更简单的方法可以做到这一点,以便我可以将我的所有记录导出为具有报告名称和记录 ID 的特定格式? For instance we use an auto-number id for each record so one pdf may be named 12.pdf and the next is 13.pdf, etc. But this should be run on demand and at anytime.例如,我们为每条记录使用一个自动编号 id,因此一个 pdf 可以命名为 12.pdf,下一个是 13.pdf。

Is there anyone who knows if I can do this via tsql or a sproc?有谁知道我是否可以通过 tsql 或 sproc 来做到这一点?

Right click on your SSRS report and follow this path:右键单击您的 SSRS 报告并遵循以下路径:

Manage > Subscriptions > New Subscription > Data Driven Subscription管理 > 订阅 > 新订阅 > 数据驱动订阅

Read here for more info on that.阅读此处了解更多信息。 But long story-short, you set up the parameter as you described and then fill it in via sql cell.但长话短说,您按照描述设置参数,然后通过 sql 单元格填写。

Step by step:一步步:

  1. In the "subscriptions" pane, click "new subscription"在“订阅”窗格中,单击“新订阅”
  2. In the "type of subscription" radio-buttons, be sure you click "data driven subscription" and not "standard subscription".在“订阅类型”单选按钮中,确保单击“数据驱动订阅”而不是“标准订阅”。
  3. In the "Destination" drop-down, select "windows file share".在“目标”下拉列表中,select“Windows 文件共享”。
  4. In the "dataset" section, click "edit dataset".在“数据集”部分,单击“编辑数据集”。 Establish a connection, write a sql query.建立连接,编写一个 sql 查询。 In your case, a 10000 row sql query that outputs a different "title" or similarly named column for each different report.在您的情况下,一个 10000 行 sql 查询为每个不同的报告输出不同的“标题”或类似命名的列。 You may also want to output a "fileName" field.您可能还想给 output 一个“fileName”字段。
  5. Validate as necessary and click "apply" to return to the previous screen.根据需要进行验证,然后单击“应用”以返回上一个屏幕。
  6. In the "Delivery Options" section, select "get value from dataset" in the "Source of value" section for the file name, and reference the filename column created in step 4.在“交付选项”部分中,select 在“值的来源”部分中的“从数据集中获取值”作为文件名,并引用在步骤 4 中创建的文件名列。
  7. Write in the path manually, select the render format manually, and complete any other options as necessary.手动写入路径,select 手动渲染格式,并根据需要完成任何其他选项。
  8. In the report parameters, select "get value from dataset" for the title parameter you have in the ssrs report.在报告参数中,select“从数据集中获取值”用于您在 ssrs 报告中的标题参数。 Then reference the "title" field you created in step 4.然后引用您在第 4 步中创建的“标题”字段。
  9. Fill in other cells as necessary.根据需要填写其他单元格。
  10. Click "Create Subscription".单击“创建订阅”。

Here is a visual tour, with implementation for a different report of my own:这是一个视觉导览,实现了我自己的不同报告:

数据驱动的子初始

数据驱动的子查询

数据驱动的亚决赛

So you mention that you don't have enterprise edition, and it seems that only enterprise edition has data-driven subscriptions.所以你提到你没有企业版,似乎只有企业版有数据驱动订阅。

There is another way.还有另一种方法。 You can execute SSRS via WebClient in C#.您可以通过 C# 中的 WebClient 执行 SSRS。

First, capture the parameters dynamically via SQL:首先,通过SQL动态捕获参数:

class parameterRow {
    public string fileName;
    public string active;
}

... 

var parameterRows = new List<parameterRow>();

var sql = @"

    select      fileName = 'activeRecords.pdf', 
                active = 'Active' 
    
    union all
    select      fileName = 'inactiveRecords.pdf', 
                active = 'Inactive'
    
";

using (var con = new SqlConnection("server=someServer;database=someDB;trusted_connection=yes"))
using (var cmd = new SqlCommand(sql, con)) {
    
    con.Open();
    
    using (var rdr = cmd.ExecuteReader()) 
        while (rdr.Read()) 
            parameterRows.Add(new parameterRow {
                fileName = rdr[0].ToString(),
                active = rdr[1].ToString()
            });
            
}
    

Then loop the dynamically created parameters, downloading and zipping each iteration by integrating the parameter name into the url and the file name into the zip entry name:然后循环动态创建的参数,通过将参数名称集成到 url 并将文件名集成到 zip 条目名称中,循环下载和压缩每个迭代:

using (var client = new WebClient()) 
using (var fileStream = new FileStream(@"c:\...\ssrs.zip", FileMode.Create, FileAccess.Write)) 
using (var zipArchive = new ZipArchive(fileStream, ZipArchiveMode.Create, false)) {

    foreach(var paramRow in parameterRows) {
    
        var url = Uri.EscapeUriString(
            @"http://ssrs.YourSite.org/ReportServer/Pages/ReportViewer.aspx?" + 
            @"/Basic Student Lists/Student List" + 
            $@"&Active={paramRow.active}" + 
            @"&SortOrder=Student Name" + 
            @"&rs:Command=Render&rs:Format=pdf"
        ); 

        client.UseDefaultCredentials = true;            
        var data = client.DownloadData(url);
        var zipEntry = zipArchive.CreateEntry(paramRow.fileName);

        using (var entryMs = new MemoryStream(data))
        using (var zipEntryStream = zipEntry.Open()) 
            entryMs.CopyTo(zipEntryStream);
        
    }
    
}       
        

Obviously you may not have to zip, but since it's part of the old code I fetched, I figure I'd keep that feature in case it's useful.显然,您可能不必 zip,但由于它是我获取的旧代码的一部分,我想我会保留该功能以防万一它有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM