1

I am using this particular part of code which i found on the net, which i am trying to implement, this javascript takes the values of the table displayed in the html table and convert's it into excel sheet.

But for some unknown reason the below part of the code is not working. It can be used only in IE and i am not sure why the below code is not working. Can someone what is wrong with this code and can tell me how to correct this code?

<html>
  <head>
    <script type="text/javascript">
      function write_to_excel() {
        str="";
        var mytable = document.getElementsByTagName("table")[0]; 
        var row_Count = mytable.rows.length; 
        var col_Count = mytable.getElementsByTagName("tr")[0].getElementsByTagName("td").length; 

        var ExcelApp = new ActiveXObject("Excel.Application"); 
        var ExcelSheet = new ActiveXObject("Excel.Sheet"); 
        ExcelSheet.Application.Visible = true; 

        for(var i=0; i < row_count ; i++) 
        { 
          for(var j=0; j < col_Count; j++) 
          { 
            str= mytable.getElementsByTagName("tr")[i].getElementsByTagName("td")[j].innerHTML; 
            ExcelSheet.ActiveSheet.Cells(i+1,j+1).Value = str; 
          } 
        } 

      }
    </script>
    </script>
  </head> 
  <body> 

    <input type="submit" value="Export to EXCEL" onclick="write_to_excel();"/> 

    <!-- ************************************************--> 
    <!--**** INSERT THE TABLE YOU WANT EXPORT HERE ****--> 
    <table><tr><td>First</td><td>second</td></tr></table> 
    <!-- *******************example given above****************--> 

  </body> 
</html>
4
  • not the cause of your issue, but you have an extra closing </script> tag Commented Aug 6, 2012 at 9:29
  • You need to format the code so that we can read it. What kind of error are you getting? Commented Aug 6, 2012 at 9:29
  • the code should transfer the table into an excel sheet.I am not gettting any error. It opens an empty excel sheet with no values inserted in it. Commented Aug 6, 2012 at 9:47
  • @BillyMoon : even though i have taken out the extra </script> tag, it is still not working Commented Aug 6, 2012 at 9:49

2 Answers 2

7

This does the trick:

function writeToExcel() {
    var i, j, str,
        myTable = document.getElementById('mytable'),
        rowCount = myTable.rows.length,
        excel = new ActiveXObject('Excel.Application');// Activates Excel
    excel.Workbooks.Add(); // Opens a new Workbook
    excel.Application.Visible = true; // Shows Excel on the screen
    for (i = 0; i < rowCount; i++) {
        for (j = 0; j < myTable.rows[i].cells.length; j++) {
            str = myTable.rows[i].cells[j].innerText;
            excel.ActiveSheet.Cells(i + 1, j + 1).Value = str; // Writes to the sheet
        }
    }
    return;
}

Your original code actually works, there is just a typo in for(i)-loop (row_count == undefined). And no errors? However, with this code you can get rid of the horrible hack of referring cells in the rows, also it opens a "Workbook" instead of "Object".

Sign up to request clarification or add additional context in comments.

2 Comments

hi..i tried to use this code.but it shows the following error.ReferenceError: ActiveXObject is not defined [Break On This Error] excel = new ActiveXObject('Excel.Application');// Activates Excel
@DinoopNair (Un)fortenately ActiveXObject() is available in IE only ; ).
1

Where does it saves the excel sheet or atleast opens the excel sheet? I am trying this code on my Wamp Server and after clicking the button, I see no action. How do I check if anything happened?

1 Comment

Upvoted answer? @randomuser I've added some comments in my code. There is no saving function in the code, so the newly created Workbook is not saved programmatically.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.