我在 Stack Overflow 上找不到针对我的特定问题的现有问题/答案,所以我发布了这个。我有一个用 HTML 和 jQuery/JavaScript 编写的应用程序。我需要使用 jsxlsx (sheetsjs) 插件将 HTML 表格导出到 Excel 表格中。我尝试复制他们的示例以将 HTML 表转换为 Excel,但程序出错。这是我目前拥有的:
我的 HTML 代码位于名为 books.cfm 的冷融合页面中。我的 JavaScript 在一个名为 Utils.js 的文件中。这些是:
function s2ab(s) {
var buf = new ArrayBuffer(s.length);
var view = new Uint8Array(buf);
for (var i=0; i<s.length; i++)
view[i] = s.charCodeAt(i) & 0xFF;
return buf;
}
function convertToExcel(event){
event.preventDefault();
console.log("button clicked");
var wb = XLSX.utils.table_to_book( $("#exceltable"), {sheet:"Exported
table"} );
saveAs(new Blob([s2ab(wb)],{type:"application/octet-stream"}),
'test.xlsx');
}
<html>
<head>
<script src="libs/jquery.min.js"></script>
<script src="libs/sheets/xlsx.full.min.js"></script>
<script src="libs/FileSaver/FileSaver.min.js"></script>
<script src="scripts/Utils.js"></script>
</head>
<body>
<table id="myTable">
<tr>
<td>ID</td>
<td>Name</td>
</tr>
<tr>
<td>1</td>
<td>Johnny</td>
</tr>
</table>
<button id="myBtn" onclick="convertToExcel(event)">Export</button>
</body>
</html>
当我运行上面的代码并单击按钮转换为 Excel 时,我收到以下错误:
**Uncaught TypeError: e.getElementsByTagName is not a function
at GC (xlsx.full.min.js:20)
at Object.jC [as table_to_book] (xlsx.full.min.js:20)
at exportErrsToExcel (Util.js:1227)
at HTMLButtonElement.onclick (books.cfm:18)**
注意:我从这个站点获得了上面的 JavaScript 代码。