<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Export to csv on click of button in Getting Data In</title>
    <link>https://community.splunk.com/t5/Getting-Data-In/Export-to-csv-on-click-of-button/m-p/695376#M115470</link>
    <description>&lt;P&gt;What if I want to download a PDF instead of csv is that possible?&lt;/P&gt;</description>
    <pubDate>Tue, 06 Aug 2024 06:59:48 GMT</pubDate>
    <dc:creator>Siddharthnegi</dc:creator>
    <dc:date>2024-08-06T06:59:48Z</dc:date>
    <item>
      <title>Export to csv on click of button</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Export-to-csv-on-click-of-button/m-p/444724#M77410</link>
      <description>&lt;P&gt;Hi everyone. I am just new to splunk and i am trying to create a function where I can export a table results to a csv file using a javascript. can someone tell me what can i do?&lt;/P&gt;

&lt;P&gt;Any help will be much appreciated!! &lt;/P&gt;</description>
      <pubDate>Fri, 28 Jun 2019 11:45:29 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Export-to-csv-on-click-of-button/m-p/444724#M77410</guid>
      <dc:creator>riya1</dc:creator>
      <dc:date>2019-06-28T11:45:29Z</dc:date>
    </item>
    <item>
      <title>Re: Export to csv on click of button</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Export-to-csv-on-click-of-button/m-p/444725#M77411</link>
      <description>&lt;P&gt;Hi riya1,&lt;BR /&gt;
if you are asking to export a table in a csv from the search form, see in on the top right part of the screen, under the time picker and near the print button: there's a button to export search results.&lt;BR /&gt;
If instead you are in a dashboard panel in the bottomof the panel, there's a button with a magnifying glass that does the same thing.&lt;/P&gt;

&lt;P&gt;Bye.&lt;BR /&gt;
Giuseppe&lt;/P&gt;</description>
      <pubDate>Fri, 28 Jun 2019 13:05:42 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Export-to-csv-on-click-of-button/m-p/444725#M77411</guid>
      <dc:creator>gcusello</dc:creator>
      <dc:date>2019-06-28T13:05:42Z</dc:date>
    </item>
    <item>
      <title>Re: Export to csv on click of button</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Export-to-csv-on-click-of-button/m-p/444726#M77412</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;

&lt;P&gt;Check this sample&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;&amp;lt;dashboard script="multi.js"&amp;gt;
  &amp;lt;label&amp;gt;csv&amp;lt;/label&amp;gt;
  &amp;lt;row&amp;gt;
    &amp;lt;panel&amp;gt;
      &amp;lt;html&amp;gt;
        &amp;lt;div id="tableplain"&amp;gt;&amp;lt;/div&amp;gt;
      &amp;lt;/html&amp;gt;
    &amp;lt;/panel&amp;gt;
  &amp;lt;/row&amp;gt;
  &amp;lt;row&amp;gt;
    &amp;lt;html&amp;gt;
      &amp;lt;button id="export"&amp;gt;Export&amp;lt;/button&amp;gt;
    &amp;lt;/html&amp;gt;
  &amp;lt;/row&amp;gt;
