Implementing Excel export in ASP.NET5 using Open XML
September 01, 2015 by Anuraj
.Net ASP.Net ASP.Net MVC HTML5
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
Copyright © 2024 Anuraj. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Powered by Jekyll. Hosted with ❤ by GitHub