通过borwser将jquery在函数中创建的html表导出到excel文件

iibxawm4  于 2024-01-07  发布在  jQuery
关注(0)|答案(1)|浏览(137)

我使用jQuery从一个页面收集了大量的数据,并将它们存储在变量中,然后我将这些变量传递给一个函数,在那里我试图动态创建一个表,然后一旦完成导出CSV文件,当浏览器提示时,我可以在本地保存。
我已经把它带到了下面这么远的地方,但我似乎找不到正确的方法来创建Excel文件,该文件包含由表周围的ID选择的表。
我想知道我是否可以得到一些帮助,建议我是否走在正确的道路上,如果有人可以帮助我越过终点线。

  1. function exportStaffProfileExcel(profileTitle,profileDepartment,profileLocation,profileTelephone,profileEmail,profileBiography) {
  2. var table = "";
  3. table += "<table>";
  4. table += "<tr><td colspan='2'>Name : " + 'Work!' + "</td></tr></table>";
  5. var uri = 'data:application/vnd.ms-excel;base64,';
  6. var template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><?xml version="1.0" encoding="UTF-8" standalone="yes"?><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>';
  7. var base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }
  8. var format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) }
  9. var toExcel = $(table).html();
  10. var ctx = {
  11. worksheet: name || '',
  12. table: toExcel
  13. };
  14. newWindow = window.open(uri + base64(format(template, ctx)));
  15. return newWindow;
  16. //window.open(uri + base64(format(template, ctx)));
  17. };

字符串

d6kp6zgx

d6kp6zgx1#

我有一些工作,希望这是一个很好的选择:

  1. function exportStaffProfileExcel(profileTitle,profileDepartment,profileLocation,profileTelephone,profileEmail,profileBiography) {
  2. var table = "";
  3. table += "<table>";
  4. table += "<tr><td colspan='2'>Name : " + 'Work!' + "</td></tr>";
  5. table += "</table>";
  6. // Get table HTML content
  7. var tableHtml = $(table).prop('outerHTML');
  8. // Create a Blob with the table HTML
  9. var blob = new Blob([tableHtml], { type: 'application/vnd.ms-excel' });
  10. // Create a data URI for the Blob
  11. var url = window.URL.createObjectURL(blob);
  12. // Create a temporary link element
  13. var link = document.createElement('a');
  14. link.href = url;
  15. // Set the download attribute and filename
  16. link.download = 'myExcelFile.xls';
  17. // Append the link to the document
  18. document.body.appendChild(link);
  19. // Trigger a click on the link to start the download
  20. link.click();
  21. // Remove the link from the document
  22. document.body.removeChild(link);
  23. };

字符串

展开查看全部

相关问题