Skip to content

Instantly share code, notes, and snippets.

@moppa
Created April 29, 2013 10:23
Show Gist options
  • Save moppa/5480818 to your computer and use it in GitHub Desktop.
Save moppa/5480818 to your computer and use it in GitHub Desktop.
Hur man anropar Reporting services från MVC.
Kräver referenser till:
Microsoft.ReportViewer.WebForms
Microsoft.ReportViewer.Common
För SQL Server 2008 R2 kör man version 10.0.0.0
================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.Reporting.WebForms;
namespace ReportingTest.Controllers
{
public class ReportingController : Controller
{
//
// GET: /Reporting/
public ActionResult Index()
{
//Sätt upp objektet som en server-report
ReportViewer rv = new ReportViewer();
rv.ServerReport.ReportServerUrl = new Uri("http://<dbserver>/reportserver");
rv.ServerReport.ReportPath = "/Folder/Report"; //Utan .rdl
//Lägg till eventuella raportparametrar
ReportParameterCollection paramCol = new ReportParameterCollection();
paramCol.Add(new ReportParameter("key","value"));
//paramCol.Add(new ReportParameter("ComplianceOfficerSSN", null));
rv.ServerReport.SetParameters(paramCol);
//De två viktiga parametrarna
string reportType = "PDF"; //"PDF", "HTML 4.0" eller "Excel".
string mimeType; //Blir "application/pdf" för PDF. För Excel kan det bli olika beroende på RS-version.
//För att få ut vilka renderingsformat som stöds.
//List<RenderingExtension> renderingExtensions = rv.ServerReport.ListRenderingExtensions().ToList();
//Övrig metadata för anropet
string encoding;
string fileNameExtension;
Warning[] warnings;
string[] streams;
byte[] renderedBytes;
//Device-info. Skickar med specifik device info. Jag väljer köra raportserverns default.
//The DeviceInfo settings should be changed based on the reportType
//http://msdn2.microsoft.com/en-us/library/ms155397.aspx
string deviceInfo =
"<DeviceInfo>" +
" <OutputFormat>PDF</OutputFormat>" +
" <PageWidth>8.5in</PageWidth>" +
" <PageHeight>11in</PageHeight>" +
" <MarginTop>0.5in</MarginTop>" +
" <MarginLeft>1in</MarginLeft>" +
" <MarginRight>1in</MarginRight>" +
" <MarginBottom>0.5in</MarginBottom>" +
"</DeviceInfo>";
renderedBytes = rv.ServerReport.Render(reportType, null /*deviceInfo*/, out mimeType, out encoding, out fileNameExtension, out streams, out warnings);
return File(renderedBytes, mimeType);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment