We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Export Grid Excel with Dynamic Columns

I want to export a data grid using 'Export Grid from Server'.

However, if there are dynamically generated columns, an error is generated in the file 'ej2_min'.

I would appreciate it if you could refer to the source and suggest a solution.

The error portion of the 'ej2 file'.

Below is the code.

   <button class="e-btn" onclick="Template_BtnClick()" style="font-size: 12px; height: 30px; width: 150px;">Template Download</button>


       <ejs-grid gridLines="Both" allowResizing="false" allowSelection="true" allowTextWrap="false" width="auto" height="690px" frozenColumns="3" created="IndexGridDataBinding"

                               queryCellInfo="DesignQueryCellEvent" cellEdit="IndexCellEdit" toolbar="toolbarItems" ToolbarClick="ToolbarClick" cellSaved="cellSaved" batchCancel="BatchCancel">

           <e-grid-textwrapsettings wrapMode="Header"></e-grid-textwrapsettings>

           <e-grid-editsettings allowAdding="false" allowEditing="true" allowDeleting="false" mode="Batch"></e-grid-editsettings>

           <e-grid-selectionsettings cellSelectionMode="Box" mode="Cell" type="Single"></e-grid-selectionsettings>

           <e-grid-columns>

               <e-grid-column field="MTO_KEY" headerText="MTO KEY" headerTextAlign="Center" textAlign="Left" width="140"></e-grid-column>

               <e-grid-column field="DESCRIPTION" headerText="DESCRIPTION" headerTextAlign="Center" textAlign="Left" width="220" edit="@(new { create = "Index_Create", read = "Index_Read", destory = "Index_Destory", write = "Index_Write"})"></e-grid-column>

               <e-grid-column field="UNIT" headerText="UNIT" headerTextAlign="Center" textAlign="Left" width="100" edit="@(new { create = "Index_Create", read = "Index_Read", destory = "Index_Destory", write = "Index_Write" })"></e-grid-column>

           </e-grid-columns>

       </ejs-grid>

 

   function IndexGridDataBinding() {

       if (freezeCheck) {

           freezeCheck = false;

           var mtoValue = document.getElementById('MTOType').ej2_instances[0].value;

 

           var header = document.getElementById('MenuTitle');

           header.innerText = headerTitle + " - PIPERACK";

 

           var seqList = document.getElementById('SeqType').ej2_instances[0];

 

           var seqNo = seqList.itemData;

 

           $.ajax({

               url: "/Bom/IndexHeaderBinding?sJobNo=" + jobNo + "&sMenuName=" + mtoValue,

               type: "POST",

               success: function (headerList) {

                   deleteHeaders = headerList;

                   var grid = document.getElementById('IndexGrid').ej2_instances[0];

                   var gridCols = grid.columns;

 

                   for (var i = 0; i < headerList.length; i++) {

                       gridCols.push(headerList[i]);

                   }

 

                   if (headerList.length > 0) {

                       $.ajax({

                           url: "/Bom/IndexDataBinding?sJobNo=" + jobNo + "&sMenuName=" + mtoValue + "&seqNo=" + seqNo,

                           type: "POST",

                           success: function (gridData) {

                               var settingGrid = document.getElementById('IndexGrid').ej2_instances[0];

                               settingGrid.dataSource = ej.data.DataUtil.parse.parseJson(gridData);

 

                               settingGrid.refreshColumns();

                               settingGrid.refresh();

                           },

                           error: function () {

                               alert("Please try again in a few minutes. If the same problem repeats, contact your administrator.");

                           }

                       });

                   }

               },

               error: function () {

                   alert("Please try again in a few minutes. If the same problem repeats, contact your administrator.");

               }

           });

       }

   }

 

   function Template_BtnClick() {

       var grid = document.getElementById("IndexGrid").ej2_instances[0];

       grid.serverExcelExport("/Bom/Export")

   }


4 Replies 1 reply marked as answer

TK TaeWook Kang March 6, 2023 07:29 AM UTC

I also attach a file that reenacts the error.


Attachment: DynamicalGridExcelExport_a3f0c12f.zip


RS Rajapandiyan Settu Syncfusion Team March 9, 2023 01:17 PM UTC

Hi Taewook,


Thanks for contacting Syncfusion support.


We suggest you to use the below code to resolve the reported problem.

 

    function IndexGridDataBinding() {

        if (initialRender) {

            initialRender = false;

            $.ajax({

                url: "/Home/GridHeaderAndDataBinding",

                type: "POST",

                success: function (headerList) {

                    var grid = document.getElementById('IndexGrid').ej2_instances[0];

                    var gridCols = grid.columns;

                    for (var i = 0; i < headerList.length; i++) {

                        delete headerList[i]["columns"];

                        gridCols.push(headerList[i]);

                    }

 

                    grid.refreshColumns();

 

                    if (headerList.length > 0) {

                        $.ajax({

                            url: "/Home/IndexDataBinding",

                            type: "POST",

                            success: function (gridData) {

                                var settingGrid = document.getElementById('IndexGrid').ej2_instances[0];

                                settingGrid.setProperties({

                                    dataSource: ej.data.DataUtil.parse.parseJson(gridData),

                                    columns: gridCols

                                }, true)

                                // refresh the Grid

                                settingGrid.freezeRefresh();

                            },

                        });

                    }

                },

            });

        }

    }

 


Regards,

Rajapandiyan S



TK TaeWook Kang March 9, 2023 11:57 PM UTC

Thank you for your answer



SG Suganya Gopinath Syncfusion Team March 10, 2023 05:26 AM UTC

Taewook, hope the provided solution helped to solve the issue. 

If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.



Marked as answer
Loader.
Up arrow icon