&amp;lt;/dashboard&amp;gt;
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;js:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;require([
    "jquery",
    "splunkjs/mvc/searchmanager",
    "splunkjs/mvc/tableview",
    "splunkjs/mvc/simplexml/ready!"
], function ($, SearchManager,TableView) {
    $(document).ready(function()
 {
    var search1 = new SearchManager({
        id: "search1",
        preview: true,
        cache: true,
        search: "index=_internal | stats count by sourcetype, source, host" 
    });

    var myplaintable = new TableView({
        managerid: "search1",
        el: $("#tableplain")
    }).render();

    $("#export").click(function(){
        var myResults = search1.data("results");
        myResults.on("data", function () {
             var data = myResults.collection().toJSON();
             console.log(data);
             DownloadJSON2CSV(data);
         });
      });


      function DownloadJSON2CSV(objArray)
      {
        var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
        var str = '';
        var headers = new Array();
        for (var i = 0; i &amp;lt; array.length; i++) {
            var line = '';
            var data = array[i];
            for (var index in data) {
                headers.push(index);
                if (line != '') {
                    line += ','
                }
                line += '"' + array[i][index] + '"';
                console.log('line: ' + line);
            }
            str += line + ((array.length&amp;gt;1) ? '\r\n' : '');
            line = '';
        }

        headers = getHeaders(headers);
        str = headers + '\r\n' + str;
        console.log('final : ' + str);
        var hiddenElement = document.createElement('a');
        hiddenElement.href = 'data:text/csv;charset=utf-8,' + encodeURI(str);
        hiddenElement.target = '_blank';
        hiddenElement.download ='download.csv';
        hiddenElement.click();
    }

    function getHeaders(a) {
        var temp = {};
        for (var i = 0; i &amp;lt; a.length; i++)
            temp[a[i]] = true;
        var r = [];
        for (var k in temp)
            r.push(k);
        return r;
    }

});
});
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;IMG src="https://i.ibb.co/JtQgW8H/csv.png" alt="alt text" /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Jul 2019 09:31:35 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Export-to-csv-on-click-of-button/m-p/444726#M77412</guid>
      <dc:creator>vnravikumar</dc:creator>
      <dc:date>2019-07-02T09:31:35Z</dc:date>
    </item>
    <item>
      <title>Re: Export to csv on click of button</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Export-to-csv-on-click-of-button/m-p/444727#M77413</link>
      <description>&lt;P&gt;the code works however if the data on the table is not yet populated, you will get infinite number of export file until the data are completed. that's the time that the export file will stop and will give a final export file.&lt;/P&gt;</description>
      <pubDate>Mon, 29 Jul 2019 08:18:44 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Export-to-csv-on-click-of-button/m-p/444727#M77413</guid>
      <dc:creator>shariinPH</dc:creator>
      <dc:date>2019-07-29T08:18:44Z</dc:date>
    </item>
    <item>
      <title>Re: Export to csv on click of button</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Export-to-csv-on-click-of-button/m-p/444728#M77414</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;

&lt;P&gt;Try this, the Export button will get enable after the data loaded completely in the table.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;&amp;lt;dashboard script="multi.js"&amp;gt;
  &amp;lt;label&amp;gt;csv&amp;lt;/label&amp;gt;
  &amp;lt;row&amp;gt;
    &amp;lt;panel&amp;gt;
      &amp;lt;html&amp;gt;
         &amp;lt;div id="tableplain"/&amp;gt;
       &amp;lt;/html&amp;gt;
    &amp;lt;/panel&amp;gt;
  &amp;lt;/row&amp;gt;
  &amp;lt;row&amp;gt;
    &amp;lt;html&amp;gt;
       &amp;lt;button id="export" disabled="true"&amp;gt;Export&amp;lt;/button&amp;gt;
     &amp;lt;/html&amp;gt;
  &amp;lt;/row&amp;gt;
&amp;lt;/dashboard&amp;gt;
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;js:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;require([
    "jquery",
    'splunkjs/mvc',
    "splunkjs/mvc/searchmanager",
    "splunkjs/mvc/tableview",
    "splunkjs/mvc/simplexml/ready!"
], function ($,mvc, SearchManager,TableView) {
    $(document).ready(function()
 {
    var tokens = mvc.Components.get("default");

    var search1 = new SearchManager({
        id: "search1",
        preview: true,
        cache: true,
        search: "index=_internal | stats count by sourcetype, source, host" 
    });

    search1.on('search:done', function() {
        $('#export').prop('disabled', false);
     });

    var myplaintable = new TableView({
        managerid: "search1",
        el: $("#tableplain")
    }).render();

    $("#export").click(function(){
        var myResults = search1.data("results");
        myResults.on("data", function () {
             var data = myResults.collection().toJSON();
             console.log(data);
             DownloadJSON2CSV(data);
         });

      });


      function DownloadJSON2CSV(objArray)
      {
        var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
        var str = '';
        var headers = new Array();
        for (var i = 0; i &amp;lt; array.length; i++) {
            var line = '';
            var data = array[i];
            for (var index in data) {
                headers.push(index);
                if (line != '') {
                    line += ','
                }
                line += '"' + array[i][index] + '"';
                console.log('line: ' + line);
            }
            str += line + ((array.length&amp;gt;1) ? '\r\n' : '');
            line = '';
        }

        headers = getHeaders(headers);
        str = headers + '\r\n' + str;
        console.log('final : ' + str);
        var hiddenElement = document.createElement('a');
        hiddenElement.href = 'data:text/csv;charset=utf-8,' + encodeURI(str);
        hiddenElement.target = '_blank';
        hiddenElement.download ='download.csv';
        hiddenElement.click();
    }

    function getHeaders(a) {
        var temp = {};
        for (var i = 0; i &amp;lt; a.length; i++)
            temp[a[i]] = true;
        var r = [];
        for (var k in temp)
            r.push(k);
        return r;
    }

});
});
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 29 Jul 2019 09:15:32 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Export-to-csv-on-click-of-button/m-p/444728#M77414</guid>
      <dc:creator>vnravikumar</dc:creator>
      <dc:date>2019-07-29T09:15:32Z</dc:date>
    </item>
    <item>
      <title>Re: Export to csv on click of button</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Export-to-csv-on-click-of-button/m-p/444729#M77415</link>
      <description>&lt;P&gt;Hello Vnravikumar, thanks for this.. hmm, do you happen to encounter to export without max lines? becausse this one limits to 100 results only&lt;/P&gt;</description>
      <pubDate>Fri, 02 Aug 2019 07:04:44 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Export-to-csv-on-click-of-button/m-p/444729#M77415</guid>
      <dc:creator>shariinPH</dc:creator>
      <dc:date>2019-08-02T07:04:44Z</dc:date>
    </item>
    <item>
      <title>Re: Export to csv on click of button</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Export-to-csv-on-click-of-button/m-p/444730#M77416</link>
      <description>&lt;P&gt;Hi Riya,&lt;/P&gt;

