无法将表导出到csv(Can not export table to csv)

系统教程 行业动态 更新时间:2024-06-14 16:57:40
无法将表导出到csv(Can not export table to csv)

我想填写一个表并将其导出到csv。 以下是我的代码

<html> <head> <title></title> </head> <body> <div class="container"> <h1>Build Table</h1> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <div> <table id="blacklistgrid"> <tr id="Row1"> <td>Week Number</td> <td>Oranges Sold</td> <td>Apples Sold</td> </tr> <tr class="Row2"> <td> <input type="text"/> </td> <td> <input type="text"/> </td> <td> <input type="text"/> </td> </tr> </table> <button type="button" id="btnAdd">Add Row!</button> <button><a href="#" class="export">Export Table data into Excel</a></button> </div> <script> jQuery(document).ready(function () { jQuery('#btnAdd').click(function () { jQuery( ".Row2" ).clone().appendTo( "#blacklistgrid" ); }); }); $(document).ready(function() { function exportTableToCSV($table, filename) { var $rows = $table.find('tr:has(td)'), // Temporary delimiter characters unlikely to be typed by keyboard // This is to avoid accidentally splitting the actual contents tmpColDelim = String.fromCharCode(11), // vertical tab character tmpRowDelim = String.fromCharCode(0), // null character // actual delimiter characters for CSV format colDelim = '","', rowDelim = '"\r\n"', // Grab text from table into CSV formatted string csv = '"' + $rows.map(function(i, row) { var $row = $(row), $cols = $row.find('td'); return $cols.map(function(j, col) { var $col = $(col), text = $col.text(); return text.replace(/"/g, '""'); // escape double quotes }).get().join(tmpColDelim); }).get().join(tmpRowDelim) .split(tmpRowDelim).join(rowDelim) .split(tmpColDelim).join(colDelim) + '"'; // Deliberate 'false', see comment below if (false && window.navigator.msSaveBlob) { var blob = new Blob([decodeURIComponent(csv)], { type: 'text/csv;charset=utf8' }); // Crashes in IE 10, IE 11 and Microsoft Edge // See MS Edge Issue #10396033 // Hence, the deliberate 'false' // This is here just for completeness // Remove the 'false' at your own risk window.navigator.msSaveBlob(blob, filename); } else if (window.Blob && window.URL) { // HTML5 Blob var blob = new Blob([csv], { type: 'text/csv;charset=utf-8' }); var csvUrl = URL.createObjectURL(blob); $(this) .attr({ 'download': filename, 'href': csvUrl }); } else { // Data URI var csvData = 'data:application/csv;charset=utf-8,' + encodeURIComponent(csv); $(this) .attr({ 'download': filename, 'href': csvData, 'target': '_blank' }); } } // This must be a hyperlink $(".export").on('click', function(event) { // CSV var args = [$('#blacklistgrid'), 'export.csv']; exportTableToCSV.apply(this, args); // If CSV, don't do event.preventDefault() or return false // We actually need this to be a typical hyperlink }); }); </script> </body> </html>

我可以填充表格并动态添加行。 但是,我似乎无法将填充的数据导出到csv文件。 我究竟做错了什么?

I want to fill a table and export it to csv. The following is my code

<html> <head> <title></title> </head> <body> <div class="container"> <h1>Build Table</h1> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <div> <table id="blacklistgrid"> <tr id="Row1"> <td>Week Number</td> <td>Oranges Sold</td> <td>Apples Sold</td> </tr> <tr class="Row2"> <td> <input type="text"/> </td> <td> <input type="text"/> </td> <td> <input type="text"/> </td> </tr> </table> <button type="button" id="btnAdd">Add Row!</button> <button><a href="#" class="export">Export Table data into Excel</a></button> </div> <script> jQuery(document).ready(function () { jQuery('#btnAdd').click(function () { jQuery( ".Row2" ).clone().appendTo( "#blacklistgrid" ); }); }); $(document).ready(function() { function exportTableToCSV($table, filename) { var $rows = $table.find('tr:has(td)'), // Temporary delimiter characters unlikely to be typed by keyboard // This is to avoid accidentally splitting the actual contents tmpColDelim = String.fromCharCode(11), // vertical tab character tmpRowDelim = String.fromCharCode(0), // null character // actual delimiter characters for CSV format colDelim = '","', rowDelim = '"\r\n"', // Grab text from table into CSV formatted string csv = '"' + $rows.map(function(i, row) { var $row = $(row), $cols = $row.find('td'); return $cols.map(function(j, col) { var $col = $(col), text = $col.text(); return text.replace(/"/g, '""'); // escape double quotes }).get().join(tmpColDelim); }).get().join(tmpRowDelim) .split(tmpRowDelim).join(rowDelim) .split(tmpColDelim).join(colDelim) + '"'; // Deliberate 'false', see comment below if (false && window.navigator.msSaveBlob) { var blob = new Blob([decodeURIComponent(csv)], { type: 'text/csv;charset=utf8' }); // Crashes in IE 10, IE 11 and Microsoft Edge // See MS Edge Issue #10396033 // Hence, the deliberate 'false' // This is here just for completeness // Remove the 'false' at your own risk window.navigator.msSaveBlob(blob, filename); } else if (window.Blob && window.URL) { // HTML5 Blob var blob = new Blob([csv], { type: 'text/csv;charset=utf-8' }); var csvUrl = URL.createObjectURL(blob); $(this) .attr({ 'download': filename, 'href': csvUrl }); } else { // Data URI var csvData = 'data:application/csv;charset=utf-8,' + encodeURIComponent(csv); $(this) .attr({ 'download': filename, 'href': csvData, 'target': '_blank' }); } } // This must be a hyperlink $(".export").on('click', function(event) { // CSV var args = [$('#blacklistgrid'), 'export.csv']; exportTableToCSV.apply(this, args); // If CSV, don't do event.preventDefault() or return false // We actually need this to be a typical hyperlink }); }); </script> </body> </html>

I can fill the table and add rows dynamically. However, I can not seem to export the filled data to the csv file. What am I doing wrong?

最满意答案

由于您使用的是输入元素,因此text()方法不起作用 - 实际上<td>元素之间没有任何内容。 相反,您希望使用val()方法从所述输入元素获取文本。 这是一个快速而肮脏的例子: https : //jsfiddle.net/dzy5ktv6/

请注意,我更改了选择器以选择input元素而不是td 。

Since you are using input elements, the text() method won't work - essentially there is nothing between your <td> elements. Instead, you want to use the val() method to get the text from said input element. Here's a quick-and-dirty example: https://jsfiddle.net/dzy5ktv6/

Note that I changed the selector to select input elements instead of td.

更多推荐

本文发布于:2023-04-13 12:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/8a58acb393fd4fe423d140d3c119ab9d.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:导出到   csv   table   export

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!