0

function convertTableToCSV(table) {
    // Get the table header row.
    var headerRow = jQuery(table).find('tr:first');
     // Create a CSV string.
    var csvString = '';
     // Iterate over the header row cells and add them to the CSV string.
    headerRow.find('th').each(function() {
      var cellText = jQuery(this).text();
      // Escape single quotes and double quotes
      cellText = cellText.replace(/'/g, "''");
      cellText = cellText.replace(/"/g, '""');
      csvString += cellText + ',';
    });
     // Iterate over the table rows and add them to the CSV string.
    jQuery(table).find('tr').each(function() {
      var row = jQuery(this);
       // Iterate over the row cells and add them to the CSV string.
      row.find('td').each(function() {
        var cellText = jQuery(this).text();
        // Escape single quotes and double quotes
        cellText = cellText.replace(/'/g, "''");
        cellText = cellText.replace(/"/g, '""');
        csvString += '"' + cellText + '",';
      });
       // Add a newline character to the end of the row.
      csvString += '\n';
    });
     // Return the CSV string.
    return csvString;
  }
   function downloadCSV(filename, target) {
    // Get the table element.
    var table = jQuery(target);
     // Convert the table to CSV.
    var csvString = convertTableToCSV(table);
     // Create a Blob object from the CSV string.
    var blob = new Blob([csvString], { type: 'text/csv' });
     // Create a link element to download the CSV file.
    var link = document.createElement('a');
    link.href = URL.createObjectURL(blob);
    link.download = filename+'.csv';
     // Click the link to download the CSV file.
    link.click();
  }
  //downloadCSV();

Call function

downloadCSV(fileName,".downtab");

Jagdish Sarma Asked question February 12, 2024
Add a Comment