-1

I came across this thread: https://stackoverflow.com/a/38328008/15691137

The script works to some extent, I was able to export the data into a xls file. But, the code has links to a deprecated html version. Also, the exported xls file gives an error when I try to open it which is as follows: enter image description here

How can I update the code to use html5 instead of 4?. Also, how can I fix the error that I am facing? Can someone please help me with this code. Thank you.

Add Export button:

<button  type="button" class="btn btn-warning btn-sm" onClick="tableToExcel('testTable', 'W3C Example Table')"   title="Export to Excel">Export</button></div><br/>

Add id to table:

<table class="table table-bordered table-hover " border="1" id="testTable" >

Add script at below of body:

<script type="text/javascript"> 
    var tableToExcel = (function() {
              var uri = 'data:application/vnd.ms-excel;base64,'
              , template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>'
              , base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }
              , format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) }
              return function(table, name) {
                if (!table.nodeType) table = document.getElementById(table)
                  var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
                window.location.href = uri + base64(format(template, ctx))
              }
            })()</script>
1
  • This isn't an answer, but a CSV file is simply formatted as "column,column,column". Commented Nov 26, 2021 at 11:54

1 Answer 1

0

To fix the error, right click the file -> Properties (or select the file and hit Alt+Enter), then check the box named 'Unblock'. That message actually isn't an error. It just warns you that the file may be corrupted.

Sign up to request clarification or add additional context in comments.

2 Comments

not helpful at all. Am I supposed to add this message for the users who are going to download the file?
I need permanent solution, thank you for your time.