javascript - 如何将单元格边框添加到 SheetJS .xlsx 生成的文件?

标签 javascript sheetjs

我有一个 SheetJS .xlsx 生成的文件,但我无法将 border 放入 cells

我有这个:

enter image description here

我需要这个:

enter image description here

有没有办法用 SheetJS 做到这一点?如果有一种方法可以应用其他单元格样式(如背景颜色),那就太棒了。

编辑:

我正在制作具有此功能的床单:

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;
}

编辑 2

我已经创建了这个示例片段,如果您可以在此处放置边框和/或其他单元格样式,那将是一个胜利:

$(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>

最佳答案

您正在使用 SheetJS 库的社区版本并且使用此版本不能进行样式设置。但是在 Pro Version样式功能可用。

查看此官方评论:

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

有关专业版的更多信息,请访问其官方网站:http://sheetjs.com/pro

关于javascript - 如何将单元格边框添加到 SheetJS .xlsx 生成的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50110595/

相关文章:

javascript - 使用 Javascript 转换 JSON 日期

javascript - 获取简短的 JavaScript CustomEvent polyfill 以在 TypeScript 中进行编译?

javascript - SheetJS json_to_sheet 重命名标题

javascript - 使用 SheetJS 将 JSON 对象数组导出到 Excel

angular - SheetJS json_to_sheet 在 ISO 8601 中将日期写为字符串

angular - 将数据导出到 excel 时,日期会根据时区发生变化。如何避免?

javascript - Vuejs - 如何在元素上的加载事件上调用方法

javascript - Bootstrap 下拉菜单冲突 jQuery 1.11.3

javascript - 如何将 JavaScript [object String] 转换为 HTMLElement?

javascript - 工作表js xlsx writeFile回调