3

I need a JavaScript to export HTML tables to Excel. I have tried this script, but it is only exporting one table:

<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>

<input type="button" onclick="CreateExcelSheet()" value="Create Excel Sheet">

<table id='myTable'>
<td>
<tr>
.
.
.
.

So, is there any code that I can have to export N number of tables in the HTML?

Wayne Koorts
  • 10,861
  • 13
  • 46
  • 72
maas
  • 31
  • 1
  • 2
  • 1
    I'm confused what you need help with. You're saying there is only one worksheet? Or you want more than just the #myTable HTML table put into the XLS? If the latter, you just do another `xls.Workbooks.Add` and more JavaScript like the above but pointing to the other HTML table you want. – Jon Adams Oct 21 '11 at 15:33
  • as your question this code is exporting entire `myTable` to xls or you want to say it is exporting only one row ? – anshuVersatile Sep 29 '20 at 04:29

1 Answers1

0

Have you though of using php instead, and generate the excel on the backend.You could do this with ajax and then on callback redirect to the generated excel document, thus triggering download prompt.

http://phpexcel.codeplex.com/

Provides php to excel, This would allow cross browser compatibility rather than ActiveXObjects.

Bodman
  • 7,938
  • 3
  • 29
  • 34