简体   繁体   English

将带有 rowspan 和 colspan 的 html 表导出到 csv 或 xls

[英]Export html table with rowspan and colspan to csv or xls

I have a grid which looks as in the image below.我有一个如下图所示的网格。 在此处输入图片说明

Here we are using rowspans and colspans to make it span to some number of rows and columns.在这里,我们使用 rowspans 和 colspans 使其跨越一定数量的行和列。

I want to export the same content to either excel or csv but it should retain the format of rows and columns being spanned.我想将相同的内容导出到 excel 或 csv,但它应该保留被跨越的行和列的格式。

Is there an easier way I can do this using Javascript and HTML?有没有更简单的方法可以使用 Javascript 和 HTML 来做到这一点?

Short answer: Sorry, No. There is no easy way.简短回答:对不起,不。没有简单的方法。

Long answer: While there are easy ways to convert structured html tables to xls or csv (see for example: HTML Table to CSV/Excel Converter ), but the irregular and multi-level header in your table along with the multiple instances of colspan and rowspan , subvert the logical relationships and predictability in the table.长答案:虽然有一些简单的方法可以将结构化的 html 表转换为 xls 或 csv(参见示例: HTML 表到 CSV/Excel 转换器),但表中的不规则和多级标题以及colspanrowspan ,颠覆了表中的逻辑关系和可预测性。

Tip/Suggestion: With a few alterations to the structure of your table, it can easily be:提示/建议:通过对表格结构进行一些改动,它很容易成为:

  1. more machine readable, and thus easier to export and manipulate;机器可读性更强,因此更容易导出和操作;
  2. more human-readable, including more accessible to users using assistive technology.更具人类可读性,包括使用辅助技术的用户更易于访问

Specifically:具体来说:

  • Use a list, rather than table cells for listing the cities使用列表而不是表格单元格来列出城市
  • Nix any use of colspan and rowspan on the other cells禁止在其他单元rowspan上使用colspanrowspan
  • Delete the multi-level table header.删除多级表头。 The top row housing "Place desc" colspan is definitely redundant, and ultimately unnecessary.顶排房屋“Place desc” colspan绝对是多余的,最终是不必要的。

i had the same issue, solved by me self with the following code:我有同样的问题,我自己用以下代码解决了:

full code to download html table to csv file.将 html 表下载到 csv 文件的完整代码。

