In this article I am going to explain , how to convert crystal reports to different file format using crystal reports service , The export file format that supported by our Crystal Reports as follows :
1) PDF
2) MS Word
3) MS Excel
4) HTML
5) RTF
please do the following steps :
Creating a class file ExportReports.cs
Please place the following code in that file
public ExportReports()
{
//
// TODO: Add constructor logic here
//
}
public enum ReportFormats : int
{
None = 0,
PDF = 1,
MS_Word = 2,
MS_Excel = 3,
HTML = 4,
RTF = 5
}
public byte[] ExportReportsTo(DataList<T> dt, string fileName,
ReportFormats format)
{
CrystalDecisions.CrystalReports.Engine.ReportDocument doc = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
//DataSet ds = (DataSet)context.Cache.Get(“ExportData”);
doc.Load(fileName);
doc.SetDataSource(dt);
CrystalDecisions.Shared.ExportOptions exportOpts = doc.ExportOptions;
if (format == ReportFormats.PDF)
{
exportOpts.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat;
}
else if (format == ReportFormats.MS_Word)
{
exportOpts.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.WordForWindows;
}
else if (format == ReportFormats.MS_Excel)
{
exportOpts.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.Excel;
}
else if (format == ReportFormats.HTML)
{
exportOpts.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.HTML40;
}
else if (format == ReportFormats.RTF)
{
exportOpts.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.RichText;
}
exportOpts.ExportDestinationType = CrystalDecisions.Shared.
ExportDestinationType.NoDestination;
exportOpts.DestinationOptions = new CrystalDecisions.Shared.
DiskFileDestinationOptions();
// Response.Write(doc.ExportToStream(exportOpts.ExportFormatType));
byte[] _stream = new byte[1024];
MemoryStream _ms = new MemoryStream();
_ms = (MemoryStream)doc.ExportToStream(exportOpts.ExportFormatType);
return _ms.ToArray();
}
}
the parameter dt support following format :
1)Data set
2) Data Table
3)Data Reader
4) System.Collection.IEnumerable (in this example we used)
you can change the parameter according to your wish
After creating class file we can you this service in your web page (aspx)
before you should create rpt file (eg: NewSubscriptionReports.rpt)
Use IDE to design crystal Report Templates :
Save this rpt file to a web folder then use the following code in the page
protected void btnExport_Click(object sender, EventArgs e)
{
yournamespace.ExportReports.ReportFormats format;
format = ExportReports.ReportFormats.None;
string strExt = “”;
if (ddlFormat.SelectedValue.Equals(“1″))
{
format = ExportReports.ReportFormats.PDF;
strExt = “Pdf”;
}
else if (ddlFormat.SelectedValue.Equals(“2″))
{
format = ExportReports.ReportFormats.MS_Word;
strExt = “Doc”;
}
else if (ddlFormat.SelectedValue.Equals(“3″))
{
strExt = “xls”;
format = ExportReports.ReportFormats.MS_Excel;
}
else if (ddlFormat.SelectedValue.Equals(“4″))
{
format = ExportReports.ReportFormats.HTML;
strExt = “HTML”;
}
else if (ddlFormat.SelectedValue.Equals(“5″))
{
strExt = “RTF”;
format = ExportReports.ReportFormats.RTF;
}
ExportReports exportReports = new ExportReports();
// System.IO.MemoryStream _ms = new System.IO.MemoryStream();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ContentType = “application/” + strExt;
HttpContext.Current.Response.AddHeader(“Content-Disposition”, “inline; filename=NewSubscriptionReports.” + strExt);
HttpContext.Current.Response.BinaryWrite(exportReports.ExportReportsTo
(SchoolService.GetSchoolsSubscriptionReports
(SchoolService.ReportType.NewSubscriptionReport, startDate, endDate, allReopts, 0, 0), Server.MapPath(“~/Maintenance/SubscriptionReports
/ReportTemplates/NewSubscriptionReports.rpt”), format));
HttpContext.Current.Response.End();
}
please specify the .rpt file path in calling function:
Server.MapPath(“~/Maintenance/SubscriptionReports/ReportTemplates/NewSubscriptionReports.rpt”),



