简体   繁体   English

使用Javascript将可扩展表导出到excel文件

[英]Export expandable table to excel file using Javascript

I have an expandable table in html:我在 html 中有一个可扩展的表格:

在此处输入图片说明

How can I convert it into excel file with grouping, something like this:如何将其转换为带有分组的excel文件,如下所示:

在此处输入图片说明

Im not to sure about the grouping in XLS but the main way we convert data into XL is to use CSV我不确定 XLS 中的分组,但我们将数据转换为 XL 的主要方式是使用 CSV

You can parse the data from your HTML into an array then export that array to CSV您可以将 HTML 中的数据解析为数组,然后将该数组导出为 CSV

Here is the code to make your html table downloadable to CSV这是使您的 html 表可下载为 CSV 的代码

 var data = [ ['Foo', 'programmer'], ['Bar', 'bus driver'], ['Moo', 'Reindeer Hunter'] ]; function download_csv() { var csv = 'Name,Title\\n'; data.forEach(function(row) { csv += row.join(','); csv += "\\n"; }); console.log(csv); var hiddenElement = document.createElement('a'); hiddenElement.href = 'data:text/csv;charset=utf-8,' + encodeURI(csv); hiddenElement.target = '_blank'; hiddenElement.download = 'people.csv'; hiddenElement.click(); }
 <button onclick="download_csv()">Download CSV</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM