0

function downloadCSV(tableId, filename) {
  // Get the table element.
  var table = document.getElementById(tableId);
   // Create an array to store the data from each row.
  var csvData = [];
   // Iterate over each row in the table.
  for (var i = 0; i < table.rows.length; i++) {
    // Create an array to store the data from each column in the row.
    var rowData = [];
     // Iterate over each column in the row.
    for (var j = 0; j < table.rows[i].cells.length; j++) {
      // Get the text content of the cell.
      var cellData = table.rows[i].cells[j].textContent;
       // Add the cell data to the row data array.
      rowData.push(cellData);
    }
     // Add the row data array to the csvData array.
    csvData.push(rowData);
  }
   // Create a CSV string from the csvData array.
  var csvString = csvData.join("\n");
   // Create a Blob object from the CSV string.
  var csvBlob = new Blob([csvString], {type: "text/csv"});
   // Create a download link.
  var downloadLink = document.createElement("a");
   // Set the href attribute of the download link to the Blob object.
  downloadLink.href = window.URL.createObjectURL(csvBlob);
   // Set the download attribute of the download link to the filename.
  downloadLink.download = filename;
   // Click the download link to download the CSV file.
  downloadLink.click();
}
downloadCSV("down1", "auto_datas.csv");

“down1” is table id

Jagdish Sarma Asked question August 29, 2023
Add a Comment