Return PDF/TXT/XLS/PXLS to browser in ASP.NET MVC using crystal report

This example is using SAP crystal report for Visual studio. Can get a copy here for free.

Below sample show how to generate different file type format report to the browser dynamically.


In Controller:

public ActionResult PrintReport(string filters, string fileType)
  var page = FilterPage(new Page<StaffOrder>(), filters);

  report = ReportUtil.GetReport<StaffOrder, StaffOrderReport>(StaffOrderModel.PropertyNames, page.result.OrderBy(x => x.StaffName).ThenBy(z => z.Id).ToList());
  ReportUtil.AssignBasicInfo(report, "Staff Order Report", WebUtil.GetUserDetails().username);

  return ReportUtil.GenerateReport(report, fileType);



In ReportUtil class:

using System.Globalization;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Web.Mvc;
using Microsoft.Office.Interop.Excel;

namespace STDSInternal
 public class ReportUtil
 public static TK GetReport<T, TK>(string[] names, IList<T> items)
 where T : class
 where TK : ReportClass, new()
 var report = new TK();
 report.SetDataSource(GetDataTable<T>(names, items));
 return report;
public static System.Data.DataTable GetDataTable<T>(string[] names, IList<T> items)
 var dt = new System.Data.DataTable();
 foreach (T item in items)
 var objs = new List<object>();
 foreach (var name in names)
 PropertyInfo pi = item.GetType().GetProperty(name);
 var obj = pi.GetValue(item);
 return dt;

public static DataColumn[] GetDataColumns(string[] columnNames)
 return columnNames.Select(name => new DataColumn(name)).ToArray();

public static FileStreamResult GenerateReport<T>(T report, string fileType) where T : ReportClass
 var contentType = WebConstants.APPLICATION_PDF;
 switch (fileType)
 case "txt":
   report.ExportOptions.ExportFormatType = ExportFormatType.Text;
   contentType = WebConstants.TEXT_PLAIN;
 case "xls":
   report.ExportOptions.ExportFormatType = ExportFormatType.Excel;
   contentType = WebConstants.TEXT_XLS;
 case "pxls":
   report.ExportOptions.ExportFormatType = ExportFormatType.ExcelRecord;
   contentType = WebConstants.TEXT_XLS;
   fileType = "xls";
   report.ExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
   fileType = "pdf";
 return new FileStreamResult(report.ExportToStream(report.ExportOptions.ExportFormatType), contentType)
 FileDownloadName = report.SummaryInfo.ReportTitle + "." + fileType



In WebConstants class file:

public static readonly string APPLICATION_PDF = "application/pdf";
public static readonly string TEXT_PLAIN = "text/plain";
public static readonly string TEXT_XLS = "text/xls";




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.