gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 02:44:21 24 4
gpt4 key购买 nike

我有一个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com