Dashboards & Visualizations

How to export tables in dashboard to Excel format using JavaScript?

marxsabandana
Path Finder

I got this code from CodexWorld(dot)com. How can I integrate this in a Splunk dashboard?

 function exportTableToExcel(tableID, filename = ''){
        var downloadLink;
        var dataType = 'application/vnd.ms-excel';
        var tableSelect = document.getElementById(tableID);
        var tableHTML = tableSelect.outerHTML.replace(/ /g, '%20');

        // Specify file name
        filename = filename?filename+'.xls':'excel_data.xls';

        // Create download link element
        downloadLink = document.createElement("a");

        document.body.appendChild(downloadLink);

        if(navigator.msSaveOrOpenBlob){
            var blob = new Blob(['\ufeff', tableHTML], {
                type: dataType
            });
            navigator.msSaveOrOpenBlob( blob, filename);
        }else{
            // Create a link to the file
            downloadLink.href = 'data:' + dataType + ', ' + tableHTML;

            // Setting the file name
            downloadLink.download = filename;

            //triggering the function
            downloadLink.click();
        }
    }

Then I put a button wrapped in an html tag, with putting an id in my table like this:

<html>
<button class="btn btn-primary" onclick="exportTableToExcel('tableid1', 'file-name')">Export Table Data To Excel File</button>
</html>

I also already tried using the Excel Export app at Splunkbase but I need to modify the code, to make the table headers in bold font weight and have dynamic column size depending on the text length of each headers.

pramit46
Contributor

Hi @marxsabandana

Please refer this question (the requirements was almost similar) and see if it helps:
https://answers.splunk.com/answers/749857/how-to-make-a-second-download-button.html#answer-749318

I have provided an example of csv file. But you can try to change the format to xls and see if that works.

0 Karma

marxsabandana
Path Finder

Thanks. Will check on it now.

0 Karma

pramit46
Contributor

Did it help? Were you looking for anything else?

0 Karma

at1ll3y
New Member

Hi,

you have to create a javascript file in App/appserver/static/your_script_name.js.

this file has to contain this:

require([
"splunkjs/mvc/simplexml/ready!"
], function() {

PUT YOUR CODE HERE

});

To reference this javascript file in your dashboard, you have to add it the header of the dashboard like this:

0 Karma
Get Updates on the Splunk Community!

Extending Observability Content to Splunk Cloud

Watch Now!   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to leverage ...

More Control Over Your Monitoring Costs with Archived Metrics!

What if there was a way you could keep all the metrics data you need while saving on storage costs?This is now ...

New in Observability Cloud - Explicit Bucket Histograms

Splunk introduces native support for histograms as a metric data type within Observability Cloud with Explicit ...