我正在尝试从数据表导出到 CSV 文件。这是我的全部功能。它下载了一个文件,但它显示了表格的整个代码并且没有分离出任何数据。当我说整个代码时,我指的是“表/表”中的所有内容
function displayDataTable(index){
$("#pageOverlay").empty();
html = "<div id='volumeChartDiv'><h4>Data Table</h4><table id='dataTable' class='table table-bordered table-striped dataTable' role='grid'><thead><tr role='row'><th>header1</th><th>header2</th><th>header3</th></tr></thead><tbody><tr><td>cell1-1</td><td>cell1-2</td><td>cell1-3</td></tr><tr><td>bell2-1</td><td>bell2-2</td><td>bell2-3</td></tr><tr><td>fell3-1</td><td>fell3-2</td><td>fell3-3</td></tr></tbody></table><input type='button' value='Close' onclick='closeOverlay()'> <input type='button' id='exportDataTable' value='Export Table'></div>";
$("#pageOverlay").html(html);
// export data function
$("#exportDataTable").click(function (e) {
window.open('data:application/vnd.ms-excel,' + $('.dataTable').html());
e.preventDefault();
});
openOverlay();
}
原文由 lostInTheTetons 发布,翻译遵循 CC BY-SA 4.0 许可协议
CSV 格式不能接受
$('.dataTable').html()
因为.html()
不是结构化数据,它甚至不是数据,只是一个愚蠢的 hmtl。你必须从你的表中获取数据,制作一个代表 CSV 格式的字符串,然后下载它,因为你没有在这里显示你的表数据结构,下面是工作演示
如果您发现任何错误,请在下方评论,我会修复它们