In SSRS you can use parameters to export a report to a specific format. For instance I can add to a url rs:format=PDF
to get the report to export to a pdf file. When this exports the report comes in pdf file with the name of the report the title of the actual report. I realize there is no way in SSRS to actually give a specific name to the report when it is downloaded.
However, we have 10k records that a user wants automatically exported to say pdf export. 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. I'd have to do that 10k times!
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?? 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.
Is there anyone who knows if I can do this via tsql or a sproc?
Right click on your SSRS report and follow this path:
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.
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#.
First, capture the parameters dynamically via 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:
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.