&lt;P&gt;Check the below example without use of JS file,&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;&amp;lt;form&amp;gt;
  &amp;lt;label&amp;gt;Download CSV file&amp;lt;/label&amp;gt;
  &amp;lt;row&amp;gt;
    &amp;lt;panel&amp;gt;
      &amp;lt;html depends="$export_button$"&amp;gt;
       &amp;lt;a class="btn btn-primary" role="button" href="/api/search/jobs/$export_sid$/results?isDownload=true&amp;amp;amp;timeFormat=%25FT%25T.%25Q%25%3Az&amp;amp;amp;maxLines=0&amp;amp;amp;count=0&amp;amp;amp;filename=$filename_token$&amp;amp;amp;outputMode=csv"&amp;gt;Download CSV&amp;lt;/a&amp;gt;
      &amp;lt;/html&amp;gt;
      &amp;lt;table&amp;gt;
        &amp;lt;search&amp;gt;
          &amp;lt;query&amp;gt;index=_internal | head 1000  | stats count by sourcetype, source, host&amp;lt;/query&amp;gt;
          &amp;lt;earliest&amp;gt;-24h@h&amp;lt;/earliest&amp;gt;
          &amp;lt;latest&amp;gt;now&amp;lt;/latest&amp;gt;
          &amp;lt;sampleRatio&amp;gt;1&amp;lt;/sampleRatio&amp;gt;
          &amp;lt;done&amp;gt;
            &amp;lt;set token="export_sid"&amp;gt;$job.sid$&amp;lt;/set&amp;gt;
            &amp;lt;set button="export_button"&amp;gt;1&amp;lt;/set&amp;gt;
          &amp;lt;/done&amp;gt;
        &amp;lt;/search&amp;gt;
        &amp;lt;option name="count"&amp;gt;100&amp;lt;/option&amp;gt;
        &amp;lt;option name="dataOverlayMode"&amp;gt;none&amp;lt;/option&amp;gt;
        &amp;lt;option name="drilldown"&amp;gt;none&amp;lt;/option&amp;gt;
        &amp;lt;option name="link.visible"&amp;gt;0&amp;lt;/option&amp;gt;
        &amp;lt;option name="percentagesRow"&amp;gt;false&amp;lt;/option&amp;gt;
        &amp;lt;option name="refresh.display"&amp;gt;progressbar&amp;lt;/option&amp;gt;
        &amp;lt;option name="rowNumbers"&amp;gt;false&amp;lt;/option&amp;gt;
        &amp;lt;option name="totalsRow"&amp;gt;false&amp;lt;/option&amp;gt;
        &amp;lt;option name="wrap"&amp;gt;true&amp;lt;/option&amp;gt;
      &amp;lt;/table&amp;gt;
    &amp;lt;/panel&amp;gt;
  &amp;lt;/row&amp;gt;
&amp;lt;/form&amp;gt;
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;It will display the Download CSV button, once query execution is completed.&lt;BR /&gt;
At line 6 &lt;CODE&gt;$filename_token$&lt;/CODE&gt; - you can set dynamic filename name or replace it with static filename.&lt;/P&gt;

&lt;P&gt;Please upvote and accept the answer if it helps.&lt;/P&gt;</description>
      <pubDate>Fri, 02 Aug 2019 08:27:47 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Export-to-csv-on-click-of-button/m-p/444730#M77416</guid>
      <dc:creator>gaurav_maniar</dc:creator>
      <dc:date>2019-08-02T08:27:47Z</dc:date>
    </item>
    <item>
      <title>Re: Export to csv on click of button</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Export-to-csv-on-click-of-button/m-p/444731#M77417</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;

