<ej:Grid ID="Grid" runat="server" AllowPaging="true"
OnServerWordExporting="Grid_ServerWordExporting"
OnServerPdfExporting="Grid_ServerPdfExporting"
OnServerExcelExporting="Grid_ServerExcelExporting">
<ToolbarSettings ShowToolbar="true" ToolbarItems="excelExport,wordExport,pdfExport"></ToolbarSettings>
<DataManager Adaptor="WebApiAdaptor" URL="/api/Orders" />
. . .
</ej:Grid>
public class OrdersController : ApiController
{
// GET api/<controller>
NorthWndDataContext db = new NorthWndDataContext();
public object Get()
{
. . .. .
var data = db.Orders;
return new { result = data.Skip(skip).Take(take), count = data.Count() };
}
. . .
}
public partial class _Default : Page
{
NorthWndDataContext db = new NorthWndDataContext();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Grid_ServerExcelExporting(object sender, Syncfusion.JavaScript.Web.GridEventArgs e)
{
ExcelExport exp = new ExcelExport();
exp.Export(Grid.Model, (IEnumerable)db.Orders, "Export.xlsx", ExcelVersion.Excel2010, true, true, "flat-lime");
}
. . .
} |
<ej:Grid ID="Grid" runat="server" AllowPaging="true"
OnServerWordExporting="Grid_ServerWordExporting"
OnServerPdfExporting="Grid_ServerPdfExporting"
OnServerExcelExporting="Grid_ServerExcelExporting">
<ToolbarSettings ShowToolbar="true" ToolbarItems="excelExport,wordExport,pdfExport"></ToolbarSettings>
<DataManager Adaptor="WebApiAdaptor" URL="/api/Orders" Offline="true" />
.. . .
<ClientSideEvents Load="onLoad" />
</ej:Grid>
<script>
function onLoad(args) {
this.ignoreOnExport.splice(this.ignoreOnExport.indexOf('dataSource'), 1);
}
</script>
public partial class _Default : Page
{
IEnumerable<Order> currentData;
. . . .
protected void Grid_ServerExcelExporting(object sender, Syncfusion.JavaScript.Web.GridEventArgs e)
{
ExcelExport exp = new ExcelExport();
string gridmodel = (string)e.Arguments.ToList()[0].Value; //collect the grid model
ConvertGridObject(gridmodel); //iterate and find the dataSource
exp.Export(Grid.Model, currentData, "Export.xlsx", ExcelVersion.Excel2010, true, true, "flat-lime");
}
private void 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)
{
//Check and retrieve additional property here
if (ds.Key == "dataSource")
{
foreach (KeyValuePair<string, object> data in (dynamic)ds.Value)
{
if (data.Key == "dataSource") {
foreach (KeyValuePair<string, object> data1 in (dynamic)data.Value)
{
if (data1.Key == "json") {
string serial = serializer.Serialize(data1.Value);
currentData = JsonConvert.DeserializeObject<IEnumerable<Order>>(serial);
break;
}
}
}
}
//
break;
}
}
}
} |
<ej:Grid ID="mainGrid" ClientIDMode="Static" runat="server">
. .
</ej:Grid>
protected void mainGrid_ServerExcelExporting(object sender, Syncfusion.JavaScript.Web.GridEventArgs e)
{
try
{
ExcelExport export = new ExcelExport();
string gridmodel = (string)e.Arguments.ToList()[0].Value; //collect the grid model
GridProperties gridProp = ConvertGridObject(gridmodel); //iterate and find the dataSource
export.Export(gridProp, (IEnumerable)currentData, "Export.xlsx", ExcelVersion.Excel2010, true, true, "flat-lime");
}
catch (Exception exp)
{
}
}
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)
{
//Check and retrieve additional property here
if (ds.Key == "dataSource")
{
. . . . . . .
}
else {
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;
} |