Posted by Anuraj on Tuesday, September 1, 2015
Today in ASP.NET forums I found one question about implementing Exel export from ASP.NET5 / MVC 6. So I thought I will implement and share the code. So this post is about implementing excel export using Open XML SDK in ASP.NET 5.
The question is about Excel export using Office Interop, as it is not a recommended approach and I am not sure, can we use COM / Interop in ASP.NET 5, I am using Open XML SDK.
First include the Open XML SDK in the project.json file.
Open XML SDK requires WindowsBase reference, so I removed the DNX Core framework reference and included WindowsBase as framework assembly. Now you need write the code for exporting excel from the model. The excel generation code I took from an SO post. In that code, the export was using Data Table, I modified the code to support model object.
And finally to download the file, you can use the FileContentResult type, to return byte array.
For some enterprise application, you may need to include Excel export option for all the Grids, for such purposes, you can create a ActionResult class, which accepts the model and exports the file.
Happy Programming :) The complete source code is available on GitHub