&lt;P&gt;replace this line in js&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;var myResults = search1.data("results",{count: 0, output_mode: 'json_rows'});
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 02 Aug 2019 09:00:22 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Export-to-csv-on-click-of-button/m-p/444731#M77417</guid>
      <dc:creator>vnravikumar</dc:creator>
      <dc:date>2019-08-02T09:00:22Z</dc:date>
    </item>
    <item>
      <title>Re: Export to csv on click of button</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Export-to-csv-on-click-of-button/m-p/444732#M77418</link>
      <description>&lt;P&gt;thanks for all the help for the csv.. is there a way to implement this also in export xls?&lt;/P&gt;</description>
      <pubDate>Mon, 05 Aug 2019 06:03:23 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Export-to-csv-on-click-of-button/m-p/444732#M77418</guid>
      <dc:creator>shariinPH</dc:creator>
      <dc:date>2019-08-05T06:03:23Z</dc:date>
    </item>
    <item>
      <title>Re: Export to csv on click of button</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Export-to-csv-on-click-of-button/m-p/444733#M77419</link>
      <description>&lt;P&gt;This worked! Thanks!&lt;/P&gt;</description>
      <pubDate>Fri, 23 Aug 2019 08:04:42 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Export-to-csv-on-click-of-button/m-p/444733#M77419</guid>
      <dc:creator>afranciiine</dc:creator>
      <dc:date>2019-08-23T08:04:42Z</dc:date>
    </item>
    <item>
      <title>Re: Export to csv on click of button</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Export-to-csv-on-click-of-button/m-p/509366#M86648</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/156785"&gt;@vnravikumar&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for giving this code.&lt;/P&gt;&lt;P&gt;We are also working on this same future But when you click on export button it is giving more number of csv files instead of 1 (more than 5)&lt;/P&gt;&lt;P&gt;I tried the same code in search done as well .&lt;/P&gt;&lt;P&gt;Can you please help us to resolve this issue .&lt;/P&gt;&lt;P&gt;Thanks &amp;amp; Regards,&lt;/P&gt;&lt;P&gt;Manikanth&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jul 2020 18:04:31 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Export-to-csv-on-click-of-button/m-p/509366#M86648</guid>
      <dc:creator>manikanthkoti</dc:creator>
      <dc:date>2020-07-15T18:04:31Z</dc:date>
    </item>
    <item>
      <title>Re: Export to csv on click of button</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Export-to-csv-on-click-of-button/m-p/538100#M90206</link>
      <description>&lt;P&gt;Hi &lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/156785"&gt;@vnravikumar&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;Thanks for great export function. I have a bug, my search1 is listen to a token so everything the token has updated, it generate another export.&amp;nbsp;&lt;BR /&gt;token.set("original","new value")&lt;BR /&gt;Thanks in advance&lt;/P&gt;</description>
      <pubDate>Mon, 01 Feb 2021 15:01:23 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Export-to-csv-on-click-of-button/m-p/538100#M90206</guid>
      <dc:creator>ThuHuongLE1</dc:creator>
      <dc:date>2021-02-01T15:01:23Z</dc:date>
    </item>
    <item>
      <title>Re: Export to csv on click of button</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Export-to-csv-on-click-of-button/m-p/695376#M115470</link>
      <description>&lt;P&gt;What if I want to download a PDF instead of csv is that possible?&lt;/P&gt;</description>
      <pubDate>Tue, 06 Aug 2024 06:59:48 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Export-to-csv-on-click-of-button/m-p/695376#M115470</guid>
      <dc:creator>Siddharthnegi</dc:creator>
      <dc:date>2024-08-06T06:59:48Z</dc:date>
    </item>
    <item>
      <title>Re: Export to csv on click of button</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Export-to-csv-on-click-of-button/m-p/695950#M115520</link>
      <description>&lt;P&gt;The No-JS solution works wonderfully and you get my karma points.&lt;BR /&gt;&lt;BR /&gt;However, it doesn't seem to acknowledge the $filename_token$ (which I also set after the search is done, no need for extra tokens, the job_sid not being null is enough for depends), it always offers to save the file with name "results".&lt;BR /&gt;&lt;BR /&gt;Just a minor thing, it's still very usable and elegant.&lt;/P&gt;</description>
      <pubDate>Mon, 12 Aug 2024 06:55:43 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Export-to-csv-on-click-of-button/m-p/695950#M115520</guid>
      <dc:creator>lorenzoalbanof</dc:creator>
      <dc:date>2024-08-12T06:55:43Z</dc:date>
    </item>
  </channel>
</rss>

