如何指定HTML应导出的行/表?

时间:2018-06-24 16:21:07

标签: javascript html csv export

嗨,我想将HTMl表下载到我的电脑上 我设法使用网上找到的示例代码来做到这一点 但是,这会下载显示的所有表格 如何指定要下载的表

function exportTableToCSV(filename) {
var csv = [];
var rows = document.querySelectorAll("table tr");

for (var i = 0; i < rows.length; i++) {
    var row = [], cols = rows[i].querySelectorAll("td, th");

    for (var j = 0; j < cols.length; j++) 
        row.push(cols[j].innerText);

    csv.push(row.join(","));        
}

// Download CSV file
downloadCSV(csv.join("\n"), filename);

}

1 个答案:

答案 0 :(得分:0)

function exportTableToCSV(tableNumber, filename) {
  var csv = [];
  var tables = document.querySelectorAll("table");

  var rows = tables[tableNumber].querySelectorAll("tr");

  for (var i = 0; i < rows.length; i++) {
    var row = [], cols = rows[i].querySelectorAll("td, th");

    for (var j = 0; j < cols.length; j++) 
      row.push(cols[j].innerText);

      csv.push(row.join(","));        
    }

    // Download CSV file
    downloadCSV(csv.join("\n"), filename);
  }
}

现在您可以输入要下载的表号