# Export From Html Table or Database to Excel file

**URL:** <https://discourse.nodered.org/t/export-from-html-table-or-database-to-excel-file/1122>\
**Category:** General\
**Created:** [27 June 2018 13:48 UTC](https://discourse.nodered.org/t/export-from-html-table-or-database-to-excel-file/1122 "2018-06-27T13:48:08Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cristiano](https://avatars.discourse-cdn.com/v4/letter/c/b5e925/32.png) [@Cristiano](https://discourse.nodered.org/u/Cristiano)\
**Post date:** [27 June 2018 13:48 UTC](https://discourse.nodered.org/t/export-from-html-table-or-database-to-excel-file/1122/1 "2018-06-27T13:48:08Z")

</div>

Hello,

I need help solving a problem.

I have a table with values ​​and I wanted to export it to an excel or csv file.

the table I have is generated by a query the database and then it is shown by a table built in html.

I leave an attachment an example of what I have. ![fff](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/bc3613393f859ece2054c386082058d679d1d3a2.JPG)

Anyone have an idea where to start?  
thanks

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [27 June 2018 13:49 UTC](https://discourse.nodered.org/t/export-from-html-table-or-database-to-excel-file/1122/2 "2018-06-27T13:49:48Z")

</div>

How are you building that table? If you have the raw data in a flow then you can equally generate the CSV format of the data at the same time.

---

<div class="post-metadata">

**Author:** ![Cristiano](https://avatars.discourse-cdn.com/v4/letter/c/b5e925/32.png) [@Cristiano](https://discourse.nodered.org/u/Cristiano)\
**Post date:** [27 June 2018 14:03 UTC](https://discourse.nodered.org/t/export-from-html-table-or-database-to-excel-file/1122/3 "2018-06-27T14:03:13Z")

</div>

yes , using “template node” and this code

```
<table style="width:100%">
  <tr>
<th>Index</th> 
<th>Timestamp</th>
<th>Value</th> 
<th>Bool</th>
  </tr>
  <tr ng-repeat="x in msg.payload | limitTo:20">
<td>{{$index}}</td>
<td>{{msg.payload[$index].TIMESTAMP}}</td>
<td>{{msg.payload[$index].VALUE}}</td> 
<td>{{msg.payload[$index].BOOL}}</td>
  </tr>
</table>

```

Using the CSV node I only got one line.

thanks

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [27 June 2018 14:47 UTC](https://discourse.nodered.org/t/export-from-html-table-or-database-to-excel-file/1122/4 "2018-06-27T14:47:33Z")

</div>

So if you are building the html table from an array.

There are many different ways to iterate through this in Node-RED but a good one to learn would be to use a function and some javascript.  
Try reading [https://www.w3schools.com/js/js\_loop\_for.asp](https://www.w3schools.com/js/js_loop_for.asp) as well as [https://nodered.org/docs/user-guide/messages](https://nodered.org/docs/user-guide/messages)

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [27 June 2018 16:19 UTC](https://discourse.nodered.org/t/export-from-html-table-or-database-to-excel-file/1122/5 "2018-06-27T16:19:03Z")

</div>

If your payload contains an array of objects (like your query results), the `csv` node will convert that into a string of comma separated values, with individual “lines” separated by a newline character (linux), or by Cr/Nl (on windows):

```auto
TIMESTAMP,VALUE,BOOL
1530107171775,5,0
1530107151774,21,0
1530107131774,71,1

```

FYI, you can simplify your `ng-repeat` logic by _not_ using the index, something like this:

```auto
    <tr ng-repeat="row in msg.payload | limitTo:20">
        <td>{{$index}}</td>
        <td>{{row.TIMESTAMP}}</td>
        <td>{{row.VALUE}}</td> 
        <td>{{row.BOOL}}</td>
    </tr>

```

---

<div class="post-metadata">

**Author:** ![karlbo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/karlbo/32/68793_2.png) [@karlbo](https://discourse.nodered.org/u/karlbo)\
**Post date:** [29 June 2018 11:16 UTC](https://discourse.nodered.org/t/export-from-html-table-or-database-to-excel-file/1122/6 "2018-06-29T11:16:49Z")

</div>

```auto
<script>
function exportTableToExcel(yourtablename, filename = ''){
    var downloadLink;
    var dataType = 'application/vnd.ms-excel';
    var tableSelect = document.getElementById(yourtablename);
      var tableHTML = tableSelect.outerHTML.replace(/ /g, '%20');
    filename = filename?filename+'.xls':'Rapport.xls';
    downloadLink = document.createElement("a");
    document.body.appendChild(downloadLink);
    if(navigator.msSaveOrOpenBlob){
        var blob = new Blob(['\ufeff', tableHTML, text], {
            type: dataType
        });
        navigator.msSaveOrOpenBlob( blob, filename);
    }else{
        downloadLink.href = 'data:' + dataType + ', ' + tableHTML;
        downloadLink.download = filename;
        downloadLink.click();
    }
}
    </script>

<div>
   
<button onclick="exportTableToExcel('yourtablename')">SAVE TO EXEL</button>

</div>

```

Im using this code

Karl

---

<div class="post-metadata">

**Author:** ![Cristiano](https://avatars.discourse-cdn.com/v4/letter/c/b5e925/32.png) [@Cristiano](https://discourse.nodered.org/u/Cristiano)\
**Post date:** [9 July 2018 21:01 UTC](https://discourse.nodered.org/t/export-from-html-table-or-database-to-excel-file/1122/7 "2018-07-09T21:01:59Z")

</div>

hello,

thank you all for the help and answers.

Karlbo, the function works really well, thanks.

btw, do you have anything similar for .pdf?

thanks

---

<div class="post-metadata">

**Author:** ![hathemi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hathemi/32/3792_2.png) [@hathemi](https://discourse.nodered.org/u/hathemi)\
**Post date:** [21 November 2018 11:02 UTC](https://discourse.nodered.org/t/export-from-html-table-or-database-to-excel-file/1122/8 "2018-11-21T11:02:13Z")

</div>

did you find a solution for .pdf??

---

<div class="post-metadata">

**Author:** ![betternoel](https://avatars.discourse-cdn.com/v4/letter/b/90db22/32.png) [@betternoel](https://discourse.nodered.org/u/betternoel)\
**Post date:** [21 September 2020 02:00 UTC](https://discourse.nodered.org/t/export-from-html-table-or-database-to-excel-file/1122/9 "2020-09-21T02:00:08Z")

</div>

how to implement this code?

---

<div class="post-metadata">

**Author:** ![betternoel](https://avatars.discourse-cdn.com/v4/letter/b/90db22/32.png) [@betternoel](https://discourse.nodered.org/u/betternoel)\
**Post date:** [21 September 2020 02:00 UTC](https://discourse.nodered.org/t/export-from-html-table-or-database-to-excel-file/1122/10 "2020-09-21T02:00:51Z")

</div>

how to implement this code together with the preceding code above mention by cristiano?

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [21 September 2020 08:18 UTC](https://discourse.nodered.org/t/export-from-html-table-or-database-to-excel-file/1122/11 "2020-09-21T08:18:36Z")

</div>

This thread is two years old. It would be better if you open a new thread explainng your issue. Thanks.

---

<div class="post-metadata">

**Author:** ![Cristiano](https://avatars.discourse-cdn.com/v4/letter/c/b5e925/32.png) [@Cristiano](https://discourse.nodered.org/u/Cristiano)\
**Post date:** [22 September 2020 20:29 UTC](https://discourse.nodered.org/t/export-from-html-table-or-database-to-excel-file/1122/12 "2020-09-22T20:29:10Z")

</div>

yes i have something take a screenshoot and print in .pdf

```auto
<script src="http://mrrio.github.io/jsPDF/dist/jspdf.debug.js"></script>
<style>
table { 
    width: 750px; 
    border-collapse: collapse; 
    margin:10px auto;
    }
/* Zebra striping */
tr:nth-of-type(odd) { 
    background: #eee; 
    }
th { 
    background: #414c6a; 
    color: white; 
    font-weight: bold; 
    text-align: center; 
    }
td, th { 
    padding: 10px; 
    border: 1px solid #ccc; 
    text-align: right; 
    font-size: 18px;
    }
.button {
  background-color: #4CAF50; /* Green */
  border: none;
  color: white;
  padding: 8px 20px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  margin: 0px 20px;
  cursor: pointer;
  border-radius: 4px;
}

.button1 {/* Green */
  background-color: #4CAF50; 
  color: white; 
  border: 2px solid #4CAF50;
}

.button3 {/* red */
  background-color: #f44336; 
  color: white; 
  border: 2px solid #f44336;
}

</style>
<div id="customers">
    <table id="t01" class="mytable" ng-repeat="res in msg.payload.results">
        <tr>
            <th>Date and Time</th>
            <th>Effective Energy</th>
            <th>Reactive Energy</th>
        </tr>
        <tr ng-repeat="x in msg.payload.results[0].series[0].values">
            <td>{{msg.payload.results[0].series[0].values[$index][0]*1000 | date: "yyyy-MM-dd"}}</td>
            <td>{{(msg.payload.results[0].series[0].values[$index][1]/1000).toFixed(2)}} kWh</td>
            <td>{{(msg.payload.results[0].series[0].values[$index][2]/1000).toFixed(2)}} kVArh</td>
        </tr>
    </table>
</div>
<div>
  <button class="button button1" onclick="exportTableToExcel('t01')">CSV</button>
  <button class="button button3" id="btnExport2">PDF</button>
</div>

<script>
function exportTableToExcel(yourtablename, filename = ''){
    var downloadLink;
    var dataType = 'application/vnd.ms-excel';
    var tableSelect = document.getElementById(yourtablename);
      var tableHTML = tableSelect.outerHTML.replace(/ /g, '%20');
    filename = filename?filename+'.xls':'Rapport.xls';
    downloadLink = document.createElement("a");
    document.body.appendChild(downloadLink);
    if(navigator.msSaveOrOpenBlob){
        var blob = new Blob(['\ufeff', tableHTML, text], {
            type: dataType
        });
        navigator.msSaveOrOpenBlob( blob, filename);
    }else{
        downloadLink.href = 'data:' + dataType + ', ' + tableHTML;
        downloadLink.download = filename;
        downloadLink.click();
    }
}
</script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.22/pdfmake.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.min.js"></script>
<script type="text/javascript">
    $("body").on("click", "#btnExport2", function () {
        html2canvas($('#customers')[0], {
            onrendered: function (canvas) {
                var data = canvas.toDataURL();
                var docDefinition = {
                    content: [{
                        image: data,
                        width: 500
                    }]
                };
                pdfMake.createPdf(docDefinition).download("Table.pdf");
            }
        });
    });
</script>

```

my dashboard

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/5/35a8975436270e77645712e6ff3f4a713ebfda53.png)

document  
[Table (1).pdf](https://discourse.nodered.org/uploads/short-url/g6619UjJ9bpBMY82EBDOiz7ipSc.pdf) (42.4 KB)

---

<div class="post-metadata">

**Author:** ![pranavthakkar](https://avatars.discourse-cdn.com/v4/letter/p/e99b99/32.png) [@pranavthakkar](https://discourse.nodered.org/u/pranavthakkar)\
**Post date:** [3 July 2022 17:57 UTC](https://discourse.nodered.org/t/export-from-html-table-or-database-to-excel-file/1122/13 "2022-07-03T17:57:25Z")

</div>

Hi @Cristiano. any better option than a screenshot? or does table to excel have the option for including a chart or picture on it.
