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");
}
var DataSource = new NorthwindDataContext().OrdersViews.ToList() is just loading the data again... this is not what I require.
How do I have a Grid on my View, click a button and pass that grid exactly as is to the Action to be used as the Datasource?
If I use the following method, how do I pass the Grid's data as dataSource parameter?
public void ExportToExcel(string GridModel, IEnumerable dataSource)
{
var exp = new ExcelExport();
var DataSource = dataSource;
var obj = ConvertGridObject(GridModel);
exp.Export(obj, DataSource, "Export.xlsx", ExcelVersion.Excel2010, false, false, "default-theme");
}
regards,
N. Jamieson
@(Html.EJ().Grid<object>("FlatGrid")
.Datasource((IEnumerable<object>)ViewBag.dataSource)
.AllowPaging()
.ToolbarSettings(toolBar => toolBar.ShowToolbar(true).ToolbarItems(items =>
{
items.AddTool(ToolBarItems.ExcelExport);
items.AddTool(ToolBarItems.PdfExport);
items.AddTool(ToolBarItems.WordExport);
}))
.Columns(col =>
{
...
})
.ClientSideEvents(evt =>evt.Load("load"))
)
<script>
function load(args) {
this.ignoreOnExport.splice(this.ignoreOnExport.indexOf('dataSource'), 1);
}
</script>
public class HomeController : Controller
{
IEnumerable<EditableOrder> currentData = null;
...
public void ExportToExcel(string GridModel)
{
ExcelExport exp = new ExcelExport();
GridProperties obj = ConvertGridObject(GridModel);
exp.Export(obj, currentData, "Export.xlsx", ExcelVersion.Excel2010, false, false, "flat-saffron");
}
...
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")
{
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<EditableOrder>>(serial);
break;
}
}
}
}
}
...
return gridProp;
}
|