【问题标题】:How to add cell border to SheetJS .xlsx generated file?如何将单元格边框添加到 SheetJS .xlsx 生成的文件?
【发布时间】:2018-05-01 02:17:40
【问题描述】:

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

我有这个:

我需要这个:

有没有办法用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

我已经创建了这个示例 sn-p,如果你可以在这里设置边框和/或其他单元格样式,那将是一个胜利:

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

【问题讨论】:

  • 您可以使用 codepen/jsfiddle/plunker 等将您的代码发布为 sn-p 吗?那很容易调试
  • @VicJordan 我已经添加了部分代码。
  • 请做一个代码sn-p。我无法执行并查看输出。
  • 我在导入所需库时遇到问题。
  • @VicJordan 我添加了一个 sn-p,请参阅我的第二次编辑。

标签: javascript sheetjs


【解决方案1】:

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

查看此官方评论:

这是社区版本。我们还提供专业版 性能增强、样式化等附加功能,以及 专门的支持。

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

【讨论】:

  • 我已经发了一封电子邮件,看看要多少钱。
  • @RobertoSepúlvedaBravo 你收到回复了吗?专业版的费用是多少?
【解决方案2】:
var wscols = [{wpx: 100}];
ws['!cols'] = wscols ;

【讨论】:

  • 我试过了,还是不行,你能解释一下怎么做吗?
  • @RobertoSepúlvedaBravo 嘿,我知道现在问这个问题已经太晚了,但是......你知道......你成功了吗?
  • @Chawchawchaw 我没有,但应该可以使用 sheetjs 专业版。
  • @RobertoSepúlvedaBravo 感谢您的回复。 :)
猜你喜欢
  • 1970-01-01
  • 2022-10-24
  • 1970-01-01
  • 1970-01-01
  • 2023-02-13
  • 2017-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多