Sử dụng ReportUtils

1. Report.aspx

2. Report.aspx.cs

public partial class Report : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//1. Get report id

string strReportId = Request.QueryString[“reportId”];

//2. Get ra data va nam
ReportData data = ReportUtils.GetReportData(strReportId);

//3. Goi ham Bind
ReportUtils.BindReport(CrystalReportViewer, data.ReportName, data.Data, data.Parameters);
}
}
}

3. Default.aspx.cs
protected void btnTTVienPhi_Click(object sender, EventArgs e)
{
// 1. Lay du lieu vao List
List lVienPhi = TaoDuLieuVienPhi();
// 1.1 Set Parameters
Dictionary param1 = new Dictionary();
param1.Add(“ThamDoi”,”Hi trung banh da thanh cong”);
param1.Add(“ThamDoi1”, “Thanh cong voi tham doi 1”);
// 2. Lay Report ID
Guid reportId = ReportUtils.SetReportData(“CrystalReportGroup.rpt”, lVienPhi,param1);
// 3. Redirect toi trang can hien thi
Response.Redirect(“Report.aspx?reportId=” + reportId.ToString());
}

Advertisements

ReportUtils cho Crystal Report

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
using System.Web;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Web;

public class ReportData
{
private string reportName;

public string ReportName
{
get { return reportName; }
set { reportName = value; }
}

private IEnumerable data;

public IEnumerable Data
{
get { return data; }
set { data = value; }
}

private Dictionary parameters;
public Dictionary Parameters
{
get { return parameters; }
set { parameters = value; }
}
}

public class ReportUtils
{
public ReportUtils()
{
}

public const string REPORTPATH = “{0}”;

public static Dictionary CurrentList
{
get
{
if (System.Web.HttpContext.Current.Session[“$#)$JDPNJ()#DJJ”] == null)
{
System.Web.HttpContext.Current.Session[“$#)$JDPNJ()#DJJ”] = new Dictionary();
}
return (Dictionary)System.Web.HttpContext.Current.Session[“$#)$JDPNJ()#DJJ”];
}
}

public static Guid SetReportData(string reportName, IEnumerable data)
{
return SetReportData(reportName, data, null);
}

public static Guid SetReportData(string reportName, IEnumerable data, Dictionary parameters)
{
Guid id = Guid.NewGuid();

ReportData reportData = new ReportData();
reportData.Data = data;
reportData.ReportName = reportName;
reportData.Parameters = parameters;

CurrentList.Add(id, reportData);

return id;
}

public static ReportData GetReportData(Guid id)
{
ReportData value;
if (CurrentList.TryGetValue(id, out value))
{
return CurrentList[id];
} else return null;
}

public static ReportData GetReportData(string id)
{
return GetReportData(new Guid(id));
}

public static void BindReport(CrystalReportViewerBase reportViewer, string reportName, IEnumerable reportData)
{
BindReport(reportViewer, reportName, reportData, null);
}

public static void BindReport(CrystalReportViewerBase reportViewer, string reportName, IEnumerable reportData, Dictionary parameters)
{
if (“error”.Equals(reportName) || reportData == null)
return;

ReportDocument reportDoc = new ReportDocument();

reportDoc.Load(HttpContext.Current.Server.MapPath(string.Format(REPORTPATH, reportName)));
reportDoc.SetDataSource(EnumerableToDataTable(reportData));

if (parameters != null)
{
foreach (KeyValuePair iPara in parameters)
reportDoc.SetParameterValue(iPara.Key, iPara.Value);
}

reportViewer.ReportSource = reportDoc;
reportViewer.DataBind();
}

#region EnumerableHelper

public static DataTable EnumerableToDataTable(IEnumerable collection)
{
DataTable table = new DataTable();
string tableName = collection.GetType().Name;
//string tableName = this.RasTable.get_Name();

IEnumerator enumerator = collection.GetEnumerator();
Type enumerableBaseType = GetEnumerableBaseType(enumerator, tableName);
if (enumerableBaseType != null)
{
table = BuildTableFromEnumerable(collection, enumerableBaseType, tableName);
}
//else
//{
// table = this.BuildTableFromFieldDefs(this.Fields, tableName);
//}

return table;
}

private static Type GetEnumerableBaseType(IEnumerator enumerator, string tableName)
{
Type type = null;
if ((enumerator != null) && enumerator.MoveNext())
{
object current = enumerator.Current;
enumerator.Reset();
type = current.GetType();
}
return type;
}

private static DataTable BuildTableFromEnumerable(IEnumerable collection, Type dataClassType, string tableName)
{
DataRow row;
int num;
DataTable table = new DataTable(tableName);
ArrayList list = new ArrayList();
PropertyInfo[] properties = dataClassType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
if (properties.Length > 0)
{
foreach (PropertyInfo info in properties)
{
ParameterInfo[] indexParameters = info.GetIndexParameters();
if ((indexParameters == null) || (indexParameters.Length == 0))
{
if (!info.PropertyType.IsGenericType)
{
table.Columns.Add(info.Name, info.PropertyType);
list.Add(info);
}
else
{
table.Columns.Add(info.Name, info.PropertyType.GetGenericArguments()[0]);
list.Add(info);
}
}
}
foreach (object obj2 in collection)
{
row = table.NewRow();
num = 0;
while (num < list.Count)
{
object objItem = ((PropertyInfo)list[num]).GetValue(obj2, null);
row[num] = objItem ?? DBNull.Value;
num++;
}
table.Rows.Add(row);
}
return table;
}
FieldInfo[] fields = dataClassType.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
foreach (FieldInfo info3 in fields)
{
table.Columns.Add(info3.Name, info3.FieldType);
list.Add(info3);
}
foreach (object obj2 in collection)
{
row = table.NewRow();
for (num = 0; num < list.Count; num++)
{
row[num] = ((FieldInfo)list[num]).GetValue(obj2);
}
table.Rows.Add(row);
}
return table;
}

private static DataTable BuildTableFromFieldDefs(DatabaseFieldDefinitions fieldDefs, string tableName)
{
DataTable table = new DataTable(tableName);
for (int i = 0; i < fieldDefs.Count; i++)
{
table.Columns.Add(fieldDefs[i].Name);
}
return table;
}

#endregion
}