Introduction
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.
![]() |
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.
Demonstration
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.
Open your ASP NET core project where you want to download HTML table data to excel.
Open the view when you want to download the data to excel.
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.
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>
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.
Now you are done with Download Excel in ASP.NET Core, run you project and test the download.
exportToExcel()
<script type="text/javascript">
function exportToExcel(){
var downloadurl;
var dataFileType = 'application/vnd.ms-excel';
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");
document.body.appendChild(downloadurl);
if(navigator.msSaveOrOpenBlob){
var blob = new Blob(['\ufeff', tableHTMLData], {
type: dataFileType
});
navigator.msSaveOrOpenBlob( blob, filename);
}else{
// Create a link to the file
downloadurl.href = 'data:' + dataFileType + ', ' + tableHTMLData;
// Setting the file name
downloadurl.download = filename;
//triggering the function
downloadurl.click();
}
}
</script>
Download Excel in ASP.NET Core
Related Articles
- Import Excel in C#
- Creating controller for File Upload
- Export DataTable To Excel in C#
- Import XML into SQL Table
- Export Table Data into XML in SQL
- Export to Excel in MVC
- Export HTML Table to Excel Using JavaScript
- Validating Excel Sheet in C#
- Reading Excel file in AngularJS
Summary
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.
Thanks