Export From Html Table or Database to Excel file

#1

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

Anyone have an idea where to start?
thanks

0 Likes

#2

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.

0 Likes

#3

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

0 Likes

#4

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 as well as https://nodered.org/docs/user-guide/messages

0 Likes

#5

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):

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:

    <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>
0 Likes

#6
<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

0 Likes

#7

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

0 Likes

#8

did you find a solution for .pdf??

0 Likes