I am trying to parse an array to xlsx using angulat and i am using this code
var app = angular.module('AngularExcel',[]);
app.controller('ArraytoExcel', function ($scope){
$scope.exportData = function () {
var data = [
{ name: "Barack Obama", pres: 44 },
{ name: "Donald Trump", pres: 45 }
];
/* generate a worksheet */
var ws = XLSX.utils.json_to_sheet(data);
/* add to workbook */
var wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, "Presidents");
/* write workbook (use type 'binary') */
var wbout = XLSX.write(wb, {bookType:'xlsx', type:'binary'});
/* generate a download */
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;
}
saveAs(new Blob([s2ab(wbout)],{type:"application/octet-stream"}), "sheetjs.xlsx");
}
});
and in html
<body ng-app="AngularExcel">
<div ng-controller="ArraytoExcel">
<h2> Angular to Excel </h2>
<button ng-click="exportData()">Export</button>
</div>
<!-- <script src="xlsxworker.flow.js"></script> -->
<script src="xlsx.js"></script>
<script src="xlsx.flow.js"></script>
<!-- <script src="xlsxworker.js"></script> -->
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<script src="angular.min.js"></script>
<script src="FileSaver.js"></script>
<script src="AngularExcel.js"></script>
<script src="https://rawgithub.com/eligrey/FileSaver.js/master/FileSaver.js" type="text/javascript"></script>
<script src="angular-js-xlsx.js"></script>
<script src="xlsx.full.min.js"></script>
<script src="xlsx.core.min.js"></script>
<script src="index.d.ts"></script>
<script src="JSJS.js"></script>
<!-- <script lang="javascript" src="dist/xlsx.full.min.js"></script> -->
</body>
and TypeError: XLSX.utils.json_to_sheet is not a function happens when i run the code
You pulled in way too many copies of the library!
https://github.com/SheetJS/js-xlsx/tree/master/demos/angular has a complete example that you can just copy and tinker with. Play with it here: http://plnkr.co/edit/2aqgi7lF8zzUiHTcAhOk?p=preview
now i only import
and the same problem
You might be using an older version. Can you run console.log(XLSX.version) in the browser console? The current version is 0.11.5 but you may have an older version
i am using 0.8.8 ,, from where can i get a newer version
It's available in various places, as explained in the README:
js-xlsxthaaaaaaaaaaaaank's a lot it solved the problem just now an error appears telling that ReferenceError: saveAs is not defined
can you help me in this
You need FileSaver.js: https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/1.3.3/FileSaver.min.js
I have this issue (the original utils not being defined) with v0.15.0, installed via npm. Currently I'm just loading xlsx.full.min.js, so I'm guessing I just need to load an additional file or two... (will update here if I figure this out first)
This issue is from 2017. xlsx.full.min.js has everything. If you are trying to write a file, use XLSX.writeFile -- it embeds all of the file writing logic
Most helpful comment
You need FileSaver.js: https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/1.3.3/FileSaver.min.js