@(Html.EJ().Grid<object>("FlatGrid")
..
}).ClientSideEvents(eve=>eve.DataBound("databound")))
<script type="text/javascript">
function databound(args) {
var grid = $('#FlatGrid').ejGrid('instance');
grid.ignoreOnExport.splice(grid.ignoreOnExport.indexOf('dataSource'), 1); // here we will splice dataSource property in ignoreOnExport method
}
</script>
GridController.cs
public ActionResult GridFeatures()
{
var DataSource = new NorthwindDataContext().OrdersViews.Take(50).ToList();
ViewBag.datasource = DataSource;
return View();
}
public void ExportToExcel(string GridModel)
{
ExcelExport exp = new ExcelExport();
JavaScriptSerializer serializer = new JavaScriptSerializer();
GridProperties obj = ConvertGridObject(GridModel);
Type tableType = GetTableType(obj);
// Create instance of Array to the dynamic table type
var inst = Array.CreateInstance(tableType, 1);
string griddata = serializer.Serialize(obj.DataSource);
// serialize the export data as object array
obj.DataSource = serializer.Deserialize(griddata, inst.GetType()) as object[];
exp.Export(obj, (IEnumerable)(obj.DataSource), "Export.xlsx", ExcelVersion.Excel2010, false, false, "flat-saffron");
}
public static Type GetTableType(GridProperties gridProperty)
{
// Create needed TypeBuilder helpers
AppDomain myDomain = Thread.GetDomain();
AssemblyName myAsmName = new AssemblyName("Anonymous");
AssemblyBuilder myAsmBuilder = myDomain.DefineDynamicAssembly(myAsmName, AssemblyBuilderAccess.Run);
ModuleBuilder myModBuilder = myAsmBuilder.DefineDynamicModule(myAsmName.Name);
TypeBuilder myTypeBuilder = myModBuilder.DefineType("TestClass", TypeAttributes.Public);
foreach (var col in gridProperty.Columns)
{
Type PropertyType;
var PropertyName = col.Field;
if (col.CustomAttributes.Count > 0)
{
var colTypeAct = col.CustomAttributes["type"];
if (colTypeAct.Equals("duration"))
{
col.Type = "string";
}
}
if (col.HeaderText.Contains("<BR>"))
{
col.HeaderText = col.HeaderText.Replace("<BR>", "\n");
}
switch (col.Type)
{
case "number": PropertyType = Type.GetType("System.Decimal");
break;
case "string": PropertyType = Type.GetType("System.String");
break;
case "boolean": PropertyType = Type.GetType("System.Boolean");
break;
case "date": PropertyType = Type.GetType("System.DateTime");
break;
default: PropertyType = Type.GetType("System.String");
break;
}
FieldBuilder PropertyFieldBuilder = myTypeBuilder.DefineField(PropertyName.ToLower(), PropertyType, FieldAttributes.Private);
PropertyBuilder PBuilder = myTypeBuilder.DefineProperty(PropertyName, System.Reflection.PropertyAttributes.HasDefault, PropertyType, null);
MethodAttributes getSetAttr = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig;
MethodBuilder getPropertyBuilder = myTypeBuilder.DefineMethod("get" + PropertyName, getSetAttr, PropertyType, Type.EmptyTypes);
// Constructing IL Code for get and set Methods.
ILGenerator GetPropGenerator = getPropertyBuilder.GetILGenerator();
GetPropGenerator.Emit(OpCodes.Ldarg_0);
GetPropGenerator.Emit(OpCodes.Ldfld, PropertyFieldBuilder);
GetPropGenerator.Emit(OpCodes.Ret);
MethodBuilder setPropertyBuulder = myTypeBuilder.DefineMethod("set_" + PropertyName, getSetAttr, null, new Type[] { PropertyType });
ILGenerator SetPropGenerator = setPropertyBuulder.GetILGenerator();
SetPropGenerator.Emit(OpCodes.Ldarg_0);
SetPropGenerator.Emit(OpCodes.Ldarg_1);
SetPropGenerator.Emit(OpCodes.Stfld, PropertyFieldBuilder);
SetPropGenerator.Emit(OpCodes.Ret);
PBuilder.SetGetMethod(getPropertyBuilder);
PBuilder.SetSetMethod(setPropertyBuulder);
}
ConstructorInfo objCtor = typeof(object).GetConstructor(new Type[-1 + 1]);
ConstructorBuilder pointCtor = myTypeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, Type.EmptyTypes);
ILGenerator ctorIL = pointCtor.GetILGenerator();
// Constructing IL Code for the Type Constructor.
ctorIL.Emit(OpCodes.Ldarg_0);
ctorIL.Emit(OpCodes.Call, objCtor);
ctorIL.Emit(OpCodes.Ret);
return myTypeBuilder.CreateType();
}
private static PropertyInfo[] GetProperties(object obj)
{
return obj.GetType().GetProperties();
}
private GridProperties ConvertGridObject(string gridProperty)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
IEnumerable div = (IEnumerable)serializer.Deserialize(gridProperty, typeof(IEnumerable));
GridProperties gridProp = new GridProperties();
foreach (KeyValuePair<string, object> ds in div)
{
var property = gridProp.GetType().GetProperty(ds.Key, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase);
if (property != null)
{
Type type = property.PropertyType;
string serialize = serializer.Serialize(ds.Value);
object value = serializer.Deserialize(serialize, type);
property.SetValue(gridProp, value, null);
}
}
return gridProp;
} |
public void ExportToExcel(string GridModel)
{
ExcelExport exp = new ExcelExport();
JavaScriptSerializer serializer = new JavaScriptSerializer();
GridProperties obj = ConvertGridObject(GridModel);
Type tableType = GetTableType(obj);
// Create instance of Array to the dynamic table type
var inst = Array.CreateInstance(tableType, 1);
string griddata = serializer.Serialize(obj.DataSource);
// serialize the export data as object array
obj.DataSource = serializer.Deserialize(griddata, inst.GetType()) as object[];
for (int i = 0; i < 2; i++)
{
obj.Columns.RemoveAt(i);
}
exp.Export(obj, (IEnumerable)(obj.DataSource), "Export.xlsx", ExcelVersion.Excel2010, false, false, "flat-saffron");
} |