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

Issues with ASP.NET.MVC Grid Excel-Export

Dear all, 

I am having trouble following the example provided in the online documentation for registered users (https://bit.ly/2kNyMkw). I am trying to export the described Grid to Excel. I followed the steps provided by the documentation, with the exception that I created my own GridController and the corresponding View(s). 
The documentation mentions "to define the ExcelExport  in inbuild toolbar", but there is neither a hyperlink nor a description describing the necessary steps. The documentation continues to mention "define the ToolbarClick" event for exporting the Grid. Clicking on the link refers back to a the property description, but again there is no example given about how to apply the information to the actual ExcelExport.

I would like to know if somebody could provide an example for exporting data from a Grid into Excel in MVC5, ideally without any JavaScript.

Thanks a mil in advance for any help provided.



Attachment: files2_d024cac9.zip

5 Replies

SS Seeni Sakthi Kumar Seeni Raj Syncfusion Team September 18, 2019 09:32 AM UTC

 
Greetings from Syncfusion.  
 
In Essential JavaScript 2 Grid, we have provided the support to export the Grid content to Excel or PDF in client-end itself and it doesn’t need any server to export its contents. Refer to the demo and documentation. 
 
 
Regards,  
Seeni Sakthi Kumar S 




CR CR September 18, 2019 12:02 PM UTC

Dear Seeni Sakthi Kumar S,

Thanks a mil for getting back to me and sorry about the messy layout. I appreciate your help. Thank you also for providing the website links. I read the articles with great interest.
However, the information provided did not contain a solution for my present problem. I might need to rephrase what I am trying to accomplish. 

1. I cannot make heads or tails out of the sample that is provided at (https://bit.ly/2kNyMkw). I guess I followed the instructions (apart from creating my own GridController as previously mentioned, cf. files attached in last post). The solution compiles, but when I click the Excel-Export button nothing happens. I guess I must have made a mistake somewhere but given the current documentation I am unable to figure out exactly where the mistake might be hidden. 
2. The links provided are purely JavaScript. I might have not been too clear about it, but I would like to achieve the export without using any JavaScript (ideally). Am I thus right in the assumption that the solution provided at https://bit.ly/2kNyMkw) is the solution with the least amount of JavaScript involved when trying to export a data grid to Excel? 

I had something like this in mind for code behind (just theoretically speaking,)
string path = "output.xlsx";
grid1.ExportToXlsx([someParameters], path);
[... Some more code...]
[EndOfFunction]  ; 

Sorry for the misunderstanding. It would be grand if you or somebody could be of assisstance in resolving the present matter. Thank you very much in advance for your feeeback.

Kind regards

Chris


SS Seeni Sakthi Kumar Seeni Raj Syncfusion Team September 19, 2019 02:25 PM UTC

Hi Chris, 

Thanks for your update. 

Based on your requirement we have prepared sample with client-side and server-side excel export. In EJ 2 Grid we do not have support for server side export. For your requirement we have customized the export in server side as a workaround. Please find the below sample code and sample for more information. 

In external button, will submit the form to server-end which will bring the exported excel from the server-end whereas the toolbarclick even brings the excel file from the web-client using the ejs scripts. 
 
Index.cshtml 

 
@{ 
    ViewBag.Title = "Home Page"; 
} 
 
@using Syncfusion.EJ2 
@Html.EJS().Button("exportexcel").Content("Export server side").IsPrimary(true).Render() 
 
@(Html.EJS().Grid<EJ2Grid.Controllers.HomeController.Orders>("Grid").DataSource((IEnumerable<object>)ViewBag.data).AllowPaging(true).ToolbarClick("toolbarClick"). 
                 . . . 
Toolbar(new List<string>() { "ExcelExport" }).AllowExcelExport(true).Render()) 
 
<form action="/Home/ExcelExport" method="post"> 
    <input type="hidden" name="GridModel" id="hdnexcelexport" /> 
</form> 
 
<script> 
    document.getElementById("exportexcel").addEventListener('click', function () { 
        var form = document.getElementsByTagName("form")[0]; 
        form.submit(); 
    }); 
    function toolbarClick(args) { 
        var gridInstance = document.getElementById("Grid").ej2_instances[0]; 
        if (args.item.text === 'Excel Export') { 
            gridInstance.excelExport(); 
        } 
    } 
</script> 

Controller 

public void ExcelExport(string GridModel) 
        { 
            using (ExcelEngine excelEngine = new ExcelEngine()) 
            { 
                IApplication application = excelEngine.Excel; 
                application.DefaultVersion = ExcelVersion.Excel2013; 
                IWorkbook workbook = application.Workbooks.Create(1); 
                IWorksheet worksheet = workbook.Worksheets[0]; 
                IEnumerable DataSource = order; 
             
                //Import the data to worksheet 
                IList<Orders> reports = DataSource.AsQueryable().Cast<Orders>().ToList(); 
                worksheet.ImportData(reports, 2, 1, true);  
                workbook.SaveAs("Excel.xlsx", HttpContext.ApplicationInstance.Response, ExcelDownloadType.Open); 
            } 
        } 


Please get back to us, if you need further assistance. 

Regards, 
Seeni Sakthi Kumar S 



CR CR September 20, 2019 09:35 AM UTC

Dear Seeni Sakthi Kumar S, 

Thanks a mil for your reply and the sample you kindly prepared based on my recent comments. Also thank you for explaining the technical ramifications regarding my query. The sample works perfectly fine. I'll try to implement your sample into an existing project and I might get back to you in case I require further assistance. As for now I'd say the matter is closed. Thanks again for preparing the sample and for your general help. 

Kind regards.
Chris


SS Seeni Sakthi Kumar Seeni Raj Syncfusion Team September 20, 2019 10:56 AM UTC

Hi Chris,  
 
Thanks for your update.  
 
We are happy to hear that the provided solution helped you in achieving your requirement. Please get back to us, if you require further assistance on this.  
 
Regards,  
Seeni Sakthi Kumar S 


Loader.
Up arrow icon