June 9, 2008 at 1:29 pm |
please clear the me the things which code to write in which page…also i m getting error
” CPMS_Bussiness.ExportReports.ReportFormats ddlFormat; “…where CPMS_Business in my namespace name
June 9, 2008 at 2:18 pm |
try this code in your page file with your name space
protected void btnExport_Click(object sender, EventArgs e)
{
DateTime startDate = new DateTime();
DateTime endDate = new DateTime();
if ((txtEndDate.Value == “”) & (txtStartDate.Value == “”))
{
startDate = DateTime.Now.Date;
endDate = DateTime.Now.Date;
}
else if ((txtEndDate.Value != “”) & (txtStartDate.Value != “”))
{
startDate = Convert.ToDateTime(((txtStartDate.Value.ToString())));
endDate = Convert.ToDateTime(txtEndDate.Value.ToString());
}
DataTable activityTable = ReportService.GetNewRegistrationReports(startDate, endDate, Convert.ToInt32(ddlAccountHandler.SelectedValue));
if (activityTable.Rows.Count != 0)
{
ExportReportServices.ReportFormats format;
format = ExportReportServices.ReportFormats.None;
string strExt = “”;
if (ddlFormat.SelectedValue.Equals(“1″))
{
format = ExportReportServices.ReportFormats.PDF;
strExt = “Pdf”;
}
else if (ddlFormat.SelectedValue.Equals(“2″))
{
format = ExportReportServices.ReportFormats.MS_Word;
strExt = “Doc”;
}
else if (ddlFormat.SelectedValue.Equals(“3″))
{
strExt = “xls”;
format = ExportReportServices.ReportFormats.MS_Excel;
}
else if (ddlFormat.SelectedValue.Equals(“4″))
{
format = ExportReportServices.ReportFormats.HTML;
strExt = “HTML”;
}
else if (ddlFormat.SelectedValue.Equals(“5″))
{
strExt = “RTF”;
format = ExportReportServices.ReportFormats.RTF;
}
lblStatus.Visible = false;
ExportReportServices exportReports = new ExportReportServices();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ContentType = “application/” + strExt;
HttpContext.Current.Response.AddHeader(“Content-Disposition”, “inline; filename=AccountActivity.” + strExt);
HttpContext.Current.Response.BinaryWrite(exportReports.ExportReportsTo(activityTable, reportPath, format));
HttpContext.Current.Response.End();
}
else
{
lblStatus.Text = “No Records to Display”;
lblStatus.Visible = true;
}
}
June 9, 2008 at 2:20 pm |
plz use this code for class lib
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.IO;
namespace Portal.BusinessServices
{
public class ExportReportServices
{
public enum ReportFormats : int
{
None = 0,
PDF = 1,
MS_Word = 2,
MS_Excel = 3,
HTML = 4,
RTF = 5
}
public byte[] ExportReportsTo(DataTable dt, string fileName, ReportFormats format)
{
CrystalDecisions.CrystalReports.Engine.ReportDocument doc = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
//DataSet ds = (DataSet)context.Cache.Get(“ExportData”);
doc.Load(fileName);
doc.SetDataSource(dt);
CrystalDecisions.Shared.ExportOptions exportOpts = doc.ExportOptions;
if (format == ReportFormats.PDF)
{
exportOpts.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat;
}
else if (format == ReportFormats.MS_Word)
{
exportOpts.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.WordForWindows;
}
else if (format == ReportFormats.MS_Excel)
{
exportOpts.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.Excel;
}
else if (format == ReportFormats.HTML)
{
exportOpts.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.HTML40;
}
else if (format == ReportFormats.RTF)
{
exportOpts.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.RichText;
}
exportOpts.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.NoDestination;
exportOpts.DestinationOptions = new CrystalDecisions.Shared.DiskFileDestinationOptions();
// Response.Write(doc.ExportToStream(exportOpts.ExportFormatType));
byte[] _stream = new byte[1024];
MemoryStream _ms = new MemoryStream();
_ms = (MemoryStream)doc.ExportToStream(exportOpts.ExportFormatType);
doc.Close();
doc.Dispose();
return _ms.ToArray();
}
}
}
June 11, 2008 at 3:53 am |
HttpContext.Current.Response.BinaryWrite(exportReports.ExportReportsTo(mDataset, Server.MapPath(“C:\\Documents and Settings\\rk0030836\\Desktop\\PROJECT\\CPMS_june6\\CPMS\\CrystalReport.rpt”), Format));
M GETTING
June 11, 2008 at 3:55 am |
ERROR!!…
“HTTPException was unhandled by user code” n getting error as the report path given is not a virtual path
September 6, 2009 at 2:03 am |
HI all, can we restrict download export options like i want to download report in pdf only . i am taking about the options that come in html view of reports