0

I am trying to create a JavaScript function that will export a HTML table to Excel. The code below works in Chrome, but in IE, I receive a popup saying "No apps are installed to open this type of link (data)." I need the export to happen on the client and using ActiveXObject is not an option. Any help on making this work in both IE and Chrome would be greatly appreciated.

var uri = "data:application/vnd.ms-excel;base64,"
var template = "<!DOCTYPE html><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> " + 
                   "   <meta http-equiv=Content-Type content='text/html;charset=windows-1252'><meta name=Generator content='Microsoft Excel 11'> " +
                   "   <meta name=ProgId content=Excel.Sheet> <meta name=Generator content='Microsoft Excel 11'> " +
                   "<style> " +
                   "<!--table @page{}-->" +
                   "</style>" +
                   "<!--[if gte mso 9]><xml> <x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet>  <x:Name>Sheet1</x:Name>    <x:WorksheetOptions><x:Panes> " +
                   "</x:Panes></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets><x:ExcelWorkbook></xml><![endif]--> " +
                   "</head>" +
                   "<body><form method='POST' action='PRJEdetail-ClientExportTest.asp' name='frmExcel'></form>" +
                   tableHTML +
                   "</body>" +
               "</html>"
var base64 = function (s) { return   window.btoa(unescape(encodeURIComponent(s))) }
window.location.href = uri + base64(template)
Isaac
  • 277
  • 4
  • 17
  • this question is answered here check 2nd answer for solution http://stackoverflow.com/questions/21012580/is-it-possible-to-write-data-to-file-using-only-javascript – Faraz May 25 '16 at 15:45
  • Are you referring to passing data to the server for processing? I've tried this, but some of my tables to export are large and I run into data limit issues when trying to pass the data to the server. That is why I'm trying to find a client side solution. – Isaac May 25 '16 at 15:52
  • No if you check solutions there they are generating export files using JS in browser. in second answer they provided a IE specific answer as well. – Faraz May 25 '16 at 16:00
  • Do you mean the solution posted by Useless Code using Blob? – Isaac May 25 '16 at 16:06
  • no, solution provided by Lifecube, Useless's code only export plain text files. but you can merge both and create a better solution for your self – Faraz May 25 '16 at 16:26
  • Unfortunately, my table is still proving to be too large. I think I'll have to save the data to a file, pass the file name to the server, and then process the file from there. I was hoping I could find a more straightforward solution. Thanks anyhow. – Isaac May 25 '16 at 16:54

0 Answers0