the code is structured only if you can base the col numbers in a specific row.只有当您可以将列号放在特定行中时,代码才会被结构化。 in my situation it's the last in .在我的情况下,它是 .

 function download_table_as_csv(separator = ',') { // Change here to your table with jquery selector, by table_id or table class. this code uses CLASS 'table'. var rows = $('.table tr'); // Construct csv var csv = []; var lastrow = []; var repeatrowval = []; var tabledata = []; // the code is structured only if you can base the col numbers in a spesific row. in my situation it's the last tr in thead. var cols = $('.table > thead > tr:last > th').length; $('.table > thead > tr:last > th').attr('rowSpan','1'); for (var i = 0; i < cols; i ++) {repeatrowval.push(1); lastrow.push('none')} for (var i = 0; i < rows.length -1; i++) { //loop every row console.log("row: " + i) var row = []; var col = rows[i].querySelectorAll('td, th'); var col_len = 0; for (var j = 0; j < cols; j++) { var a=0; console.log(repeatrowval[j]); if (repeatrowval[j] > 1) { data = lastrow[j]; repeatrowval[j] = repeatrowval[j] - 1; console.log("row: " + i + " reapet_col: " + j + " = " + data); row.push('"' + data + '"'); } else { if(col[col_len] === undefined) break; var colspan = col[col_len].colSpan ?? 1 ; console.log("col: " + j + ", colspan = " + colspan + ", repeatrowval: " + repeatrowval[j]) for (var r = 0; r < colspan; r++) { var rowspan = col[col_len].rowSpan ?? 1 ; //console.log('rowspan: ' +rowspan) if (rowspan == 1) { // Clean innertext to remove multiple spaces and jumpline (break csv) var data = col[col_len].innerText.replace(/(\\r\\n|\\n|\\r)/gm, '').replace(/(\\s\\s)/gm, ' ') // Escape double-quote with double-double-quote (see https://stackoverflow.com/questions/17808511/properly-escape-a-double-quote-in-csv) data = data.replace(/"/g, '""'); repeatrowval[j] = 1; lastrow[j] = data //} } else { // Clean innertext to remove multiple spaces and jumpline (break csv) var data = col[col_len].innerText.replace(/(\\r\\n|\\n|\\r)/gm, '').replace(/(\\s\\s)/gm, ' ') // Escape double-quote with double-double-quote (see https://stackoverflow.com/questions/17808511/properly-escape-a-double-quote-in-csv) data = data.replace(/"/g, '""'); lastrow[j] = data; repeatrowval[j] = rowspan; } // Push escaped string console.log("row: " + i + " col: " + j + " = " + data); row.push('"' + data + '"'); if(colspan > 1) {a++}; } col_len++ } if(a>0){j=+a}; } csv.push(row.join(separator)); tabledata.push(row); } var csv_string = csv.join('\\n'); // Download it var filename = 'export_Report_' + new Date().toLocaleDateString() + '.csv'; var link = document.createElement('a'); link.style.display = 'none'; link.setAttribute('target', '_blank'); link.setAttribute('href', 'data:text/csv;charset=utf-8,' + encodeURIComponent(csv_string)); link.setAttribute('download', filename); document.body.appendChild(link); link.click(); document.body.removeChild(link); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button onclick="download_table_as_csv();">Download Table as csv file</button> <table class="table"><thead><tr><th colspan="3" rowspan="2"></th><th colspan="7">Attendance</th></tr><tr><th colspan="7">Nov</th></tr><tr><th>Class</th><th rowspan="1" colspan="2">Student Name</th><th colspan="1" rowspan="2">17</th><th colspan="1" rowspan="2" >18</th><th colspan="1" rowspan="2" >19</th><th colspan="1" rowspan="2" >23</th><th colspan="1" rowspan="2" >24</th><th colspan="1" rowspan="2" >25</th><th colspan="1" rowspan="2" >26</th></tr></thead><tbody><tr><th rowspan="25" >2-A</th><th rowspan="1" colspan="2" >Alan, Ahoshe</th><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td></tr><tr><th rowspan="1" colspan="2" >Ael, Teddy</th><td>✅</td><td >✅</td><td >✅</td><td >✅</td><td >✅</td><td >✅</td><td >✅</td></tr><tr><th rowspan="1" colspan="2" >Bel, Kham</th><td>✅</td><td >✅</td><td >✅</td><td >✅</td><td >✅</td><td >✅</td><td >✅</td></tr><tr><th rowspan="1" colspan="2" >Fohr, Nlili</th><td>✅</td><td >❌</td><td >✅</td><td >❌</td><td >✅</td><td >❌</td><td >✅</td></tr><tr><th rowspan="1" colspan="2" >Foman, Mdel</th><td>✅</td><td >✅</td><td >✅</td><td >✅</td><td >✅</td><td >✅</td><td >✅</td></tr><tr><th rowspan="1" colspan="2" >Gorger, Cim</th><td>✅</td><td >✅</td><td >✅</td><td >✅</td><td >❌</td><td >✅</td><td >✅</td></tr><tr><th rowspan="1" colspan="2" >Goes, Joph</th><td>✅</td><td >✅</td><td >✅</td><td >✅</td><td >✅</td><td >✅</td><td >❌</td></tr><tr><th rowspan="1" colspan="2" >Hich, Cheel</th><td>✅</td><td >✅</td><td >✅</td><td >✅</td><td >✅</td><td >✅</td><td >✅</td></tr><tr><th rowspan="1" colspan="2" >Kain, Nali</th><td>✅</td><td >✅</td><td >✅</td><td >✅</td><td >✅</td><td >✅</td><td >✅</td></tr><tr><th rowspan="1" colspan="2" >Klon, Levpi</th><td>✅</td><td >✅</td><td>✅</td><td >✅</td><td >✅</td><td >✅</td><td >✅</td></tr><tr><th rowspan="1" colspan="2" >Le, Shlo</th><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td></tr><tr><th rowspan="1" colspan="2" >Ahi, Hufd</th><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td></tr><tr><th rowspan="1" colspan="2" >Leitz, Shru</th><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td></tr><tr><th rowspan="1" colspan="2" >Rerg, Aeh</th><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>❌</td><td>❌</td><td>✅</td></tr><tr><th rowspan="1" colspan="2" >Roeld, Did</th><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td></tr><tr><th rowspan="1" colspan="2" >Rothb, An</th><td>❌</td><td>❌</td><td>❌</td><td>✅</td><td>❌</td><td>❌</td><td>❌</td></tr><tr><th rowspan="1" colspan="2" >Rubein, Moha</th><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td></tr><tr><th rowspan="1" colspan="2" >Shel, Khak</th><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td></tr><tr><th rowspan="1" colspan="2" >Sofer, Shia</th><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td></tr><tr><th rowspan="1" colspan="2" >Strar, Jacob</th><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td></tr><tr><th rowspan="1" colspan="2" >Teim, Jalb</th><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td></tr><tr><th rowspan="1" colspan="2" >Wes, Pel</th><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td></tr><tr><th rowspan="1" colspan="2" >Wes, Shan</th><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td></tr><tr><th rowspan="1" colspan="2" >Wemer, Am</th><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>❌</td></tr><tr><th rowspan="1" colspan="2" >Werer, Son</th><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td></tr><tr><th rowspan="19" >2-B</th><th rowspan="1" colspan="2" >Ekin, Puda</th><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td></tr><tr><th rowspan="1" colspan="2" >Fon, Shpoem</th><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>❌</td><td>❌</td></tr><tr><th rowspan="1" colspan="2" >Fron, Aor</th><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td></tr></tbody></table>

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

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