从 C# 代码导出参数化 SSRS 报告

发布于 2024-10-07 17:46:09 字数 319 浏览 4 评论 0原文

目前我已经设置了 SQL Reporting Services 2005,报表管理器位于用户可以访问报表的 URL 上。这些报告在那里运作良好。

我的问题是尝试在 C# .net 4.0 代码中生成这些报告,无需任何用户交互(例如使用屏幕上的报告查看器)。我想在 C# .net 应用程序中生成报告并将其导出为 PDF 文件。报告具有必需的参数,因此我需要将参数传递给报告。我该怎么做?

我一直在网上搜索,要么我使用了错误的关键字,要么没有太多关于这方面的信息。我很惊讶找到这方面的信息是多么困难,因为我认为这是一个相当常见的问题。感谢任何和所有建议/帮助。

Currently I have SQL Reporting Services 2005 set up, with the report manager at a URL on which users can access reports. The reports are working great there.

My issue is trying to generate these reports in C# .net 4.0 code without any user interaction (such as using the report viewer on screen). I would like to generate and export a report to a PDF file in a C# .net application. The reports have required parameters so I would need to pass the parameters to the report. How can I do this?

I have been searching around online, and either I'm using the wrong keywords or there isn't much information on this. I am quite amazed at how difficult it has been to find information on this, as I would expect it to be a fairly common question. Any and all advice / help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

水水月牙 2024-10-14 17:46:09

我很少使用 2005 版的 ReportViewer。但您应该能够执行以下操作:

ServerReport serverReport = new ServerReport();
serverReport.ReportPath = "path/to/report";
serverReport.ReportServerCredentials = ...;
serverReport.ReportServerUrl = "http://....";
serverReport.SetParameters(...);
string mimeType;
string encoding;
string extension;
string[] streams;
Warning[] warnings;
byte[] asPdf = serverReport.Render("PDF", string.Empty, out mimeType, out encoding, out extension, out streams, out warnings);

一般要点是 ServerReportLocalReport 都设计为可在 ReportViewer 之外使用。

I've not used the 2005 version of the ReportViewer much. But you should be able to do something like this:

ServerReport serverReport = new ServerReport();
serverReport.ReportPath = "path/to/report";
serverReport.ReportServerCredentials = ...;
serverReport.ReportServerUrl = "http://....";
serverReport.SetParameters(...);
string mimeType;
string encoding;
string extension;
string[] streams;
Warning[] warnings;
byte[] asPdf = serverReport.Render("PDF", string.Empty, out mimeType, out encoding, out extension, out streams, out warnings);

The general takeaway being that ServerReport and LocalReport were both designed to be usable outside of a ReportViewer.

山有枢 2024-10-14 17:46:09
string outputPath = "C:\Temp\PdfReport.pdf";

ReportViewer reportViewer = new ReportViewer();
reportViewer.ServerReport serverReport = new ServerReport();
reportViewer.ServerReport.ReportPath = @"path/to/report";
reportViewer.ServerReport.ReportServerUrl = new Uri(@"http://...");
reportViewer.ProcessingMode = ProcessingMode.Local;

reportViewer.ServerReport.ReportServerCredentials.NetworkCredentials = new 
    System.Net.NetworkCredential(username, password, domain)

List<ReportParameter> parameters = new List<ReportParameter>();
parameters.Add(new ReportParameter("parameterName", "value"));

string mimeType;
string encoding;
string extension;
string[] streams;
Warning[] warnings;
byte[] pdfBytes= serverReport.Render("PDF", string.Empty, out mimeType, 
    out encoding, out extension, out streams, out warnings);

// save the file
using (FileStream fs = new FileStream(outputPath, FileMode.Create))
{
    fs.Write(pdfBytes, 0, pdfBytes.Length);
    fs.Close();
}
string outputPath = "C:\Temp\PdfReport.pdf";

ReportViewer reportViewer = new ReportViewer();
reportViewer.ServerReport serverReport = new ServerReport();
reportViewer.ServerReport.ReportPath = @"path/to/report";
reportViewer.ServerReport.ReportServerUrl = new Uri(@"http://...");
reportViewer.ProcessingMode = ProcessingMode.Local;

reportViewer.ServerReport.ReportServerCredentials.NetworkCredentials = new 
    System.Net.NetworkCredential(username, password, domain)

List<ReportParameter> parameters = new List<ReportParameter>();
parameters.Add(new ReportParameter("parameterName", "value"));

string mimeType;
string encoding;
string extension;
string[] streams;
Warning[] warnings;
byte[] pdfBytes= serverReport.Render("PDF", string.Empty, out mimeType, 
    out encoding, out extension, out streams, out warnings);

// save the file
using (FileStream fs = new FileStream(outputPath, FileMode.Create))
{
    fs.Write(pdfBytes, 0, pdfBytes.Length);
    fs.Close();
}
恋你朝朝暮暮 2024-10-14 17:46:09

我遇到了类似的问题,我想以 PDF 格式打开报告。如果您只需要在浏览器窗口中打开带有参数的 pdf,则可以使用报表服务器本身并指定 Format=PDF 作为查询字符串选项。

例子:

http://myServer/ReportServer/Pages/ReportViewer.aspx?%2fMyApplicationReports%2fAcutalReportFileName&rs:Command=Render&rs:Format=PDF&ParamOneId=31943&ParamTwoDate=17072015

我希望这可以拯救其他人过段时间吧!

I had a similar issue where I wanted to open the report as a PDF. If you just need to open a pdf with parameters in a browser window then you can use the report server itself and specify Format=PDF as a querystring option.

Example:

http://myServer/ReportServer/Pages/ReportViewer.aspx?%2fMyApplicationReports%2fAcutalReportFileName&rs:Command=Render&rs:Format=PDF&ParamOneId=31943&ParamTwoDate=17072015

I hope this saves someone else out there some time!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文