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.Load();
 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();
 dt.Columns.AddRange(GetDataColumns(names));
 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);
 objs.Add(DoConversion(obj));
 }
 dt.Rows.Add(objs.ToArray());
 }
 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;
   break;
 case "xls":
   report.ExportOptions.ExportFormatType = ExportFormatType.Excel;
   contentType = WebConstants.TEXT_XLS;
   break;
 case "pxls":
   report.ExportOptions.ExportFormatType = ExportFormatType.ExcelRecord;
   contentType = WebConstants.TEXT_XLS;
   fileType = "xls";
   break;
 default:
   report.ExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
   fileType = "pdf";
   break;
 }
 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";

 

 

 

 

 

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s