Basic Table to CSV

by Jonathan Sampson

HTML

<table id="sites">
  <thead>
    <tr>
      <th>Site</th>
      <th>Address</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Stack Overflow</td>
      <td>http://stackoverflow.com</td>
    </tr>
    <tr>
      <td>Microsoft Edge Dev</td>
      <td>http://dev.microsoftedge.com</td>
    </tr>
    <tr>
      <td>Twitter</td>
      <td>http://twitter.com</td>
    </tr>
  </tbody>
</table>

JavaScript

(function () {

  // Generate our CSV string from out HTML Table
  var csv = tableToCSV( document.querySelector( "#sites" ) );
  // Create a CSV Blob
  var blob = new Blob( [ csv ], { type: "text/csv"} );

  // Determine which approach to take for the download
  if ( navigator.msSaveOrOpenBlob ) {
    // Works for Internet Explorer and Microsoft Edge
    navigator.msSaveOrOpenBlob( blob, "output.csv" );

  } else {

    // Attempt to use an alternative method
    var anchor = document.body.appendChild(
      document.createElement( "a" )
    );
    // If the [download] attribute is supported, try to use it
    if ( "download" in anchor ) {
      anchor.download = "output.csv";
      anchor.href = URL.createObjectURL( blob );
      anchor.click();
    }

  }

  function tableToCSV( table ) {
    // We'll be co-opting `slice` to create arrays
    var slice = Array.prototype.slice;

    return slice.call( table.rows ).map(function ( row ) {
      return slice.call( row.cells ).map(function ( cell ) {
        return '"t"'.replace( "t", cell.textContent );
      }).join( "," );
    }).join( "\r\n" );

  }

}());