1

I'd like to client side table to Excel .xls, no server side. Following code includes simple table and Javascript and table to Excel successfully but doesn't work in Firefox or Chrome?

How can I run it in all browsers?

<script type="text/javascript">
function CreateExcelSheet()
{
 var x=myTable.rows
   var xls = new ActiveXObject("Excel.Application")
   xls.visible = true
   xls.Workbooks.Add
   for (i = 0; i < x.length; i++)
   {
    var y = x[i].cells
    for (j = 0; j < y.length; j++)
   {
   xls.Cells( i+1, j+1).Value = y[j].innerText
  }
  }
 }
</script>


<table id="myTable" border="1">
<tr> <b><td>Name </td> <td>Age</td></b></tr>
<tr> <td>Shivani </td> <td>25</td> </tr>
<tr> <td>Naren </td> <td>28</td> </tr>
<tr> <td>Logs</td> <td>57</td> </tr>
</table>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user1622511
  • 91
  • 1
  • 6

1 Answers1

0

ActiveXObject is an Internet Explorer only method. You can't do what you're trying to do purely with jquery/javascript (at least, not trivially)

You're either going to have to use a server-side application that can generate the Excel spreadsheets, or generate a CSV of your file, base64-encode it and navigate the browser to the base64'ed data using the data: URI scheme to trigger a 'download' of the contents you've generated.

PhonicUK
  • 13,486
  • 4
  • 43
  • 62
  • Link to someone using the URI scheme mentioned here: http://stackoverflow.com/questions/4639372/export-to-csv-in-jquery/13814984#13814984 – dah97765 Mar 27 '13 at 21:56