Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add cell border to SheetJS .xlsx generated file?

I have a SheetJS .xlsx generated file, but I have not been able to put border to the cells.

I have this:

enter image description here

And I need this:

enter image description here

Is there a way to do it with SheetJS? It will be cool if there's a way to apply another cell styles, like background color.

EDIT:

I'm making the sheets with this function:

function makeSheet(wb, day){        //sheet for a specific day
    var ws = XLSX.utils.table_to_sheet(document.getElementById("table"+day));
    wb.SheetNames.push(day);
    wb.Sheets[day] = ws;
    //columns width working ok
    wb["Sheets"][day]["!cols"] = [{ wpx : 70 },{ wpx : 121 }];
    //wb["Sheets"][day]["A1"]["s"] = {"border":"1px"};  //I've tried this but doesn't work
  return wb;
}

EDIT 2

I've created this example snippet, if you can put borders and/or another cell style here, that's going to be a victory:

$(document).ready(myMain);
function myMain(){
  $(document).on("click","#btnexcel", function(){makeExcel()});
}
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 makeExcel(){
	var wb = XLSX.utils.table_to_book(document.getElementById("myTable"),{sheet:"Sheet 1"})	//my html table

	var wbout = XLSX.write(wb, {bookType:'xlsx',  bookSST:true, type: 'binary'});
	saveAs(new Blob([s2ab(wbout)],{type:"application/octet-stream"}), 'MyExcel.xlsx');
}
<!-- JQuery  -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<!-- Bootstrap CSS-->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<!-- Bootstrap JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

<script lang="javascript" src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.12.13/xlsx.full.min.js"></script>
<script src="https://fastcdn.org/FileSaver.js/1.1.20151003/FileSaver.min.js"></script>

<button id="btnexcel">Download excel</button>
<table id="myTable" border="1">
  <thead><tr><th>hello</th><th>dear community</th></tr></thead>
  <tbody>
    <tr><td>I need borders</td><td>around here</td></tr>
    <tr><td>I'll be glad</td><td>if you help me</td></tr>
  </tbody>
</table>
like image 680
Roberto Sepúlveda Bravo Avatar asked May 01 '18 02:05

Roberto Sepúlveda Bravo


2 Answers

You are using community version of SheetJS library and with this version can not do styling. BUT in Pro Version styling feature is available.

Checkout this official comment:

This is the community version. We also offer a pro version with performance enhancements, additional features like styling, and dedicated support.

For more information about Pro Version visit their official site: http://sheetjs.com/pro

like image 105
Vikasdeep Singh Avatar answered Sep 28 '22 16:09

Vikasdeep Singh


var wscols = [{wpx: 100}];
ws['!cols'] = wscols ;
like image 36
Varun Rayen Avatar answered Sep 28 '22 15:09

Varun Rayen