XLSX
样式
字体、填充、边框、对齐和数字格式
XLSX 使用基于索引的样式系统。当你为单元格应用样式时,库会自动去重并注册样式。
字体样式
{
"worksheets": [
{
"name": "Sheet1",
"children": [
{
"cells": [
{ "value": "Bold" },
{
"value": "Styled",
"style": {
"font": {
"bold": true,
"italic": true,
"size": 14,
"color": "FF0000",
"fontName": "Arial"
}
}
}
]
}
]
}
]
}
{
cells: [
{ value: "Bold", style: { font: { bold: true } } },
{ value: "Styled", style: { font: { bold: true, italic: true, size: 14, color: "FF0000", fontName: "Arial" } } },
],
}
填充样式
{
"worksheets": [
{
"name": "Sheet1",
"children": [
{
"cells": [
{ "value": "Yellow", "style": { "fill": { "color": "FFFF00" } } },
{ "value": "Blue", "style": { "fill": { "color": "4472C4" } } }
]
}
]
}
]
}
{
cells: [
{ value: "Yellow", style: { fill: { color: "FFFF00" } } },
{ value: "Blue", style: { fill: { color: "4472C4" } } },
],
}
边框样式
{
"worksheets": [
{
"name": "Sheet1",
"children": [
{
"cells": [
{
"value": "Bordered",
"style": {
"border": {
"top": { "style": "thin", "color": "000000" },
"bottom": { "style": "thin", "color": "000000" },
"left": { "style": "thin", "color": "000000" },
"right": { "style": "thin", "color": "000000" }
}
}
}
]
}
]
}
]
}
{
value: "Bordered",
style: {
border: {
top: { style: "thin", color: "000000" },
bottom: { style: "thin", color: "000000" },
left: { style: "thin", color: "000000" },
right: { style: "thin", color: "000000" },
},
},
}
边框样式可选值:thin、medium、thick、dotted、dashed、double、hair、none。
对齐
{
"worksheets": [
{
"name": "Sheet1",
"children": [
{
"cells": [
{
"value": "Centered",
"style": {
"alignment": { "horizontal": "center", "vertical": "center", "wrapText": true }
}
}
]
}
]
}
]
}
{
value: "Centered",
style: {
alignment: { horizontal: "center", vertical: "center", wrapText: true },
},
}
组合样式
样式会自动组合和去重:
{
"worksheets": [
{
"name": "Sheet1",
"children": [
{
"cells": [
{
"value": "Name",
"style": {
"font": { "bold": true, "color": "FFFFFF" },
"fill": { "color": "4472C4" },
"alignment": { "horizontal": "center" }
}
},
{
"value": "Score",
"style": {
"font": { "bold": true, "color": "FFFFFF" },
"fill": { "color": "4472C4" },
"alignment": { "horizontal": "center" }
}
}
]
},
{ "cells": [{ "value": "Alice" }, { "value": 95 }] }
]
}
]
}
const headerStyle = {
font: { bold: true, color: "FFFFFF" },
fill: { color: "4472C4" },
alignment: { horizontal: "center" },
};
{
cells: [
{ value: "Name", style: headerStyle },
{ value: "Score", style: headerStyle },
],
}
重复使用相同的样式对象会复用同一个 cellXf 索引,不会在输出文件中产生不必要的重复。
字体选项参考
| 选项 | 类型 | 说明 |
|---|---|---|
bold | boolean | 加粗 |
italic | boolean | 斜体 |
underline | boolean | 下划线 |
strike | boolean | 删除线 |
size | number | 字号(磅) |
color | string | 十六进制颜色 |
fontName | string | 字体名称 |
边框选项参考
| 选项 | 类型 | 说明 |
|---|---|---|
style | string | thin、medium、thick、dotted、dashed 等 |
color | string | 十六进制颜色 |
对齐选项参考
| 选项 | 类型 | 说明 |
|---|---|---|
horizontal | string | left、center、right、fill、justify |
vertical | string | top、center、bottom |
wrapText | boolean | 单元格内自动换行 |
textRotation | number | 文本旋转角度 |
indent | number | 缩进级别 |