After tinkering with the CodeProject demo that "Bradly Uffner" gave me i managed to get the code working the way i wanted it to, you can download the demo project Here. So here is what i came up with:
View:
@Html.ActionLink("Download Report in Excel Format", "ExportReport", new { ContentType = "application/vnd.ms-excel", FileType = "Excel" })
@Html.ActionLink("Download Report in PDF Format", "ExportReport", new { ContentType = "application/pdf", FileType = "pdf" })
Controller:
public ActionResult ExportReport(string FileType, string ContentType)
{
LocalReport localReport = new LocalReport();
localReport.ReportPath = Server.MapPath("~/Content/Report1.rdlc");
IList<WorldModel> customerList = new List<WorldModel>();
// SOME DEMO DATA!
customerList.Add(new WorldModel("Europe", "Sweden", "2001", "1823"));
customerList.Add(new WorldModel("Europe", "Sweden", "2002", "1234"));
customerList.Add(new WorldModel("Europe", "Sweden", "2003", "9087"));
customerList.Add(new WorldModel("Europe", "Denmark", "2001", "6793"));
customerList.Add(new WorldModel("Europe", "Denmark", "2002", "4563"));
customerList.Add(new WorldModel("Europe", "Denmark", "2003", "1897"));
customerList.Add(new WorldModel("Europe", "Norway", "2001", "5632"));
customerList.Add(new WorldModel("Europe", "Norway", "2002", "9870"));
customerList.Add(new WorldModel("Europe", "Norway", "2003", "2367"));
customerList.Add(new WorldModel("Asia", "India", "2001", "1980"));
customerList.Add(new WorldModel("Asia", "India", "2002", "9765"));
customerList.Add(new WorldModel("Asia", "India", "2003", "6789"));
//DEMO DATA END
ReportDataSource reportDataSource = new ReportDataSource();
reportDataSource.Name = "DataSet1";
//********** IF YOU NEED TO FILTER THE DATA ******************
//var customerfilterList = from c in customerList
// where c.Territory == territory
// select c;
//reportDataSource.Value = customerfilterList;
//************************************************************
reportDataSource.Value = customerList;
localReport.DataSources.Add(reportDataSource);
string reportType = FileType;
string mimeType;
string encoding;
string fileNameExtension;
//The DeviceInfo settings should be changed based on the reportType
//http://msdn2.microsoft.com/en-us/library/ms155397.aspx
string deviceInfo = "<DeviceInfo>" +
" <OutputFormat>" + FileType + "</OutputFormat>" +
" <PageWidth>8.5in</PageWidth>" +
" <PageHeight>11in</PageHeight>" +
" <MarginTop>0.5in</MarginTop>" +
" <MarginLeft>1in</MarginLeft>" +
" <MarginRight>1in</MarginRight>" +
" <MarginBottom>0.5in</MarginBottom>" +
"</DeviceInfo>";
Warning[] warnings;
string[] streams;
byte[] renderedBytes = localReport.Render(reportType, deviceInfo, out mimeType, out encoding, out fileNameExtension, out streams, out warnings);
return File(renderedBytes, ContentType, string.Format("NameOfFile.{0}",fileNameExtension));
}
I'm not sure if this will work if the reports are in a seperate project but i will try that also.
Resources:
Content Types Here