i have an asp.net-mvc web page and i am using jqgrid on the front end. i want to have an export to excel button that will export the current set of data (based on the curr
I succeed to make filtered export, taking inspiration by above @simpatric greg solution.
I set one session variable for each grid parameter, when data is requested and then passing again them to the excel export service. Greg's solution can work with asp.net MVC, which is ok for the main question. The following solution could be used with standard pure js jqgrid too:
CONTROLLER GRID ACTION
...
Session["jqsidx"] = sidx;
Session["jqsord"] = sord;
Session["jqpage"] = page;
Session["jqrows"] = rows;
Session["jq_search"] = _search;
Session["jqfilters"] = filters;
....
RECALLED INSIDE OF EXCEL EXPORT ACTION ^^
string sidx = Session["jqsidx"] as String;
string sord = Session["jqsord"] as String;
int? page = Session["jqpage"] as Nullable<Int32>;
int? rows = Session["jqrows"] as Nullable<Int32>;
bool? _search = Session["jq_search"] as Nullable<bool>;
string filters = Session["jqfilters"] as String;
var query = myqueryservice.getGridData(sidx, sord, (int)page, (int)rows, (bool)_search, filters, urlparams).ToList();
...
I hope this may help for other people having the same problem with standard jqgrid.
What I have done is put the gridstate into the cache each time data is requested, then I do the export to excel using the gridState. There are examples of this somewhere on the jqGrid site:
//this fragment in GetData method
Session["ExceptionGridState"] = gridModel.ExceptionGrid.GetState(true);
Then when the export is called:
public ActionResult ExportToExcel_CurrentData()
{
var gridModel = new ExceptionJqGridModel();
var grid = gridModel.ExceptionGrid;
// call the ExportToExcel built-in method
JQGridState gridState = Session["ExceptionGridState"] as JQGridState;
gridState.CurrentPageOnly = false;
grid.ExportToExcel(SourceQuery(),
String.Format("SomeDatasetName_Filtered_{0:yyyymmddhhmm}.xls",
DateTime.Now),
gridState);
return View();
}
This works for me.
You could put hidden fields inside the Export to Excel form:
@using (Html.BeginForm(new { action = "ExportToExcel" }))
{
@Html.Hidden("sidx")
@Html.Hidden("sord")
@Html.Hidden("page")
@Html.Hidden("rows")
<table id="list"></table>
<input type="submit" value="Export to Excel" />
}
and populate them upon form submission based on the current values:
$('form').submit(function () {
var grid = $('#list');
var sortname = grid.getGridParam('sortname');
var sortorder = grid.getGridParam('sortorder');
var page = grid.getGridParam('page');
var rows = grid.getGridParam('rowNum');
$('#sidx').val(sortname);
$('#sord').val(sortorder);
$('#page').val(page);
$('#rows').val(rows);
});
This way the ExportToExcel
controller action will take those parameters and be able to filter the list.