我正在尝试使用SheetJS以便将一些js数组推入Excel。我看了一些教程,并使用了SheetJS的源代码作为初始测试。我已经调试,除了saveAs()方法之外,其他所有东西似乎都可以正常工作。还有其他人有同样的问题吗?
这是我在HTML中使用的链接:
<link href="Style.css" type="text/css" rel="stylesheet">
<script src="scripts.js"></script>
<script src="data.js"></script>
<script src="xlsx.full.min.js"></script>
<script src="Blob.js"></script>
<script src="FileSaver.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>window.jQuery || document.write(decodeURIComponent('%3Cscript src="js/jquery-3.1.0.min.js"%3E%3C/script%3E'))</script>
这是我用于保存xls文件的js方法。
function exportToExcel()
{
var wb = XLSX.utils.book_new();
wb.Props = {
Title: "SheetJS Tutorial",
Subject: "Test",
Author: "Red Stapler",
CreatedDate: new Date(2017,12,19)
};
wb.SheetNames.push("Test Sheet");
var ws_data = [['hello' , 'world']]; //a row with 2 columns
var ws = XLSX.utils.aoa_to_sheet(ws_data);
wb.Sheets["Test Sheet"] = ws;
//Exporting the Workbook for Downloading
var wbout = XLSX.write(wb, {bookType:'xlsx', type: 'binary'});
//Convert to binary Data
function s2ab(s) {
var buf = new ArrayBuffer(s.length); //convert s to arrayBuffer
var view = new Uint8Array(buf); //create uint8array as viewer
for (var i=0; i<s.length; i++) view[i] = s.charCodeAt(i) & 0xFF; //convert to octet
return buf;
}
saveExcel();
}
function saveExcel()
{
//$("#toExcel").click(function(){
//var FileSaver = require('file-saver');
var filename = $("#input-fileName").val();
saveAs(new Blob([s2ab(wbout)],{type:"application/octet-stream"}), filename + '.xlsx');
alert('saved');
}
这是我想要实现的,但是由于某种原因,我无法保存文件: https://www.youtube.com/watch?v=41rOAt-zCu4
与链接的JQuery结合fileSaver的版本有关吗?在视频中,他们正在谈论FileSaver.min.js ...,但我只能找到FileSaver.JS。