Create an excel file with a few styles using client side JavaScript (if possible using js-xlsx library) -


i want create excel file(in .xlsx format) , make available download using client side javascript. able create sample file using js-xlsx library. not able apply styles it. @ least basic styles including background color header, bold font header , text wrapping cells required.

js-xlsx library documentation says can give styles using cell object.

i tried giving styles using cell object not reflecting in downloaded .xlsx file. tried reading .xlsx file , writing same file using xlsx.write() function. gives excel file no styles @ all. ideally expect downloaded file have same styles of uploaded file. no font color or background colors applied in recreated file. use excel 2013 testing downloaded files.

please find below excel screenshots before , after uploading.

original file

enter image description here

downloaded file

enter image description here

the code given below.

<html> <head> <meta http-equiv="content-type" content="text/html; charset=windows-1252"> <script type="text/javascript" src="xlsx.core.min.js"></script> <script type="text/javascript" src="blob.js"></script> <script type="text/javascript" src="filesaver.js"></script>  <script>  function s2ab(s) {     var buf = new arraybuffer(s.length);     var view = new uint8array(buf);     (var i=0; i!=s.length; ++i) view[i] = s.charcodeat(i) & 0xff;     return buf; }     /* set xmlhttprequest */ var url = "template-sample.xlsx"; var oreq = new xmlhttprequest(); oreq.open("get", url, true); oreq.responsetype = "arraybuffer";  oreq.onload = function(e) {   var arraybuffer = oreq.response;    /* convert data binary string */   var data = new uint8array(arraybuffer);   var arr = new array();   for(var = 0; != data.length; ++i) arr[i] = string.fromcharcode(data[i]);   var bstr = arr.join("");    /* call xlsx */   var workbook = xlsx.read(bstr, {type:"binary", cellstyles:true});      console.log("read workbook");     console.log(workbook);   /* workbook here */     var wbout = xlsx.write(workbook, {booktype:'xlsx', booksst:true, type: 'binary', cellstyles: true});     saveas(new blob([s2ab(wbout)],{type:"application/octet-stream"}), "template-download.xlsx");  }  function read(){     oreq.send();     }   </script>   </head> <body>     <button onclick="read()">save xlsx</button> </body></html> 

sample code taken here.

what forward either option give styles cells using js-xlsx library or library provides functionality. found library named exceljs , requires node js support it. looking purely client side based solution. used cordova based tablet , desktop application.

after research able find solution own question. found new library named xlsx-style giving styles. xlsx-style build on top of js-xlsx giving styles generated excel file. styles can given cells using new attribute inside cell object.

the explanation available @ npm xlsx-style page.

styling given using style object associated each cell. font, color, alignment etc can given using style object.

i have added minimalist demo in github page. sample code available @ github repository.

you can find screenshot of generated excel page below. enter image description here


Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - jQuery UI Splitter/Resizable for unlimited amount of columns -

javascript - IE9 error '$'is not defined -