-3
function toExcel(tableID)
{    
    var detailsTable = document.getElementById(tableID);
    var oExcel = new ActiveXObject("Scripting.FileSystemObject");
    var oBook = oExcel.Workbooks.Add;
    var oSheet = oBook.Worksheets(1);
    for (var y=0;y<detailsTable.rows.length;y++)
    {
        for (var x=0;x<detailsTable.rows(y).cells.length;x++)
        {
            oSheet.Cells(y+1,x+1) =detailsTable.rows(y).cells(x).innerText;
        }
    }
    oExcel.Visible = true;
    oExcel.UserControl = true;
} 
cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
veena
  • 9
  • 1
  • 1
  • 4
    Questions like yours that consist only of some source code snippet with no context and description usually get closed. Here's an example of how to ask questions: http://tinyurl.com/so-hints – Darin Dimitrov Jun 29 '11 at 07:28

2 Answers2

0

As I posted here. However, can't test it myself. So let me know, if it works :)

<html>
<head>
<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>
</head>
<body>

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

<table id="myTable" border="1">
    <tr>
        <td>Name</td>
        <td>Age</td>
    </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>
    <tr>
        <td>Kas</td>
        <td>54</td>
    </tr>
    <tr>
        <td>Sent</td>
        <td>26</td>
    </tr>
    <tr>
        <td>Bruce</td>
        <td>7</td>
    </tr>
</table>

</body>
</html>
Community
  • 1
  • 1
Kalle H. Väravas
  • 3,579
  • 4
  • 30
  • 47
0
<script language="javascript">
function runApp()
{
        var Excel, Book;
        var x=myTable.rows;
        Excel = new ActiveXObject("Excel.Application");
        Excel.Visible = true;
        Book = Excel.Workbooks.Add();
        for (i = 0; i < x.length; i++)
        {
            var y = x[i].cells;
            for (j = 0; j < y.length; j++)
            {
                Book.ActiveSheet.Cells( i+1, j+1).Value = y[j].innerText;
            }
        }
}
</script>

And the HTML CODE

<table id="myTable">
    ....
</table>
<form>
   <input type="button" onclick="runApp()" value="Export">
</form>

This should do the job.

Flexo
  • 87,323
  • 22
  • 191
  • 272
John
  • 4,413
  • 6
  • 25
  • 28