Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6084703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:33:13+00:00 2026-05-23T11:33:13+00:00

My table is in format <table id=mytable> <thead> <tr> <th>name</th> <th>place</th> </tr> </thead> <tbody>

  • 0

My table is in format

<table id="mytable">
<thead>
  <tr>
    <th>name</th>
    <th>place</th>
  </tr>
</thead>
<tbody>
<tr>
   <td>adfas</td>
   <td>asdfasf</td>
</tr>
</tbody>
</table>

I found the following code online. But it doesn’t work if i use “thead” and “tbody” tags

function write_to_excel() {

    str = "";

    var mytable = document.getElementsByTagName("table")[0];
    var rowCount = mytable.rows.length;
    var colCount = 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 < rowCount; i++) {
        for (var j = 0; j < colCount; j++) {
            str = mytable.getElementsByTagName("tr")[i].getElementsByTagName("td")[j].innerHTML;
            ExcelSheet.ActiveSheet.Cells(i + 1, j + 1).Value = str;
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T11:33:13+00:00Added an answer on May 23, 2026 at 11:33 am

    The reason the solution you found on the internet is no working is because of the line that starts var colCount. The variable mytable only has two elements being <thead> and <tbody>. The var colCount line is looking for all the elements within mytable that are <tr>. The best thing you can do is give an id to your <thead> and <tbody> and then grab all the values based on that. Say you had <thead id='headers'> :

    function write_headers_to_excel() 
    {
      str="";
    
      var myTableHead = document.getElementById('headers');
      var rowCount = myTableHead.rows.length;
      var colCount = myTableHead.getElementsByTagName("tr")[0].getElementsByTagName("th").length; 
    
    var ExcelApp = new ActiveXObject("Excel.Application");
    var ExcelSheet = new ActiveXObject("Excel.Sheet");
    ExcelSheet.Application.Visible = true;
    
    for(var i=0; i<rowCount; i++) 
    {   
        for(var j=0; j<colCount; j++) 
        {           
            str= myTableHead.getElementsByTagName("tr")[i].getElementsByTagName("th")[j].innerHTML;
            ExcelSheet.ActiveSheet.Cells(i+1,j+1).Value = str;
        }
    }
    
    }
    

    and then do the same thing for the <tbody> tag.

    EDIT: I would also highly recommend using jQuery. It would shorten this up to:

    function write_to_excel() 
    {
     var ExcelApp = new ActiveXObject("Excel.Application");
     var ExcelSheet = new ActiveXObject("Excel.Sheet");
     ExcelSheet.Application.Visible = true; 
    
      $('th, td').each(function(i){
        ExcelSheet.ActiveSheet.Cells(i+1,i+1).Value = this.innerHTML;
      });
    }
    

    Now, of course, this is going to give you some formatting issues but you can work out how you want it formatted in Excel.

    EDIT: To answer your question about how to do this for n number of tables, the jQuery will do this already. To do it in raw Javascript, grab all the tables and then alter the function to be able to pass in the table as a parameter. For instance:

    var tables = document.getElementsByTagName('table');
    for(var i = 0; i < tables.length; i++)
    {
      write_headers_to_excel(tables[i]);
      write_bodies_to_excel(tables[i]);
    }
    

    Then change the function write_headers_to_excel() to function write_headers_to_excel(table). Then change var myTableHead = document.getElementById('headers'); to var myTableHead = table.getElementsByTagName('thead')[0];. Same with your write_bodies_to_excel() or however you want to set that up.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i used PageableListView in my table but the numbering produced in following simple format:
I have a clickable table row: $(#content-display).on('click', '#myTable tbody tr', function (){ foo(); });
i have a table like this : CREATE TABLE [Mytable]( [Name] [varchar](10), [number] [nvarchar](100)
I have a php script that backup my table in .sql format and I
In my table dates are stored in the format 2010-08-26 09:00:00 so yyyy-mm-dd. I'd
I want to display a ListView in table format. The ListView should have rows
I am converting a CSV file into a table format, and I wrote an
Results are in clean files. I want to get them to latex-table format with
I am trying to insert date in table in format DD.MM.YYYY, How do I
So I have a list box that displays averages in a table like format

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.