javascript html表格到javascript中的excel表格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11825566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 14:28:08  来源:igfitidea点击:

html table to excel sheet in javascript

javascripthtml

提问by sankar

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.

我正在使用我在网上找到的代码的这个特定部分,我正在尝试实现它,这个 javascript 获取显示在 html 表中的表的值并将其转换为 Excel 表。

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?

但是由于某些未知原因,代码的以下部分不起作用。它只能在 IE 中使用,我不确定为什么下面的代码不起作用。有人能告诉我这段代码有什么问题吗?

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

回答by Teemu

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

您的原始代码确实有效,只是for(i)-loop ( row_count == undefined) 中有一个错字。并且没有错误?但是,使用此代码,您可以摆脱在行中引用单元格的可怕技巧,它还可以打开“工作簿”而不是“对象”。

回答by randomuser

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?

它在哪里保存Excel表格或至少打开Excel表格?我正在我的 Wamp 服务器上尝试此代码,单击按钮后,我看不到任何操作。我如何检查是否发生了什么事?