SheetJS xlsx样式需要Excel中的单元格样式

SheetJS xlsx-style need cell style in excel(SheetJS xlsx样式需要Excel中的单元格样式)

本文介绍了SheetJS xlsx样式需要Excel中的单元格样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用SheetJS/xlsx导出EXCEL,并希望设置单元格格式。我正在使用以下代码,Excel正在生成,但无法格式化单元格。任何一个人都可以指出该问题,或者可以为此共享完整的示例代码吗?

加载库文件

<script src="aHR0cHM6Ly9jb2RlLmpxdWVyeS5jb20vanF1ZXJ5LTMuMS4xLm1pbi5qcw=="></script>
<script type="text/javascript" src="aHR0cDovL29zcy5zaGVldGpzLmNvbS9qcy14bHN4L3hsc3guY29yZS5taW4uanM="></script>
<script type="text/javascript" src="aHR0cDovL3NoZWV0anMuY29tL2RlbW9zL0ZpbGVTYXZlci5qcw=="></script>

剩余代码为

function Workbook() {
    if(!(this instanceof Workbook)) return new Workbook();
    this.SheetNames = [];
    this.Sheets = {};
}

function sheet_from_array_of_arrays(data, opts) {
    var ws = {};
    var range = {s: {c:10000000, r:10000000}, e: {c:0, r:0 }};
    for(var R = 0; R != data.length; ++R) {
        for(var C = 0; C != data[R].length; ++C) {
            if(range.s.r > R) range.s.r = R;
            if(range.s.c > C) range.s.c = C;
            if(range.e.r < R) range.e.r = R;
            if(range.e.c < C) range.e.c = C;
            var cell = {v: data[R][C],
                  s: { alignment: {textRotation: 90 },
                       font: {sz: 14, bold: true, color: #FF00FF }
                };
        //cell.s = {}
       /*var cell ={ v: '2.4.2014',
        t: 's',
        r: '<t>2.4.2014</t>',
        h: '2.4.2014',
        w: '2.4.2014',
        s: 
        { patternType: 'solid',
          fgColor: { theme: 8, tint: 0.3999755851924192, rgb: '9ED2E0' },
          bgColor: { indexed: 64 } } };

      */
            if(cell.v == null) continue;
            var cell_ref = XLSX.utils.encode_cell({c:C,r:R});

            if(typeof cell.v === 'number') cell.t = 'n';
            else if(typeof cell.v === 'boolean') cell.t = 'b';
            else if(cell.v instanceof Date) {
                cell.t = 'n'; cell.z = XLSX.SSF._table[14];
                cell.v = datenum(cell.v);
            }
            else cell.t = 's';

            ws[cell_ref] = cell;
        }
    }
    if(range.s.c < 10000000) ws['!ref'] = XLSX.utils.encode_range(range);
    return ws;
}
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 GenerateExcelFile(inData, colWidth){
  var wb = new Workbook();
  var ws = sheet_from_array_of_arrays(inData); 

  var ws_name = "SheetJS";  

  /* add worksheet to workbook */
wb.SheetNames.push(ws_name);
wb.Sheets[ws_name] = ws;
/* TEST: column widths */
ws['!cols'] = colWidth;

var wbout = XLSX.write(wb, {bookType:'xlsx', bookSST:true, type: 'binary'});
saveAs(new Blob([s2ab(wbout)],{type:"application/octet-stream"}), "test.xlsx")

}

使用这些代码调用

var excelData = "JSON DATA";
  var wscols = [
    {wch:30},
    {wch:20},
    {wch:20}
];

<button onclick="GenerateExcelFile(excelData,wscols)">Export</button>

请帮我找出我哪里错了。

谢谢 苏曼

推荐答案

仅在专业版中不能使用SheetJs/Xlsx模块执行任何样式,因此可以使用exel4node轻松设置样式

这篇关于SheetJS xlsx样式需要Excel中的单元格样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:SheetJS xlsx样式需要Excel中的单元格样式