In this tutorial, we will learn how you can export (download) HTML table to CSV file using javascript. Firstly, we have to convert HTML table rows into a CSV file then we will export (download) it with the help of the javascript function.
Let’s get started!
Export HTML Table to CSV
To export the HTML table to a CSV file, we will follow the below points.
- Create a well structured HTML table with using proper <table> tags like: <tr>, <th>, <td>. Make sure your all table tags are closed.
- Will make export function to convert HTML to CSV
- Will make download function to download the CSV file
- Click event call when click on download button
Also Read: How to Convert HTML Table to PDF Using JavaScript?
So, let’s start with the first step to creating an HTML table.
Step 1: Create HTML Table with Well Structured
We will use the <table%gt; tag to create an HTML table and show the data in it as you want. To style the data in the table with rows and columns, we will use <tr>, <th>, and <td> tags.
Step 2: Create Export Function with JavaScript
Now, we will create a javascript function to convert HTML table rows (<tr>) data into CSV format. For this, we have to target each row of the table. Then we will loop through the rows and get the data of table cells in each row.
The above function will make new array data set with respective columns and rows. First, we make row data separated by a comma then pass the data variable in the downloadCSVFile()
function with each row start with a new line.
Step 3: Create Download File Function with JavaScript
In this function, we will create a new element by javascript to download the CSV file. First, we will use document.createElement()
to create an anchor (<a>) element then we will assign the attribute (href in our case) value using window.URL.createObjectURL()
. But this would be hidden element in the body tag.
And finally, hit that element by click()
function. See the code below.
Step 4: Call Click Event on Download Button
This is just a simple event trigger call when some click on the download button then execute the above both functions and download the file for you.
In the above code, we target the download button with ID and trigger the click()
event when clicking on the download button. It will get the table content and pass it into the htmlToCSV()
function to convert the table rows into CSV format.
Also Read: How to Convert HTML to PDF Using JavaScript?
Frequently Asked Questions
What is CSV File?
A CSV (comma-separated value) file is a text file that uses a comma delimiter to separate each value. It makes one line record set, which means each line of the file is a row of table data. In the CSV file, each line will have the same number of fields, and you can say it tabular data.
What is Blob?
Blob is a data type which has file like immutable object. It can read and store text and binary data then it will represent you in a human-readable format.
Can I create any element with createElement() function?
Yes, you can create any element with createElement() function. It create the element in HTML document with specified the HTML tag name. It will also create an element if tag name not recognized by HTML.
So, That's it!
Hope you understand all the code about exporting HTML table data to CSV format using javascript. But if you still have any questions please ask me in the comment section. I'll respond to you as soon as possible.
Hi!
Thank you so much for the code. I just have one question – can you help me change it so that the Header of the table is ignored? I only want the Data in the csv file; so the first line of the csv file must be the first record.
Hi Chrislee,
You can use the
shift()
function to remove the first item fromdata
array. Just place this line of codedata.shift()
after the for loop in htmlToCSV() function.Thank you for this great tip. Worked from the spot!
Glad! it worked for you 🙂
If someone wants to export cell where data is number_format(), here is nice addition:
.split(‘,’).join(”)
…
for (var j = 0; j < cols.length; j++) {
row.push(cols[j].innerText.split(',').join(''));
}
…
How to export pagination data? Now only 10 records exporting
Why pass in html variable and not use it?
Is it supposed to replace ‘document’ in htmlToCSV function?
Plz check Step 4,
html
variable used to refer to the HTML structure of table.Step 4 calls step 2, which never uses it. I would remove the .outerHTML and in step 2 use html as the actual table object to get the rows from thead and tbody.
Yes, you can customize the code as you want. All up to you.
Hi,
I copy and paste all code in the new HTML file but it does not work. I don’t see the button to click and download the table in the CSV format. Do you have a complete code example? Thanks in advance,
I just added this line and then it worked
Thanks!
Glad it worked for you
You can see the live demo on this link and download the code https://demo.yourblogcoach.com/s/html-to-csv-with-js/