0

I could not export my HTML table data to Excel sheet using Chrome. It only happens while table has larger no of data but for small amount of data its working. Here is my code:

$scope.generateExcelSheet=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]; }) }
    var table='exportable';
    var name='Report';
    if (!table.nodeType) table = document.getElementById(table)
    var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
    window.location.href = uri + base64(format(template, ctx));
}

This code is working for less no of rows present inside that table for 'chrome' but while large no of row data is there its not working. I do not know why this is happening like this. I need to export the data into Excel sheet.

halfer
  • 19,824
  • 17
  • 99
  • 186
satya
  • 3,508
  • 11
  • 50
  • 130
  • It seems that chrome has a URL size limit of 2MB. See https://bugs.chromium.org/p/chromium/issues/detail?id=69227. So if your `data:` URI is to big, it will fail. But this approach for exporting Excel is anyway not recommendable. There are better possibilities to do so. [DataTables](https://datatables.net/extensions/buttons/examples/initialisation/export.html) for example. – Axel Richter Sep 30 '16 at 06:45
  • try this using Blob and BlobURL http://stackoverflow.com/questions/35686430/excel-export-in-javascript-using-blob-not-working-in-firefox – Omasu Plus Sep 30 '16 at 07:04
  • Let me try these and let u know. – satya Sep 30 '16 at 09:02
  • @OmasuPlus : I tried but the sheet is generating but its not coming in proper order.Check this [plunkr](https://plnkr.co/edit/BsZ0rXqjZKPwAhPGLAFp?p=preview). – satya Sep 30 '16 at 09:33
  • Your `table1` variable as the HTML template lacks a `` start tag. So instead `..."+table+"
    ...` set `..."+table+"
    ...`. But as said, there are better possibilities to export Excel from HTML using Javasript.
    – Axel Richter Sep 30 '16 at 09:57
  • @AxelRichter : Its exporting the blank file in my project. – satya Sep 30 '16 at 10:29
  • This https://plnkr.co/edit/7SpjRRdryHOjO3nSVp9n?p=preview works for me using Chrome. – Axel Richter Sep 30 '16 at 12:42
  • @AxelRichter : but its not working in safari.any solution for that. – satya Sep 30 '16 at 12:58

0 Answers0