javascript - 如何使用Javascript从Html表导出没有最后一行的Excel?

标签 javascript jquery

我可以从html表格导出excel。但是无法避免最后一行(tr),哪一行用于屏幕上的分页。所以这一行也显示在excel文件的结果中?

function DownloadToExcel(table){
  debugger;
  var t = document.getElementById(table);
  t.border = 1;
  var html = t.outerHTML;
  html = encodeURIComponent(html);

  var uri = 'data:application/vnd.ms-excel,' + html;

  var downloadLink = document.createElement("a");
  downloadLink.href = uri;
  downloadLink.download = "sauberkeit.xlsx";
  document.body.appendChild(downloadLink);
  downloadLink.click();
  document.body.removeChild(downloadLink);
}

最佳答案

只需在序列化 HTML 之前删除最后一行,然后再次将其添加回来。

var t = document.getElementById(table);
t.border = 1;
var last = t.lastElementChild;
t.removeChild(last);
var html = t.outerHTML;
t.appendChild(last);
...

关于javascript - 如何使用Javascript从Html表导出没有最后一行的Excel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49706188/

相关文章:

javascript - 总是成功的 jQuery promise ?

javascript - css flip counter - 无法设置正确的速度

javascript - 使用 JavaScript 切换菜单

javascript - 如何全局访问使用 JavaScript 加载的图像的属性

jquery - 使用 jQueryRotate 旋转后图像变大了一点

javascript - wordpress 函数没有被执行

javascript - 从 toUTCString() 开始的日期

javascript - 如何向水平滚动条添加或删除类?

javascript - 以空格分隔的样式字符串

javascript - 为什么我的ajax成功数据没有显示在Angular JS中