要下载SSRS报表为Excel或PDF,而不是在报告管理器中呈现,可以使用以下代码:
public ActionResult DownloadReport(string format) { string mimeType; string encoding; string fileNameExtension; string[] streams; Warning[] warnings;
byte[] pdfContent = reportViewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out fileNameExtension, out streams, out warnings);
if (format.ToUpper() == "PDF")
{
return File(pdfContent, "application/pdf", "Report.pdf");
}
else if (format.ToUpper() == "EXCEL")
{
byte[] excelContent = reportViewer.LocalReport.Render("EXCEL", null, out mimeType, out encoding, out fileNameExtension, out streams, out warnings);
return File(excelContent, "application/vnd.ms-excel", "Report.xls");
}
else
{
return null;
}
}
在上面的代码中,我们可以设置报告格式的字符串作为参数format。如果该字符串为PDF,则报表将被呈现为PDF,并使用File方法返回PDF文件。如果它是Excel,则使用相同的方法将报表呈现为Excel文件。在所有情况下,我们使用Render方法来呈现报表并获得其字节数组,并将其提供给File方法以进行下载。