Wednesday, 31 October 2012

Export Html table data to Excel using jQuery

<br/>
<div id="dvData">
<table>
    <tr>
        <th>Column One </th>
        <th>Column Two</th>
        <th>Column Three</th>
    </tr>
    <tr>
        <td>row1 Col1</td>
        <td>row1 Col2</td>
        <td>row1 Col3</td>
   </tr>
   <tr>
        <td>row2 Col1</td>
        <td>row2 Col2</td>
        <td>row2 Col3</td>
   </tr>
      <tr>
        <td>row3 Col1</td>
        <td>row3 Col2</td>
        <td>row3 Col3</td>  
   </tr>
</table>
</div>
<br/>
<input type="button" id="btnExport" value=" Export Table data into Excel " />

$("#btnExport").click(function(e) {
      window.open('data:application/vnd.ms-excel,' + encodeURIComponent($('#dvData').html()));
       e.preventDefault();
});

6 comments:

  1. why this code cannot work in IE??

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Ah!!! Sorry, before posting I didn't check that it is not working in IE. But this is not going to work in IE. To make it work in IE, you need to set response header. If you are using ASP.NET then

    Response.AddHeader("Content-Disposition", "attachment;filename=download.xls");

    You can find more details about this issue @

    http://stackoverflow.com/questions/11084232/export-html-table-to-excel-javascript-function-add-select-file-name
    http://stackoverflow.com/questions/393647/response-content-type-as-csv

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. this is not working. the file was downloaded but not in excel format

    ReplyDelete
  6. /**
    ** get data from the table
    **/
    $( '#btnExport' ).click(function () {
    var titles = [];
    var data = [];
    $( '.dataTable th' ).each( function () {
    titles.push($(this).text());
    });
    $( '.dataTable td' ).each( function () {
    data.push( $( this ).text () );
    });
    var CSVString = prepCSVRow( titles, titles.length, '' );
    CSVString = prepCSVRow( data, titles.length, CSVString );
    var downloadLink = document.createElement( "a" );
    var blob = new Blob( [ "\ufeff", CSVString ] );
    var url = URL.createObjectURL ( blob );
    var today = new Date ( );
    var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
    var time = today.getHours() + "-" + today.getMinutes() + "-" + today.getSeconds();
    var mili = today.getMilliseconds();
    var report = document.getElementById("change_name").innerHTML;
    var dateTime = report+'-'+date+'//'+time+"-"+mili;
    downloadLink.href = url;
    downloadLink.download = dateTime+".csv";

    document.body.appendChild ( downloadLink );
    downloadLink.click();
    document.body.removeChild ( downloadLink );
    });
    /**
    ** create CSX file
    **/
    function prepCSVRow ( arr, columnCount, initial ) {
    var row = '';
    var delimeter = ',';
    var newLine = '\r\n';
    function splitArray( _arr, _count ) {
    var splitted = [];
    var result = [];
    _arr.forEach ( function ( item, idx ) {
    if ( ( idx + 1 ) % _count === 0 ) {
    splitted.push ( item );
    result.push ( splitted );
    splitted = [];
    } else {
    splitted.push ( item );
    }
    });
    return result;
    }
    var plainArr = splitArray ( arr, columnCount );
    plainArr.forEach ( function ( arrItem ) {
    arrItem.forEach ( function ( item, idx ) {
    row += item + ( ( idx + 1 ) === arrItem.length ? '' : delimeter );
    });
    row += newLine;
    });
    return initial + row;
    }
    /*end of excel file export */

    ReplyDelete