[英]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:一步步:
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.