借鉴自:使用JavaScript把页面上的表格导出为Excel文件
地址:https://www.cnblogs.com/zhuxinghan/p/6063193.html
JavaScript版的:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <meta charset="utf-8" /> <style> /* 此样式仅用于浏览器页面效果,Excel不会分离表格边框,不需要此样式 */ table { border-collapse: collapse; } </style> </head> <body> <!-- 设置border="1"以显示表格框线 --> <table border="1"> <!-- caption元素可以生成表标题,其单元格列跨度为表格的列数 --> <caption>学生成绩表</caption> <tr> <!-- 可以使用rowspan和colspan来合并单元格 --> <th rowspan="2">编号</th> <th rowspan="2">学号</th> <th rowspan="2">姓名</th> <th rowspan="2">性别</th> <th rowspan="2">年龄</th> <th colspan="3">成绩</th> </tr> <tr> <th>语文</th> <th>数学</th> <th>英语</th> </tr> <tr> <td>1</td> <td>2016001</td> <td>张三</td> <td>男</td> <td>13</td> <td>85</td> <td>94</td> <td>77</td> </tr> <tr> <td>2</td> <td>2016002</td> <td>李四</td> <td>女</td> <td>12</td> <td>96</td> <td>84</td> <td>89</td> </tr> </table>
<a>导出表格</a>
<script> // 使用outerHTML属性获取整个table元素的HTML代码(包括<table>标签),然后包装成一个完整的HTML文档,设置charset为urf-8以防止中文乱码 var html = "<html><head><meta charset='utf-8' /></head><body>" + document.getElementsByTagName("table")[0].outerHTML + "</body></html>"; // 实例化一个Blob对象,其构造函数的第一个参数是包含文件内容的数组,第二个参数是包含文件类型属性的对象 var blob = new Blob([html], { type: "application/vnd.ms-excel" }); var a = document.getElementsByTagName("a")[0]; // 利用URL.createObjectURL()方法为a元素生成blob URL a.href = URL.createObjectURL(blob); // 设置文件名 a.download = "学生成绩表.xls"; </script> </body> </html>
|
Angular版的,其实也差不多,只是构建一个table:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| exportCSV(value, columns, exportFilename) { const data = value; let table = '<table><tr>'; // headers for (let i = 0; i < columns.length; i++) { table += `<td>${columns[i]}</td>` } table += '</tr>' data.forEach((record) => { table += '<tr>'; for (let i_1 = 0; i_1 < columns.length; i_1++) { table += `<td>${record[columns[i_1]]}</td>`; } table += '</tr>' }); table += '</table>'
// 使用outerHTML属性获取整个table元素的HTML代码(包括<table>标签),然后包装成一个完整的HTML文档,设置charset为urf-8以防止中文乱码 var html = "<html><head><meta charset='utf-8' /></head><body>" + table + "</body></html>"; console.log(html); // 实例化一个Blob对象,其构造函数的第一个参数是包含文件内容的数组,第二个参数是包含文件类型属性的对象 var blob = new Blob([html], { type: "application/vnd.ms-excel" });
if (window.navigator.msSaveOrOpenBlob) { navigator.msSaveOrOpenBlob(blob, 'Sheet1.xls'); } else { const link = document.createElement('a'); link.style.display = 'none'; document.body.appendChild(link); if (link.download !== undefined) { link.setAttribute('href', URL.createObjectURL(blob)); link.setAttribute('download','Sheet1.xls'); link.click(); } document.body.removeChild(link); } }
|
导出CSV,其实个导出Excel差不多,只是改下属性,数据类型:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| analysisExportCSV(value, columns, exportFilename) { const data = value; let csv = '\ufeff'; // headers const columnsData = [ 'XXXX表头XXXX', 'XXXX表头XXXX', 'XXXX表头XXXX', 'XXXX表头XXXX', ]; for (let i = 0; i < columnsData.length; i++) { const column = columnsData[i]; csv += '"' + column + '"'; if (i < (columns.length)) { csv += this.csvSeparator; } } // body data.forEach((record) => { csv += '\n'; for (let i_1 = 0; i_1 < columns.length; i_1++) { const column = columns[i_1]; csv += '"' + this.resolveFieldData(record, column) + '"'; if (i_1 < (columns.length)) { csv += this.csvSeparator; } } }); const blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' }); if (window.navigator.msSaveOrOpenBlob) { navigator.msSaveOrOpenBlob(blob, exportFilename + '.csv'); } else { const link = document.createElement('a'); link.style.display = 'none'; document.body.appendChild(link); if (link.download !== undefined) { link.setAttribute('href', URL.createObjectURL(blob)); link.setAttribute('download', exportFilename + '.csv'); link.click(); } else { csv = 'data:text/csv;charset=utf-8,' + csv; window.open(encodeURI(csv)); } document.body.removeChild(link); } }
|
和导出CSV很相似嘛