How to export dynamic columns to excel from grid

Hi. I have a problem in my app. I want to export dynamic columns to excel from grid. After click in button Excel file is generated but it is empty.

My grid:

@(Html.EJS().Grid("DefaultFunctionalities2").DataSource((IEnumerable<object>)Model.ReportSummary)
                    .AllowFiltering()
                    .AllowSorting()
                    .AllowExcelExport()
                    .AllowTextWrap()
                    .ToolbarClick("toolbarClick2")
                    .AllowMultiSorting()
                    .Locale("pl-PL")
                    .Load("onLoad2")
                    .FilterSettings(filter => { filter.Type(Syncfusion.EJ2.Grids.FilterType.Excel); })
                    .AllowPaging()
                    .PageSettings(page => page.PageCount(10).PageSize(50).PageSizes(new[] { 10, 25, 50, 100 }))
                    .Toolbar(new List<string>() { "Search" })
                    .Columns(ViewBag.cols)
                    .Render()
        )

Javascript:

<script>
        function toolbarClick2(args) {
            var gridObjj = document.getElementById("DefaultFunctionalities2").ej2_instances[0];
            if (args.item.id === 'DefaultFunctionalities2_excelexport') { 
                gridObjj.excelExport();
            }
        }
    </script>


I create columns in controller like this:

List<GridColumn> cols = new List<GridColumn>();
cols.Add(new GridColumn() { Field = "FullName", HeaderText = "Nazwisko Imię", Width = "auto", TextAlign = TextAlign.Right });


Thanks for your help.
Regards :)


1 Reply

MS Manivel Sellamuthu Syncfusion Team April 2, 2020 02:23 PM UTC

Hi Bart, 

Greetings from Syncfusion support. 

From your code for creating columns, we have noticed that you have used width as ‘auto’. By default Grid accepts the width of column in pixels or percentage and default value is ‘’. So we suggest you to modify the width of the column property based on that to resolve the issue. 

Code example:  

  Cols.Add(new GridColumn() { Field = "OrderID", Width = "", TextAlign = TextAlign.Right }); 

(Or) 
 
Cols.Add(new GridColumn() { Field = "OrderID", Width = "100px", TextAlign = TextAlign.Right }); 
 
 
(Or) 
 
Cols.Add(new GridColumn() { Field = "OrderID", Width = "30%", TextAlign = TextAlign.Right }); 
 


Please let us know if you need any further assistance. 

Regards, 
Manivel 


Loader.
Up arrow icon