Export HTML Table Data To Excel Using JavaScript

Hello devs,

In this tutorial, we will see how to export a specific div as an excel file in javascript. We will create an onlick method with a button to export html table to excel with formatting javascript. When a user will click this button, we will export excel. I will create a simple simple user list to create this tutorial. You will learn export html div to excel using javascript in this example. 

Let's see the example code. First, create a button like that with table data:

 

Now write a javascript function like below:

function ExportToExcel(type, fn, dl) {
	var elt = document.getElementById('my_table');
	var wb = XLSX.utils.table_to_book(elt, { sheet: "sheet1" });
	return dl ?
	  XLSX.write(wb, { bookType: type, bookSST: true, type: 'base64' }):
	  XLSX.writeFile(wb, fn || ('user_list.' + (type || 'xlsx')));
}

 

Hope it can help you.

 

#javascript #jquery