SheetJS xlsx 单元格样式

SheetJS xlsx-cell styling

我指的是导出工作表的示例https://github.com/SheetJS/js-xlsx/issues/817。细胞怎么做 样式,如背景颜色、字体大小和增加宽度 使数据适合的单元格 exactly.I 已经阅读了文档,但找不到任何合适的示例来使用填充 etc.Is 有没有办法进行格式化?

Below is the code snippet:
    /* make the worksheet */
var ws = XLSX.utils.json_to_sheet(data);

/* add to workbook */
var wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, "People");

/* write workbook (use type 'binary') */
var wbout = XLSX.write(wb, {bookType:'xlsx', type:'binary'});

/* generate a download */
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;
}
saveAs(new Blob([s2ab(wbout)],{type:"application/octet- 
stream"}),"sheetjs.xlsx");

样式 仅在 SheetJSPro Version 中可用。但我认为您使用的是社区版(免费版)。 社区版样式不可用

你可以在这里查看official information:

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

我使用 sheetjs-style(它是 sheetjs 的一个分支)为 excel 文件中的单元格添加格式。

ws["A1"].s =        // set the style for target cell
  font: {
    name: '宋体',
    sz: 24,
    bold: true,
    color: { rgb: "FFAA00" }
  },
};

这很容易。但是,您必须为每个单独的单元格添加样式。给一系列单元格添加样式不方便。

更新:官方示例使用颜色“FFFFAA00”。但是我删除了第一个“FF”,它仍然像以前一样工作。删除的部分用于透明度(参见Cell Styles中的COLOR_SPEC),但不知何故当我更改或删除它时它没有任何效果。

有很多允许样式化的社区分叉。我个人最喜欢的是 xlsx-js-style. 与其他库相比,它是最新的并且运行良好。

sheetjs-style is also up to date, but i had some problems with it. See: Styles not working

xlsx-style 不是最新的。目前有 397 个提交落后于 SheetJS:master。如果可能我不会使用它。

所有这些库共享相同的样式选项。这是一堆例子:

for (i in ws) {
    if (typeof(ws[i]) != "object") continue;
    let cell = XLSX.utils.decode_cell(i);

    ws[i].s = { // styling for all cells
        font: {
            name: "arial"
        },
        alignment: {
            vertical: "center",
            horizontal: "center",
            wrapText: '1', // any truthy value here
        },
        border: {
            right: {
                style: "thin",
                color: "000000"
            },
            left: {
                style: "thin",
                color: "000000"
            },
        }
    };

    if (cell.c == 0) { // first column
        ws[i].s.numFmt = "DD/MM/YYYY HH:MM"; // for dates
        ws[i].z = "DD/MM/YYYY HH:MM";
    } else { 
        ws[i].s.numFmt = "00.00"; // other numbers
    }

    if (cell.r == 0 ) { // first row
        ws[i].s.border.bottom = { // bottom border
            style: "thin",
            color: "000000"
        };
    }

    if (cell.r % 2) { // every other row
        ws[i].s.fill = { // background color
            patternType: "solid",
            fgColor: { rgb: "b2b2b2" },
            bgColor: { rgb: "b2b2b2" } 
        };
    }
}

测试以上所有选项后。对于 ReactJS,我终于找到了一个完美运行的包。

https://github.com/ShanaMaid/sheetjs-style

import XLSX from 'sheetjs-style'; 

var workbook = XLSX.utils.book_new();

var ws = XLSX.utils.aoa_to_sheet([
    ["A1", "B1", "C1"],
    ["A2", "B2", "C2"],
    ["A3", "B3", "C3"]
])
ws['A1'].s = {
    font: {
        name: 'arial',
        sz: 24,
        bold: true,
        color: "#F2F2F2"
    },
}

XLSX.utils.book_append_sheet(workbook, ws, "SheetName");
XLSX.writeFile(workbook, 'FileName.xlsx');