I am trying to use SheetJS in order to push some js arrays to Excel. I looked at some tutorials and used the source code from SheetJS as an initial test. I have debugged and everything seems to work fine apart from the saveAs() method. Has anyone else had the same problem?
Here are the links I'm using in my 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>
And here is my js method for saving the xls file.
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');
}
This is what I want to achieve, but for some reason I'm not able to save the file: https://www.youtube.com/watch?v=41rOAt-zCu4
Does it have something to do with the versions of my linked JQuery in combination with fileSaver? In the video they are talking about FileSaver.min.js... but I could only find FileSaver.JS.