1

I'm making a script that is supposed to export a html table to an xls file. I'm using Chrome when testing and it's no requirement that it's working on IE.

The problem is that the file that gets downloaded when clicking the export button is a text file.

Snippet from text file:

<table><tr><th>Mail</th><th>Telefonnummer</th></tr><td>asdasd@asdasd.com </td><td></td></tr><td>test@company.com </td><td></td></tr><td>asd@adsasd.com

My code:

var export_data = '';
        var objTable = document.getElementById('table');
        export_data += '<table><tr>';

        for(var i = 0; i < objTable.rows.length; i++){
            export_data += objTable.rows[i].innerHTML + '</tr>';
        }

        export_data += '</table>';




            sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(export_data));

        return sa;
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Svante
  • 135
  • 1
  • 6
  • Please ask your question more clean. – Ali Sheikhpour Jan 21 '18 at 20:48
  • @AliSheikhpour You will have to specify "clean" a bit more. – Svante Jan 21 '18 at 20:50
  • Your last sentences is totally meaningless. Export button is a txt file? The problem is that the file gets downloaded? Problem is download on click? Ask the main question again with new words please – Ali Sheikhpour Jan 21 '18 at 20:54
  • @AliSheikhpour I basically said that when i click a button that i called for "Export button" the file that i get downloaded (which is supposed to be a xls file) is not an xls file. It's a txt file. – Svante Jan 21 '18 at 20:56

2 Answers2

0

Why dont you write a csv file which contains the table titles and tabe contents comma separated. That is just plain text and some comma. you do not need the HTML tags for that.

var export_data = '';
var objTable = document.getElementById('table');
for(var i = 0; i < objTable.rows.length; i++){
    export_data += objTable.rows[i].innerHTML + '\r\n';
}
export_data.replace(/</t(h|d)><t(h|d)>/, '; ');
export_data.replace(/<t(h|d)>/, '');
export_data.replace(/</t(h|d)>/, '');

That file sould contain only:

Mail; Telefonnummer

asdasd@asdasd.com;

test@company.com;

asd@adsasd.com;

window.open('data:text/csv;charset=utf-8,' + encodeURIComponent(export_data));

Here you are going to find some information too: Using javascript to download file as a.csv file

D. Braun
  • 508
  • 1
  • 4
  • 11
0

Have you considering using application/csv and just piecing together the cell values in a straight forward way, using comma separated values?

In this example I generate 6 cells of data that can be opened in libreoffice (or also) Microsoft Excel.

<html>

<body>

<script language='JavaScript'>

function run_opencsv() {

    var export_data = '';

    export_data += 'Column1name, Column2name, Column3name' + '\n';

    export_data += 'Cell1, Cell2, Cell3' + '\n';

    export_data += 'Cell4, Cell5, Cell6' + '\n';

    sa = window.open('data:application/csv,' + encodeURIComponent(export_data));

    return sa;

    }

run_opencsv();

</script>

</body>

</html>

By the way, on my machine (using firefox) the code fragment you provided did generate an .xls data file. The only problem is it has the export_data text in it which is all still in html. A closer alternative might be to use .xlsx which is known as an Open XML format. So it will have tags in it, however it is in compressed zip format. You would have to use something like JSZip as a final step towards producing a download.

Basically you would be creating a set of .xml files in plain text similar to how you put together export_data, but then in the end you combine the .xml files using JSZip into an .xlsx for download.

Snohdo
  • 156
  • 5
  • Thanks! Yeah i tried it on another PC machine too and it worked. When it didn't work i was on a macbook. – Svante Jan 22 '18 at 16:26