Download Excel in ASP.NET Core

Kailash Chandra Behera | Wednesday, July 08, 2020


Here in this blog Download Excel in ASP.NET Core,is creating in excel using javascript at runtime from HTML table and downloads data from SQL to excel without using any server-side code like C# or any library like Microsoft interop and third pary library like Open XML library.

import data from excel file to database table in asp net core

Download Excel in ASP.NET Core

Getting Started

We will download the data to excel from the HTML table which will render on your page using JavaScript code. Note that the data which appears on the page can be downloaded to excel if you have applied pagination and the data is hidden then the hidden data will not be downloaded.

The code has capable to draw borderlines of row and cell if you have applied it in HTML Table and will put the background-color of excel cells if it is there in the HTML table. Follow the below demonstration to Download Excel in ASP.NET Core.


In this demonstration, the JavaScript code is used to download the excel from page is creating a anchor tag and binds the HTML data to the src attribute of anchor tag at runtime. Then it calls the click function of anchor tag to download excel sheet.

The code can be used to download an excel sheet as well as csv file just you need to provide the required extension with filen name.

  1. Open your ASP NET core project where you want to download HTML table data to excel.

  2. Open the view when you want to download the data to excel.

  3. Give id to the HTML table which data you want to download, if you have already given ID to the table then it’s fine. I have given exportable.

  4. Add a button control like below code to the page. It will download the HTML data to excel.

     <button onclick="exportToExcel()">Export To Excel</button>  
  5. Copy the JavaScript codes(exportToExcel()) given below in this blog and past inside your HTML file or any other file like JavaScript file, where you think that is an appropriate place.

  6. Now you are done with Download Excel in ASP.NET Core, run you project and test the download.


 <script type="text/javascript">    
  function exportToExcel(){    
   var downloadurl;    
   var dataFileType = 'application/';    
   var tableSelect = document.getElementById('printTable');    
   var tableHTMLData = tableSelect.outerHTML.replace(/ /g, '%20');    
   // Specify file name    
   filename = 'StudentDetails.xls';    
   // Create download link element    
   downloadurl = document.createElement("a");    
   var blob = new Blob(['\ufeff', tableHTMLData], {    
    type: dataFileType    
   navigator.msSaveOrOpenBlob( blob, filename);    
   // Create a link to the file    
   downloadurl.href = 'data:' + dataFileType + ', ' + tableHTMLData;    
   // Setting the file name = filename;    
   //triggering the function;    

Download Excel in ASP.NET Core

Related Articles

  1. Import Excel in C#
  2. Creating controller for File Upload
  3. Export DataTable To Excel in C#
  4. Import XML into SQL Table
  5. Export Table Data into XML in SQL
  6. Export to Excel in MVC
  7. Export HTML Table to Excel Using JavaScript
  8. Validating Excel Sheet in C#
  9. Reading Excel file in AngularJS


Here in the above demonstration, we say how to Download Excel in ASP.NET Core with formatting like cell background, border etc. I hope you have enjoyed it a lot.