Hi Manolo,
Thanks for using the Syncfusion product.
We suspect that you are using the external filter for grid. If so we need to pass filtered record to Export method.
But by default grid has inbuilt filtering support where you need not to pass filtered data to Export method.
Please find the code example to pass filtered data to export method.
Controller public void ExportToExcel(string GridModel) { ExcelExport exp = new ExcelExport(); var DataSource = new NorthwindDataContext().OrdersViews.ToList(); GridProperties obj = ConvertGridObject(GridModel); exp.Export(obj, DataSource, "Export.xlsx", ExcelVersion.Excel2010, false, false, "bootstrap-theme"); } |
We have created a simple sample with inbuilt filter support and it can be downloaded from the below location:
Sample Link: http://www.syncfusion.com/downloads/support/directtrac/119826/Exporting-715826328.zip
Please let us know if you require any further assistance on this.
Regards,
Isuriya R
Hi Manolo,
We can achieve your requirement by using filterColumn method of the grid. In filterColumn method we have to pass fieldname ,operator,value and comparer . we can apply filtering to grid through filterColumn method and we can get filtered column details in controller after exporting.
Please refer the below code snippets.
function click() { var obj = $("#FlatGrid").ejGrid('instance') obj.filterColumn("CustomerID", "equal", "ALFKI", "and", true); } |
As in the below screenshot ,while filtering the column using filterColumn method we can get the filtered details in the grid controller itself which can be passed to ExportToExcel method
While doing the filtering operation we must mention the allowFiltering property as a true.
We have created a sample and the same can be downloaded from the following location:
Sample Link: http://www.syncfusion.com/downloads/support/directtrac/119826/Exporting-1900867735.zip
Please refer below the documentation link for how we can do filtering for grid columns.
Online Link: https://help.syncfusion.com/api/js/ejgrid#methods:filtercolumn
Please try the above sample and let us know if it helps. If we misunderstood your query, please provide us clear information regarding your requirements. It will help us to provide the prompt solution.
Regards,
Isuriya R
Hi Manolo,
Thanks for using Syncfusion products.
By default, while exporting we ignore to pass the datasource with Grid model to Export action. Whereas we need to pass filter data in export function while using external filter. To avoid that, we are passing the datasource with model to Export action and directly exporting the Grid datasource. Please find the code example.
index.cshtml function create(args) { //ignore the dataSource property var index = $.inArray("dataSource", ej.Grid.prototype.ignoreOnExport); ej.Grid.prototype.ignoreOnExport.splice(index, 1);
}
Controller
public void ExportToExcel(string GridModel) { ExcelExport exp = new ExcelExport(); var DataSource = new NorthwindDataContext().OrdersViews.ToList(); GridProperties obj = (GridProperties)ConvertGridObject(GridModel); // filter dataSource exp.Export(obj, (IEnumerable)obj.DataSource, "Export.xlsx", ExcelVersion.Excel2010, false, false, "bootstrap-theme"); } 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); if (ds.Key == "dataSource") {
List<OrdersView> value1 = serializer.Deserialize<List<OrdersView>>(serialize); property.SetValue(gridProp, value1, null); continue;
}
object value = serializer.Deserialize(serialize, type); property.SetValue(gridProp, value, null); } } return gridProp; } |
We have created a sample and the same can be downloaded from the following location:
Sample Link: http://www.syncfusion.com/downloads/support/directtrac/119826/Grid-1285788659.zip
Please try the above sample and let us know if it helps. If we misunderstood your query, please provide us clear information regarding your requirements. It will help us to provide the prompt solution.
Regards,
Isuriya R