当表格导出为EXCEL文件时,我希望表格中的所有数据也保存在所有单元格中.

这是我的脚本代码:

function exportTableToExcel(tableID, filename = ''){
    var downloadLink;
    var dataType = 'application/vnd.ms-excel';
    var tableSelect = document.getElementById(tableID);
    var tableHTML = tableSelect.outerHTML.replace(/ /g, '%20');    
    filename = filename?filename+'.xls':'excel_data.xls';    
    downloadLink = document.createElement("a");
    document.body.appendChild(downloadLink);
    if(navigator.msSaveOrOpenBlob){
        var blob = new Blob(['\ufeff', tableHTML], {
            type: dataType
        });
        navigator.msSaveOrOpenBlob( blob, filename);
    }
    else{
        downloadLink.href = 'data:' + dataType + ', ' + tableHTML;    
        downloadLink.download = filename;
        downloadLink.click();
    }
}

function addField(table){

  var tableRef = document.getElementById(table);
  var newRow   = tableRef.insertRow(-1);

  var newCell  = newRow.insertCell(0);
  var newElem = document.createElement( 'input' );
  newElem.setAttribute("name", "links");
  newElem.setAttribute("type", "text");
  newCell.appendChild(newElem);

  newCell = newRow.insertCell(1);
  newElem = document.createElement( 'input' );
  newElem.setAttribute("name", "keywords");
  newElem.setAttribute("type", "text");
  newCell.appendChild(newElem);

  newCell = newRow.insertCell(2);
  newElem = document.createElement( 'input' );
  newElem.setAttribute("name", "violationtype");
  newElem.setAttribute("type", "text");
  newCell.appendChild(newElem);

  newCell = newRow.insertCell(3);
  newElem = document.createElement( 'input' );
  newElem.setAttribute("type", "button");
  newElem.setAttribute("value", "Delete Row");
  newElem.setAttribute("onclick", 'SomeDeleteRowFunction(this)')
  newCell.appendChild(newElem);
}
    
window.SomeDeleteRowFunction = function SomeDeleteRowFunction(o) {
     var p=o.parentNode.parentNode;
     p.parentNode.removeChild(p);
}

这是我的网页地址

<table id="tblData" class="editable">
    <tr>
        <th>Name</th>
        <th>HW</th>
        <th>HW</th>
    </tr>
    <tr>
      <td><input type="text" name="links" /></td>
      <td><input type="text" name="keywords" /></td> 
      <td><input type="text" name="violationtype" /></td>
      <td><input type="button" value="Delete Row" onclick="SomeDeleteRowFunction(tblData)"/></td>          
    </tr>
</table>
<input type="submit" class="button" value="Add another line" onclick="addField('tblData');" />
<button onclick="exportTableToExcel('tblData', 'class1')">Save data</button>

该代码创建了一个能够创建更多行的表,当您双击这些单元格时,这些单元格是可编辑的.然后有一个按钮,允许将表格导出到EXCEL文件.除了当你点击刚刚下载的EXCEL文件时,它在单元格中没有任何数据之外,一切都是正常的.它知道添加了多少行,但不接受数据.

推荐答案

您的问题是,Excel将单元格的值基于元素的内容,而不是其属性.输入将它们的值保存在属性值中,因此您看不到它.

可以通过在导出之前将输入替换为它们的值来修复这个问题(首先创建一个副本,这样就不会 destruct 您的接口)

这将是您固定的exportTableToExcel函数(我注释了更改,其余代码是相同的)


        function exportTableToExcel(tableID, filename = '') {
            var downloadLink;
            var dataType = 'application/vnd.ms-excel';
            // Note the cloneNode(true) at the end
            // this creates a virtual copy of your table so you can alter it
            // without breaking the interface
            var tableSelect = document.getElementById(tableID).cloneNode(true);
            // replaces each input in the table with its value
            tableSelect.querySelectorAll("input").forEach(function (element) {
                element.closest("td").innerHTML = element.value
            })
            // tge rest is the same as in your code
            var tableHTML = tableSelect.outerHTML.replace(/ /g, '%20');
            filename = filename ? filename + '.xls' : 'excel_data.xls';
            downloadLink = document.createElement("a");
            document.body.appendChild(downloadLink);
            if (navigator.msSaveOrOpenBlob) {
                var blob = new Blob(['\ufeff', tableHTML], {
                    type: dataType
                });
                navigator.msSaveOrOpenBlob(blob, filename);
            }
            else {
                downloadLink.href = 'data:' + dataType + ', ' + tableHTML;
                downloadLink.download = filename;
                downloadLink.click();
            }
        }

这就是结果

enter image description here

请注意,按钮也会出现.您可能希望通过在克隆表中使用适当的 Select 器来删除它们.

Note:您使用的方法没有生成正确的EXCEL文件.虽然Excel可以打开该文件,但它会警告您它不完全符合该格式,因为Excel使用自己的XML struct ,这与HTML表格不同.如果你想要更好的结果,我建议你go 看看专门为此而建的库,比如exceljs或类似的库

Javascript相关问答推荐

了解Node.js中的EventEums和浏览器中的addEventEums之间的关系

从Node JS将对象数组中的数据插入Postgres表

如何使用子字符串在数组中搜索重复项

提交链接到AJAX数据结果的表单

更改预请求脚本中重用的JSON主体变量- Postman

类构造函数不能在没有用With Router包装的情况下调用

查询参数中的JAVASCRIPT/REACT中的括号

NG/Express API路由处理程序停止工作

面对代码中的错误作为前端与后端的集成

未捕获语法错误:Hello World中的令牌无效或意外

为列表中的项目设置动画

如何使用抽屉屏幕及其子屏幕/组件的上下文?

Socket.IO在刷新页面时执行函数两次

P5play SecurityError:无法从';窗口';读取命名属性';Add';:阻止具有源的帧访问跨源帧

JavaScript:多个图像错误处理程序

如何设置时间选取器的起始值,仅当它获得焦点时?

正在发出错误的URL请求

我如何让我的弹力球在JavaScript和HTML画布中相互碰撞后改变 colored颜色 ?

如何使用属性访问器定义循环的for...的局部变量

在Java脚本/类型脚本中覆盖父构造函数中的默认值