ArticleZip > Export Html Table To Csv Using Vanilla Javascript

Export Html Table To Csv Using Vanilla Javascript

Exporting HTML Table to CSV Using Vanilla JavaScript

Have you ever needed to export data from an HTML table to a CSV file using just plain JavaScript? Well, you're in luck because in this article, we will walk you through how to accomplish just that! Exporting data from an HTML table to a CSV file can be a handy feature to have when working on web applications that require data manipulation or sharing data with others in a structured format.

First things first, let's understand what CSV is. CSV stands for Comma-Separated Values, and it's a common file format used for storing tabular data. Essentially, a CSV file is a plain text file where each line represents a record, and fields within each record are separated by commas.

Now, let's dive into the steps to export an HTML table to a CSV file using vanilla JavaScript.

Step 1: Get the HTML Table Data
The first step is to extract the data from the HTML table that you want to export. You can use JavaScript to select the table element and retrieve its rows and cells data. For example, you can use the `querySelectorAll` method to select all `

` elements within the table and iterate over them to get the cell values.

Step 2: Format the Data as CSV
Once you have retrieved the table data, the next step is to format it as CSV. To do this, you need to create a CSV string by combining the cell values with commas to separate the fields and newline characters to separate the records.

Step 3: Create and Download the CSV File
Now that you have the data formatted as CSV, it's time to create a CSV file and enable the user to download it. You can achieve this by creating a Blob object with the CSV data, creating a download link, and triggering a click event on the link to download the file.

Please note that the following code snippet provides a basic example of how to achieve this functionality:

Javascript

function exportTableToCSV() {
  const rows = document.querySelectorAll('table tr');
  let csv = '';

  rows.forEach((row) => {
    const rowData = Array.from(row.children).map((cell) => cell.innerText);
    csv += rowData.join(',') + 'n';
  });

  const blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' });
  const url = URL.createObjectURL(blob);
  const link = document.createElement('a');

  link.setAttribute('href', url);
  link.setAttribute('download', 'table_data.csv');
  link.style.display = 'none';

  document.body.appendChild(link);
  link.click();

  document.body.removeChild(link);
}

Step 4: Implement the Export Functionality
Lastly, you need to call the `exportTableToCSV` function when a user triggers the export action, such as clicking a button. You can add an event listener to the button element to handle the export functionality.

And there you have it! With these simple steps, you can export data from an HTML table to a CSV file using vanilla JavaScript. This functionality can enhance the user experience of your web application by providing an easy way to share and manipulate data. Give it a try in your next project and streamline your data exporting process!

×