Jaylen
Jaylen

Reputation: 40301

Export a single HTML table from a page to Excel page

I have been trying to add a function under all my existing reports that. the function will need to export a single table from the page and into an excel file.

I found a good solution online but needs some tweaking. This is the solution that I found JavaScript - export HTML table data into Excel

you can see how it works http://jsfiddle.net/Scipion/P8rpn/1/

However, this code is exporting all tables into excel file when the page first loads.

What I need to change it to do is to export a table based on a giving element ID and it need to export when a link is clicked not when a page loads.

This is what I have tried to to do so far but is not working

$(function(){

    $('#exp1').click(function(){
        //tableToExcel(document.getElementsByTagName("table"),"First Report");
        tableToExcel(document.getElementsById("testTable1"),"First Report");
    });

     $('#exp2').click(function(){
        tableToExcel(document.getElementsById("testTable2"),"Second Report");
    });
});

you can see my code js fiddle in here http://jsfiddle.net/P8rpn/702/

Upvotes: 0

Views: 1441

Answers (1)

gpojd
gpojd

Reputation: 23065

You need jQuery in your fiddle. Also, the table names do not match what you are trying to get in JavaScript.

http://jsfiddle.net/P8rpn/706/

$(function () {

    $('#exp1').click(function () {
        //tableToExcel(document.getElementsByTagName("table"),"First Report");
        tableToExcel($("#testTable1"), "First Report");
    });

    $('#exp2').click(function () {
        tableToExcel($("#testTable2"), "Second Report");
    });
});

Upvotes: 1

Related Questions